数据处理中的过采样、下采样、联合采样和集成采样

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())
  • 16
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值