一、不平衡數據是什麼
數據不平衡是指某些類別的樣本數量很少,在機器學習中,訓練數據集並不均衡,可能導致分類器過於關注數量眾多的類別,而忽視數量較少的類別。數據不平衡問題在自然界廣泛存在,比如醫療診斷、金融欺詐和電力設備故障預測等領域。
數據不平衡是指在類別分佈極不平等的情況下進行建模或者分析的問題。在實際中存在多種數據不平衡的情況,如:正反樣本、不同類別間樣本數目不平衡等。說明訓練集中不同類別之間的數量的差別很大,這種不均衡很容易導致分類器不能很好地區分數據,從而得到不理想的模型。
二、數據不平衡的解決方案
1.過採樣方法
過採樣是一種重放樣本數據的方法。通過對數據集中的較少類別的實例進行複製或生成新樣本,從而使得較少的分類的樣本數量增加,從而平衡數據集的類別分佈。 在實際中,過採樣可以使用的方法有:SMOTE(Synthetic Minority Over-sampling Technique)算法,ADASYN(Adaptive Synthetic Sampling), Borderline-SMOTE, ROSE等算法。
下面是SMOTE(Synthetic Minority Over-sampling Technique)算法的Python代碼示例:
import numpy as np from sklearn.datasets import make_classification from imblearn.over_sampling import SMOTE X, y = make_classification(n_samples=5000, n_features=10, weights=[0.01, 0.99], random_state=42) print('Original dataset:', X.shape, y.shape) smote = SMOTE(random_state=42) X_res, y_res = smote.fit_resample(X, y) print('Resampled dataset:', X_res.shape, y_res.shape)
2.欠採樣方法
欠採樣主要是通過對較多類別的實例進行刪除或者抽樣達到平衡數據集類別分佈的目的,而欠採樣最簡單的方法就是直接刪除多數類的樣本。欠採樣算法有:Random Under Sampling、OneSidedSelection、TomekLinks、NeighborhoodCleaningRule等。
下面是Random Under Sampling算法的Python代碼示例:
import numpy as np from sklearn.datasets import make_classification from imblearn.under_sampling import RandomUnderSampler X, y = make_classification(n_samples=5000, n_features=10, weights=[0.01, 0.99], random_state=42) print('Original dataset:', X.shape, y.shape) rus = RandomUnderSampler(random_state=42) X_res, y_res = rus.fit_resample(X, y) print('Resampled dataset:', X_res.shape, y_res.shape)
3.集成方法
集成方法通過使用多個分類器,然後進行綜合,提高分類器的整體效果的方式。集成方法的主要思想是在有偏樣本上訓練出多個分類器,然後將它們組合起來,從而使分類器的表現更為優秀。集成方法包括Bagging、Boosting、Stacking等。
下面是使用Random Forest方法的集成方法的Python代碼示例:
import numpy as np from sklearn.datasets import make_classification from sklearn.ensemble import RandomForestClassifier X, y = make_classification(n_samples=5000, n_features=10, weights=[0.01, 0.99], random_state=42) print('Original dataset:', X.shape, y.shape) clf = RandomForestClassifier(n_estimators=100, random_state=42) clf.fit(X, y) print('Original model score:', clf.score(X, y)) # Resample the data with SMOTE smote = SMOTE(random_state=42) X_res, y_res = smote.fit_resample(X, y) print('Resampled dataset:', X_res.shape, y_res.shape) clf_res = RandomForestClassifier(n_estimators=100, random_state=42) clf_res.fit(X_res, y_res) print('Resampled model score:', clf_res.score(X_res, y_res))
4.閾值移動(Threshold Moving)
閾值移動方法是一種簡單有效的方式, 只需將訓練好的分類器對測試數據的判定閾值設定為低於0.5,將其降低以便更多地檢測出少數類別的實例。 閾值移動允許我們以不同的閾值去評估分類器的結果,並允許我們選擇一個最合適的閾值來最大化分類器表現。
下面是使用Random Forest方法和閾值移動的Python代碼示例:
import numpy as np from sklearn.datasets import make_classification from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, f1_score X, y = make_classification(n_samples=5000, n_features=10, weights=[0.01, 0.99], random_state=42) print('Original dataset:', X.shape, y.shape) clf = RandomForestClassifier(n_estimators=100, random_state=42) clf.fit(X, y) print('Original model score:', clf.score(X, y)) # Predict using the original model y_pred = clf.predict(X) print('Original model accuracy:', accuracy_score(y, y_pred)) print('Original model f1 score:', f1_score(y, y_pred)) # Predict with a higher threshold y_pred_higher_threshold = (clf.predict_proba(X)[:, 1] > 0.6) print('Higher threshold model accuracy:', accuracy_score(y, y_pred_higher_threshold)) print('Higher threshold model f1 score:', f1_score(y, y_pred_higher_threshold))
三、總結
數據不平衡問題是機器學習模型訓練過程中不可避免的問題。目前解決數據不平衡問題的方法主要包括過採樣、欠採樣、集成學習和閾值移動等。每個方法都有自己的優缺點,根據實際情況選擇適合的方法可以提高模型效果和準確率。在實際中,對於不同的數據不平衡問題需要結合實際情況採用適當的方法進行解決。
原創文章,作者:WZHVQ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/363798.html