社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

【Python】Pandas+Numpy+Sklearn随机取数

机器学习初学者 • 3 年前 • 325 次点击  

公众号:尤而小屋
作者:Peter
编辑:Peter

大家好,我是Peter~

本文记录的是如何使用Python、pandas、numpy、scikit-learn来实现随机打乱、抽取和切割数据。主要的方法包含:

  • sample
  • shuffle
  • np.random.permutation
  • train_test_split

导入数据

In [1]:

import pandas as pd
import numpy as np
import random  # 随机模块

import plotly_express as px  # 可视化库
import plotly.graph_objects as go

内置数据

采用的是plotly库中内置的一份消费数据集:

In [2]:

df = px.data.tips()
df.head()

基本信息

In [3]:

df.shape

Out[3]:

(244, 7)

In [4]:

columns = df.columns
columns

Out[4]:

Index(['total_bill''tip''sex''smoker''day''time''size'], dtype='object')

sample实现

行方向

In [5]:

随机抽取一行记录:

df.sample()  # 随机抽取一行记录

随机抽取多行数据:

通过参数frac实现按照比例随机抽样:

df.sample(frac=0.05)

列方向

主要是选择不同数量或者比例的属性;整体的行数量是不变的

In [8]:

df.sample(3, axis=1)  # 在列属性上抽取

shuffle实现

scikit-Learn的shuffle

In [9]:

from sklearn.utils import shuffle

In [10]:

shuffle(df)  # 打乱数据

random模块的shuffle

In [11]:

length = list(range(len(df)))  # 原始的长度作为索引
length[:5]

Out[11]:

[0, 1, 2, 3, 4]

In [12]:

random.shuffle(length)  # 打乱索引

In [13]:




    
length[:5]

Out[13]:

[136, 35, 207, 127, 29]  # 打乱后的结果

In [14]:

df.iloc[length]   # 通过打乱后的索引获取数据

numpy实现

In [15]:

# 先打乱每个索引
np.random.permutation(len(df))

Out[15]:

array([223,  98238,  17101,  26122212,  27,  79210147176,
        82164142141219,   6,  63185112158188242207,
        45,  55178150217,  32,  16160157234,  95174,  93,
        52,  57220216230,  35,  86125114100,  73,  83,  88,
        34,   7,  40115,  97165,  84,  18197151135121,  72,
       173228143227,   9183,  56,  23237136106133189,
       139,   0208,  74166,   4,  68,  12 ,  71,  85172138149,
       144232186,  99130,  41201204,  10167195,  66159,
       213,  87103117,  31211190,  24243127,  48218233,
       113,  81235229206,  96,  46222,  50156180214124,
       240140,  89225,   2120,  58169193,  39102104148,
       184170152153146179137129,  64,   3,  65128,  90,
       110,  14226181131203221,  80,  51,  94231,  44108,
        43145,  47,  75162163,  69126200,   1123,  37205,
       111,  25,  91,  11,  42,  67118196161,  28116105,  33,
        38,  78,  76224,  20202171177107,   8209239,  77,
       241154,   5198,  92,  61182,  36,  70,  22,  54187175,
       119215,  49134,  21 ,  60,  62168,  59155194109132,
        19199,  29191,  13,  30192236,  15,  53])

In [16]:

# 通过打乱后的索引来选择数据

df.iloc[np.random.permutation(len(df))]

train_test_split实现

from sklearn.model_selection import train_test_split

data = []

for i in train_test_split(df, test_size=0.2):
    data.append(i)

In [18]:

第一份数据是80%的:

data[0]   # 80%的数据

剩余的20%的数据:

往期精彩回顾




Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/135430
 
325 次点击