1. 导包
from sklearn.datasets import load_iris
from sklearn.datasets import load_breast_cancer
from collections import Counter
from imblearn.over_sampling import RandomOverSampler
from imblearn.over_sampling import SMOTE
from imblearn.over_sampling import SMOTEN
from imblearn.over_sampling import SMOTENC
from imblearn.over_sampling import BorderlineSMOTE
from imblearn.over_sampling import SVMSMOTE
from imblearn.over_sampling import KMeansSMOTE
from imblearn.over_sampling import ADASYN
from imblearn.under_sampling import RandomUnderSampler
from imblearn.under_sampling import ClusterCentroids
from imblearn.under_sampling import NearMiss
from imblearn.under_sampling import EditedNearestNeighbours
from imblearn.under_sampling import RepeatedEditedNearestNeighbours
from imblearn.under_sampling import AllKNN
from imblearn.under_sampling import CondensedNearestNeighbour
from imblearn.under_sampling import OneSidedSelection
from imblearn.under_sampling import NeighbourhoodCleaningRule
from imblearn.under_sampling import InstanceHardnessThreshold
from imblearn.combine import SMOTEENN
from imblearn.combine import SMOTETomek
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import BaggingClassifier
from imblearn.ensemble import BalancedBaggingClassifier
from imblearn.ensemble import RUSBoostClassifier
from imblearn.ensemble import EasyEnsembleClassifier
from imblearn.ensemble import BalancedRandomForestClassifier
2. 找数据
鸢尾花数据比较均衡,乳腺癌数据不均衡,可以用作样本数据
X, y = load_iris(return_X_y=True) # 鸢尾花数据集是三个类别0,1,2,而且类别均衡
print(X, y, X.shape, y.shape, len(X), len(y), sep="\n") # X.shape=(150, 4) y.shape=(150,) len(X)=150 len(y)=150
print(sorted(Counter(y).items())) # [(0, 50), (1, 50), (2, 50)]
X, y = load_breast_cancer(return_X_y=True) # 乳腺癌数据集是两个类别0,1,类别不均衡
print(X, y)
print(X.shape, y.shape) # (569, 30) (569,)
print(len(X), len(y)) # 569 569
print(sorted(Counter(y).items())) # [(0, 212), (1, 357)]
count0, count1 = 0, 0
for yy in y:
if yy == 0:
count0 += 1
elif yy == 1:
count1 += 1
print(count0, count1) # 212 357
3. 过采样
3.1 RandomOverSampler
随机过采样
ros = RandomOverSampler(random_state=42)
X_resampled, y_resampled = ros.fit_resample(X, y)
print(X_resampled, y_resampled, X_resampled.shape, y_resampled.shape, sep="\n") # (714, 30) (714,)
print(len(X_resampled), len(y_resampled)) # 714 714
print(sorted(Counter(y_resampled).items())) # [(0, 357), (1, 357)]
3.2 SMOTE
对于少数类样本a, 随机选择一个最近邻的样本b, 然后从a与b的连线上随机选取一个点c作为新的少数类样本
ros = SMOTE(random_state=42)
X_resampled, y_resampled = ros.fit_resample(X, y)
print(X_resampled, y_resampled, X_resampled.shape, y_resampled.shape, sep="\n") # # (714, 30) (714,)
print(len(X_resampled), len(y_resampled)) # 714 714
print(sorted(Counter(y_resampled).items())