<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Pandas+Numpy+Sklearn隨機(jī)取數(shù)

          共 5523字,需瀏覽 12分鐘

           ·

          2022-06-17 21:09

          本文轉(zhuǎn)自公眾號(hào):尤而小屋

          本文記錄的是如何使用Python、pandas、numpy、scikit-learn來(lái)實(shí)現(xiàn)隨機(jī)打亂、抽取和切割數(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  # 隨機(jī)模塊

          import plotly_express as px  # 可視化庫(kù)
          import plotly.graph_objects as go

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

          采用的是plotly庫(kù)中內(nèi)置的一份消費(fè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實(shí)現(xiàn)

          行方向

          In [5]:

          隨機(jī)抽取一行記錄:

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

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

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

          df.sample(frac=0.05)

          列方向

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

          In [8]:

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

          shuffle實(shí)現(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)))  # 原始的長(zhǎng)度作為索引
          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]   # 通過(guò)打亂后的索引獲取數(shù)據(jù)

          numpy實(shí)現(xiàn)

          In [15]:

          # 先打亂每個(gè)索引
          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]:

          # 通過(guò)打亂后的索引來(lái)選擇數(shù)據(jù)

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

          train_test_split實(shí)現(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)閱讀:


          瀏覽 71
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  操屄黄片| 亚洲小电影在线 | 婷婷色五月天丁香色 | 日本高清成人片在线免费 | 亚洲成人色老头77777性爱网 |