最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pandas+Numpy+Sklearn隨機取數(shù)的實現(xiàn)示例

 更新時間:2024年03月19日 15:05:46   作者:尤而小屋  
使用Python、pandas、numpy、scikit-learn來實現(xiàn)隨機打亂、抽取和切割數(shù)據(jù),文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文記錄的是如何使用Python、pandas、numpy、scikit-learn來實現(xiàn)隨機打亂、抽取和切割數(shù)據(jù)。主要的方法包含:

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

導(dǎo)入數(shù)據(jù)

In [1]:

import pandas as pd
import numpy as np
import random  # 隨機模塊

import plotly_express as px  # 可視化庫
import plotly.graph_objects as go

內(nèi)置數(shù)據(jù)

采用的是plotly庫中內(nèi)置的一份消費數(shù)據(jù)集:

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實現(xiàn)

行方向

In [5]:

隨機抽取一行記錄:

df.sample()  # 隨機抽取一行記錄

隨機抽取多行數(shù)據(jù):

通過參數(shù)frac實現(xiàn)按照比例隨機抽樣:

df.sample(frac=0.05)

列方向

主要是選擇不同數(shù)量或者比例的屬性;整體的行數(shù)量是不變的

In [8]:

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

shuffle實現(xiàn)

scikit-Learn的shuffle

In [9]:

from sklearn.utils import shuffle

In [10]:

shuffle(df)  # 打亂數(shù)據(jù)

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]  # 打亂后的結(jié)果

In [14]:

df.iloc[length]   # 通過打亂后的索引獲取數(shù)據(jù)

numpy實現(xiàn)

In [15]:

# 先打亂每個索引
np.random.permutation(len(df))

Out[15]:

array([223,  98, 238,  17, 101,  26, 122, 212,  27,  79, 210, 147, 176,
        82, 164, 142, 141, 219,   6,  63, 185, 112, 158, 188, 242, 207,
        45,  55, 178, 150, 217,  32,  16, 160, 157, 234,  95, 174,  93,
        52,  57, 220, 216, 230,  35,  86, 125, 114, 100,  73,  83,  88,
        34,   7,  40, 115,  97, 165,  84,  18, 197, 151, 135, 121,  72,
       173, 228, 143, 227,   9, 183,  56,  23, 237, 136, 106, 133, 189,
       139,   0, 208,  74, 166,   4,  68,  12,  71,  85, 172, 138, 149,
       144, 232, 186,  99, 130,  41, 201, 204,  10, 167, 195,  66, 159,
       213,  87, 103, 117,  31, 211, 190,  24, 243, 127,  48, 218, 233,
       113,  81, 235, 229, 206,  96,  46, 222,  50, 156, 180, 214, 124,
       240, 140,  89, 225,   2, 120,  58, 169, 193,  39, 102, 104, 148,
       184, 170, 152, 153, 146, 179, 137, 129,  64,   3,  65, 128,  90,
       110,  14, 226, 181, 131, 203, 221,  80,  51,  94, 231,  44, 108,
        43, 145,  47,  75, 162, 163,  69, 126, 200,   1, 123,  37, 205,
       111,  25,  91,  11,  42,  67, 118, 196, 161,  28, 116, 105,  33,
        38,  78,  76, 224,  20, 202, 171, 177, 107,   8, 209, 239,  77,
       241, 154,   5, 198,  92,  61, 182,  36,  70,  22,  54, 187, 175,
       119, 215,  49, 134,  21,  60,  62, 168,  59, 155, 194, 109, 132,
        19, 199,  29, 191,  13,  30, 192, 236,  15,  53])

In [16]:

# 通過打亂后的索引來選擇數(shù)據(jù)

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

train_test_split實現(xiàn)

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]:

第一份數(shù)據(jù)是80%的:

data[0]   # 80%的數(shù)據(jù)

剩余的20%的數(shù)據(jù):

到此這篇關(guān)于Pandas+Numpy+Sklearn隨機取數(shù)的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Pandas+Numpy+Sklearn隨機取數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

吉安县| 通许县| 龙江县| 云南省| 新安县| 武强县| 锡林浩特市| 东兰县| 松江区| 大庆市| 盐边县| 宁安市| 蒙阴县| 广丰县| 蒙城县| 华亭县| 越西县| 余庆县| 松滋市| 淮北市| 蒲江县| 云阳县| 高碑店市| 阜南县| 苍溪县| 丹巴县| 英吉沙县| 云龙县| 景德镇市| 北海市| 孟连| 广德县| 沂水县| 洮南市| 昌宁县| 贵南县| 汉阴县| 镇雄县| 克东县| 辽中县| 舒城县|