在多数类样本中获得了smote + enn

时间:2019-04-01 19:20:37

标签: python machine-learning dataset balance imblearn

我有一个不平衡的数据集,当我尝试使用SMOTEENN平衡他时,多数阶层的人数减少了一半

我尝试使用所有提供的选项更改“ sampling_strategy”参数,但这无济于事

from imblearn.combine import SMOTEENN

sme = SMOTEENN()
X_res, y_res = sme.fit_resample(X_train, y_train)

print(f'Original train dataset shape: {Counter(y_train)}')
# Original train dataset shape: Counter({1: 2194, 0: 205})

print(f'Resampled train dataset shape: {Counter(y_res)}\n')
# Resampled train dataset shape: Counter({0: 2117, 1: 1226})

1 个答案:

答案 0 :(得分:0)

如果您查看文档SMOTEENN(https://imbalanced-learn.readthedocs.io/en/stable/generated/imblearn.combine.SMOTEENN.html#imblearn.combine.SMOTEENN ):

使用SMOTE和已编辑的最近邻居合并过采样和欠采样。

如果您希望每个班级获得偶数,可以尝试使用其他技术,例如over_sampling.SMOTE

例如:

from sklearn.datasets import make_classification
from imblearn.combine import SMOTEENN
from imblearn.over_sampling import SMOTE
from collections import Counter

X, y = make_classification(n_samples=5000, n_features=2, n_informative=2,
                           n_redundant=0, n_repeated=0, n_classes=2,
                           n_clusters_per_class=1,
                           weights=[0.06, 0.94],
                           class_sep=0.1, random_state=0)


sme = SMOTEENN()
X_res, y_res = sme.fit_resample(X, y)

print(f'Original train dataset shape: {Counter(y)}')
# Original train dataset shape: Counter({1: 4679, 0: 321})

print(f'Resampled train dataset shape: {Counter(y_res)}\n')
# Resampled train dataset shape: Counter({0: 3561, 1: 3246})

sme = SMOTE()
X_res, y_res = sme.fit_resample(X, y)

print(f'Original train dataset shape: {Counter(y)}')
# Original train dataset shape: Counter({1: 4679, 0: 321})

print(f'Resampled train dataset shape: {Counter(y_res)}\n')
# Resampled train dataset shape: Counter({0: 4679, 1: 4679})