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

          【機(jī)器學(xué)習(xí)】平衡你的數(shù)據(jù)集的幾項(xiàng)技巧

          共 3368字,需瀏覽 7分鐘

           ·

          2021-05-10 20:47

          作者 | Praveen Thenraj

          編譯 | VK
          來(lái)源 | Towards Data Science


          “如果你被問(wèn)題的規(guī)模所壓倒,把它分解成更小的部分……”
          機(jī)器學(xué)習(xí)中的類不平衡是一個(gè)重要的問(wèn)題,也是處理數(shù)據(jù)集時(shí)最受關(guān)注的問(wèn)題之一。類不平衡的數(shù)據(jù)集往往更具欺騙性,如果處理不當(dāng),結(jié)果可能導(dǎo)致錯(cuò)誤的決策。
          最近我碰巧在一個(gè)數(shù)據(jù)集中工作,它有一個(gè)類不平衡的問(wèn)題。我不太喜歡數(shù)據(jù)生成技術(shù),并且由于不平衡的嚴(yán)重性,我不想使用像分層K-fold這樣的技術(shù),所以我使用了另一種技術(shù),它幫助模型比傳統(tǒng)技術(shù)更好地學(xué)習(xí)數(shù)據(jù)。
          也許我有點(diǎn)晚使用這個(gè)技術(shù),但我想在這里分享它。
          import pandas as pd

          train=pd.read_csv('/Desktop/Files/train_data.csv')
          print(train['Top-up Month'].value_counts())

          No Top-up Service    106677
           > 48 Months           8366
          36-48 Months           3656
          24-30 Months           3492
          30-36 Months           3062
          18-24 Months           2368
          12-18 Months           1034
          Name: Top-up Month, dtype: int64
          以上是訓(xùn)練數(shù)據(jù)中因變量的值計(jì)數(shù)。從上面可以清楚地看到,“No Top-up Service”類和其他類之間存在嚴(yán)重的類不平衡。


          方法


          所以我在這里采用的方法是將大多數(shù)類數(shù)據(jù)分成大小為“n”的小折。每個(gè)數(shù)據(jù)塊(折)都有與大多數(shù)類相關(guān)的“k”個(gè)數(shù)據(jù)點(diǎn)。所有其他次要類數(shù)據(jù)點(diǎn)都聚集在一起('m')
          def chunks(df,folds):

              df_no_topup=df.loc[df['Top-up Month']==0]
              df_topup=df.loc[df['Top-up Month']==1]

              recs_no_topup=int(df.loc[df['Top-up Month']==0].shape[0]/folds)

              start_no_topup=0
              stop_no_topup=recs_no_topup
              list_df=[]

              for fold in range(0,folds):
                  fold_n=df_no_topup.iloc[start_no_topup:stop_no_topup,:]
                  start_no_topup=stop_no_topup
                  stop_no_topup=start_no_topup+recs_no_topup
                  df=pd.concat([fold_n,df_topup],axis=0)
                  list_df.append(df)
              return list_df
          上面的代碼片段將主要類數(shù)據(jù)劃分為多個(gè)折,每個(gè)折包含相同數(shù)量的數(shù)據(jù)點(diǎn)。然后大多數(shù)類數(shù)據(jù)的第一個(gè)折(在上面的代碼中被引用為fold_n)與次要類(df_topup)的所有數(shù)據(jù)連接在一起。
          以上在一個(gè)循環(huán)中發(fā)生,循環(huán)將繼續(xù),直到等于折“n”的大小??梢钥吹?,每次,大多數(shù)類數(shù)據(jù)的一個(gè)新塊與次要類的完整數(shù)據(jù)連接,從而允許模型在訓(xùn)練階段在比例相同的數(shù)據(jù)下進(jìn)行訓(xùn)練。
          Major class initially - 106677
          Fold size(n) - 5
          Major class data(k) per fold=106677/5 - 21335
          Minor class(all minor classes combined) - 21978
          Total data per fold(major+minor) - 43313

          下面代碼片段中的代碼也可以驗(yàn)證這一點(diǎn)。
          list_data=chunks(df_train_main,5)
          list_data_shape=[df.shape for df in list_data]
          print(list_data_shape)

          [(433136), (433136), (433136), (433136), (433136)]
          現(xiàn)在可以看到,一個(gè)具有嚴(yán)重類不平衡問(wèn)題的大數(shù)據(jù)集被劃分為5個(gè)小數(shù)據(jù)集
          不過(guò),以下是我從上述方法中認(rèn)識(shí)到的兩個(gè)問(wèn)題:
          • 添加到每個(gè)折中的大多數(shù)類數(shù)據(jù)的分布。因?yàn)槲覀冎皇菍⒄麄€(gè)大類數(shù)據(jù)劃分為折(n),折的分布與原有分布不同。
          • 不過(guò),在所有需要解決的問(wèn)題中,類(12-18個(gè)月)和主要類(“No Top-up Service”)之間仍然存在不平衡。但對(duì)于二類,這種方法效果更好。
          這些折的數(shù)據(jù)然后被傳遞到一個(gè)模型,以訓(xùn)練模型使用更少的不平衡數(shù)據(jù)。這是一種通用的方法,不僅可以應(yīng)用于結(jié)構(gòu)化數(shù)據(jù),還可以應(yīng)用于圖像等非結(jié)構(gòu)化數(shù)據(jù)。
          我個(gè)人在一個(gè)Kaggle競(jìng)賽中應(yīng)用了同樣的方法進(jìn)行圖像分類,并獲得了更好的準(zhǔn)確度。

          往期精彩回顧





          本站qq群851320808,加入微信群請(qǐng)掃碼:

          瀏覽 31
          點(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>
                  亚洲欧美婷婷五月色综合 | 中国又粗又大性视频 | 久久国产综合 | 豆花视频国产 | 最新人人操 |