<kbd id="5sdj3"></kbd>
<th id="5sdj3"></th>

  • <dd id="5sdj3"><form id="5sdj3"></form></dd>
    <td id="5sdj3"><form id="5sdj3"><big id="5sdj3"></big></form></td><del id="5sdj3"></del>

  • <dd id="5sdj3"></dd>
    <dfn id="5sdj3"></dfn>
  • <th id="5sdj3"></th>
    <tfoot id="5sdj3"><menuitem id="5sdj3"></menuitem></tfoot>

  • <td id="5sdj3"><form id="5sdj3"><menu id="5sdj3"></menu></form></td>
  • <kbd id="5sdj3"><form id="5sdj3"></form></kbd>

    機器學習中不平衡數(shù)據(jù)處理綜述

    共 7098字,需瀏覽 15分鐘

     ·

    2021-04-12 11:28

    簡介
    在現(xiàn)實世界中,我們收集的數(shù)據(jù)在大多數(shù)時候是嚴重不平衡的,所謂不平衡數(shù)據(jù)集就是訓練樣本不是平均分布在目標類中,例如,如果我們以個人貸款分類問題為例,就很容易得到“未批準”的數(shù)據(jù),而不是“已批準”的信息,結果,模型會更偏向具有大量訓練實例的類,這降低了模型的預測能力。
    在典型的二元分類問題中,它還會II型錯誤的增加。這一障礙不僅局限于機器學習模型,而且也主要存在于計算機視覺和自然語言處理領域。這些問題可以通過對每個區(qū)域分別使用不同的技術來有效地處理。
    注意:本文將簡要概述各種可用的數(shù)據(jù)增強方法,但不深入技術細節(jié),這里展示的所有圖像都來自Kaggle。

    目錄

    1. 機器學習——不平衡數(shù)據(jù)(上采樣和下采樣)
    2. 計算機視覺——不平衡數(shù)據(jù)(圖像數(shù)據(jù)增強)
    3. NLP——不平衡數(shù)據(jù)(Google交易和分類權重)

    1. 機器學習——不平衡數(shù)據(jù)

    處理類不平衡的兩種主要方法是上采樣/過采樣和下采樣/欠采樣。抽樣過程只應用于訓練集,對驗證和測試數(shù)據(jù)不作任何更改。python中的Imblearn庫可以方便地實現(xiàn)數(shù)據(jù)重采樣。
    上采樣是將合成生成的數(shù)據(jù)點(對應于少數(shù)類)注入數(shù)據(jù)集的過程,在這個過程之后,兩個標簽的計數(shù)幾乎是相同的,這種均衡過程防止了模型向多數(shù)類傾斜,而且目標類之間的交互(邊界)保持不變,同時,上采樣機制由于附加信息的存在而給系統(tǒng)帶來偏差。
    我們可以通過分析Google Analytics的貸款預測問題來解釋這些步驟。這里使用的訓練數(shù)據(jù)集可以在以下鏈接中找到。
    • https://datahack.analyticsvidhya.com/contest/practice-problem-loan-prediction-iii/
    下面提到的所有代碼都可以在GitHub存儲庫中找到。
    • https://github.com/NandhiniN85/Class-Imbalancing
    SMOTE(SyntheticMinorityOversamplingTechnique)——upsampling: 上采樣
    SMOTE基于knearestneighbors算法,綜合生成數(shù)據(jù)點,這些數(shù)據(jù)點位于已經存在的數(shù)量被超過的群體附近。應用此方法時,輸入記錄不應包含任何空值。
    #import imblearn library
    from imblearn.over_sampling import SMOTENC
    oversample = SMOTENC(categorical_features=[0,1,2,3,4,9,10], random_state = 100)
    X, y = oversample.fit_resample(X, y)
    datduplicate—upsampling: 在這種方法中,已存在的數(shù)據(jù)點被隨機抽取并重復。
    from sklearn.utils import resample
    maxcount = 332
    train_nonnull_resampled = train_nonnull[0:0]
    for grp in train_nonnull['Loan_Status'].unique():
        GrpDF = train_nonnull[train_nonnull['Loan_Status'] == grp]
        resampled = resample(GrpDF, replace=True, n_samples=int(maxcount), random_state=123)
        train_nonnull_resampled = train_nonnull_resampled.append(resampled)
    下采樣是一種減少訓練樣本落在多數(shù)類下的機制。因為它有助于平衡目標類別的計數(shù),但刪除收集到的數(shù)據(jù),我們往往會丟失很多有價值的信息。
    Tomek (T-Links): -
    T-Link基本上是來自不同類(最近的鄰居)的一對數(shù)據(jù)點,其目標是丟棄與多數(shù)類(數(shù)量較多的類)相對應的樣本,從而減少占主導地位的標簽的數(shù)量。這也增加了兩個標簽之間的邊界空間,從而提高了性能準確性。
    from imblearn.under_sampling import TomekLinks
    undersample = TomekLinks()
    X, y  = undersample.fit_resample(X, y)
    質心
    基于: 算法嘗試在多數(shù)類中尋找同質聚類,只保留質心,這將減少多數(shù)類的最大份額。它利用了KMeans集群中使用的邏輯,但是很多有用的信息會被浪費。

    2. 計算機視覺——不平衡數(shù)據(jù)

    對于非結構化數(shù)據(jù),如圖像和文本輸入,上述平衡技術將不會有效。在計算機視覺中,模型的輸入是圖像中像素的張量表示,所以只是隨機改變像素值(為了添加更多的輸入記錄)就可以完全改變圖片本身的意義。有一種概念叫做數(shù)據(jù)增強,即圖像經過大量轉換后仍然保持其意義不變。
    各種圖像轉換包括縮放、剪切、翻轉、填充、旋轉、亮度、對比度和飽和度變化,通過這樣做,僅使用單個圖像,就可以創(chuàng)建一個龐大的圖像數(shù)據(jù)集。
    讓我們看看Analyticsvidhya中發(fā)布的計算機視覺hackathon,使用的數(shù)據(jù)集可以在這里找到。
    • https://datahack.analyticsvidhya.com/contest/janatahack-computer-vision-hackathon/#ProblemStatement
    要求是將車輛分為緊急和非緊急兩類。為了便于說明,我們使用“0.jpg”圖像。
    from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
    datagen = ImageDataGenerator(
            rotation_range=40,
            width_shift_range=0.2,
            height_shift_range=0.2,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True,
            fill_mode='nearest')
    img = load_img('images/0.jpg')  
    x = img_to_array(img)  
    x = x.reshape((1,) + x.shape)  
    print(x.shape)
    # the .flow() command below generates batches of randomly transformed images
    # and saves the results to the `preview/` directory
    i = 0
    for batch in datagen.flow(x, batch_size=1,
                              save_to_dir='preview', save_prefix='vehichle', save_format='jpeg'):
        i += 1
        if i > 19:
            break  # otherwise the generator would loop indefinitely
    在GitHub存儲庫中可以找到整個代碼和一個預訓練過的模型。
    • https://github.com/NandhiniN85/Class-Imbalancing/blob/main/Computer%20Vision%20-%20Data%20Imbalanced.ipynb
    可以參考這個鏈接了解ImageDataGenerator的詳細用法。
    • https://keras.io/api/preprocessing/image/

    3. NLP——不平衡數(shù)據(jù)

    自然語言處理模型處理序列數(shù)據(jù),如文本、移動圖像,其中當前數(shù)據(jù)與之前的數(shù)據(jù)有時間依賴性。由于文本輸入屬于非結構化數(shù)據(jù),所以我們要以不同的方式處理這些場景。例如,以票據(jù)分類語言模型為例,其中IT票據(jù)必須根據(jù)輸入文本中出現(xiàn)的單詞順序分配給不同的組。
    谷歌翻譯(google trans python包): 這是擴展少數(shù)群體數(shù)量的有用技術之一。在這里,我們把給定的句子翻譯成“非英語”語言,然后再翻譯成“英語”,通過這種方式,可以維護輸入消息的重要細節(jié),但是單詞的順序/有時具有相似意義的新詞作為新記錄引入,從而可以對不足類進行計數(shù)。
    輸入文字- "warning for using windows disk space"
    數(shù)據(jù)補充文字-“Warning about using Windows storage space”
    即使上面的句子的意思是一樣的,它也引入了新單詞,從而通過擴大輸入樣本的數(shù)量來提高語言模型的學習能力。
    下面執(zhí)行的代碼可以在GitHub存儲庫中找到。
    • https://github.com/NandhiniN85/Class-Imbalancing/blob/main/NLP%20-%20Class%20Imbalanced.ipynb
    這個例子只包含了一個非英語代碼。在google trans 中使用的語言代碼的完整列表可以在以下鏈接找到:https://py-googletrans.readthedocs.io/en/latest/
    from googletrans import Translatortranslator = Translator()
    def German_translation(x):    print(x)        german_translation = translator.translate(x, dest='de')        return german_translation.text
    def English_translation(x):    print(x)    
        english_translation = translator.translate(x, dest='en')        return english_translation.text
    x = German_translation("warning for using windows disk space")
    English_translation(x)
    類權重: 該方法是在擬合模型過程中利用類權重參數(shù)。對于目標中的每個類別,都分配一個權重,與多數(shù)類相比,少數(shù)類將獲得更多的權重,因此,在反向傳播過程中,與少數(shù)類相關聯(lián)的損失值越大,模型會對輸出中的所有類一視同仁。
    import numpy as np
    from tensorflow import keras
    from sklearn.utils.class_weight import compute_class_weight
    y_integers = np.argmax(raw_y_train, axis=1)
    class_weights = compute_class_weight('balanced', np.unique(y_integers), y_integers)
    d_class_weights = dict(enumerate(class_weights))
    history = model.fit(input_final, raw_y_train, batch_size=32, class_weight = d_class_weights, epochs=8,callbacks=[checkpoint,reduceLoss],validation_data =(val_final, raw_y_val), verbose=1)
    這個選項在機器學習分類器中也可用,如我們給class_weight = ' balanced '的' SVM '。
    # fit the training dataset on the classifier
    SVM = svm.SVC(C=1.0, kernel='linear', degree=3, gamma='auto', class_weight='balanced', random_state=100)
    使用類權重的整個python代碼可以在GitHub鏈接中找到。
    • https://github.com/NandhiniN85/Class-Imbalancing/blob/main/English_raw200_model_execution.ipynb

    結論

    到目前為止,我們已經討論了不同領域處理不平衡數(shù)據(jù)的各種方法,如機器學習、計算機視覺和自然語言處理。盡管這些方法只是解決多數(shù)Vs少數(shù)目標群體問題的開始,還有其他先進的技術可以進一步探索。


    ?------------------------------------------------



    感謝對我們的支持! 

    瀏覽 133
    點贊
    評論
    收藏
    分享

    手機掃一掃分享

    分享
    舉報
    評論
    圖片
    表情
    推薦
    點贊
    評論
    收藏
    分享

    手機掃一掃分享

    分享
    舉報

    <kbd id="5sdj3"></kbd>
    <th id="5sdj3"></th>

  • <dd id="5sdj3"><form id="5sdj3"></form></dd>
    <td id="5sdj3"><form id="5sdj3"><big id="5sdj3"></big></form></td><del id="5sdj3"></del>

  • <dd id="5sdj3"></dd>
    <dfn id="5sdj3"></dfn>
  • <th id="5sdj3"></th>
    <tfoot id="5sdj3"><menuitem id="5sdj3"></menuitem></tfoot>

  • <td id="5sdj3"><form id="5sdj3"><menu id="5sdj3"></menu></form></td>
  • <kbd id="5sdj3"><form id="5sdj3"></form></kbd>
    国内精自视频在线观看 | 成人免费看片 98欧美 91av在线播放 | 婷婷五月天激情网 | 亚洲中文字幕组 | 97色色网|