python - imblearn smote+enn 采样了多数类

标签 python machine-learning dataset 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})

最佳答案

如果您查看文档 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})

关于python - imblearn smote+enn 采样了多数类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55462124/

相关文章:

python - 通过 os.system 打开文件时出现意外的文件结尾错误

python - 如何使用命令行参数更改 Tox 命令

Python 未执行的函数列表

c# - SqlDataAdapter关闭连接方法

r - 将一行数据分解为多行

python - 在排序的 Pandas 数据帧上有效搜索范围

python - 如何向矢量化数据集添加特征?

python - 使用opencv python从视频检测图像时如何忽略背景?

python - 使用 scikit-learn 进行子采样 + 分类

r - 如何部署使用本地数据的 Shiny 应用程序