首页 文章

scikit-learn中的不 balancer

提问于
浏览
26

我在我的Python程序中使用scikit-learn来执行一些机器学习操作 . 问题是我的数据集存在严重的不 balancer 问题 .

是否有人熟悉scikit-learn或python中的不 balancer 解决方案?在Java中有SMOTE机制 . python中有并行的东西吗?

5 回答

  • 5

    SMOTE不是scikit-learn内置的,但仍有在线实现 .
    例如Here .

  • 26

    这里有一个新的

    https://github.com/scikit-learn-contrib/imbalanced-learn

    它包含以下类别中的许多算法,包括SMOTE

    • 对大多数 class 进行抽样调查 .

    • 对少数民族的过度抽样 .

    • 结合过采样和欠采样 .

    • 创建整体 balancer 集 .

  • 14

    在Scikit中,学习了一些不 balancer 校正技术,这些技术根据您使用的学习算法而有所不同 .

    其中一些,如Svmlogistic regression,具有class_weight参数 . 如果使用此参数设置'auto'来实例化SVC,它将按比例对其频率的倒数加权每个类示例 .

    不幸的是,没有用于此目的的预处理器工具 .

  • 65

    我在这里发现了另一个库,它实现了欠采样和多种过采样技术,包括多个SMOTE实现,另一个使用SVM:

    https://github.com/fmfn/UnbalancedDataset

  • 0

    由于其他人已经列出了非常受欢迎的不 balancer 学习库的链接,我将概述如何正确使用它以及一些链接 .

    https://imbalanced-learn.org/en/stable/generated/imblearn.under_sampling.RandomUnderSampler.html

    https://imbalanced-learn.org/en/stable/generated/imblearn.over_sampling.RandomOverSampler.html

    https://imbalanced-learn.readthedocs.io/en/stable/generated/imblearn.over_sampling.SMOTE.html

    https://imbalanced-learn.readthedocs.io/en/stable/auto_examples/over-sampling/plot_comparison_over_sampling.html#sphx-glr-auto-examples-over-sampling-plot-comparison-over-sampling-py

    https://imbalanced-learn.org/en/stable/combine.html

    不 balancer 学习中的一些常见的过采样和欠采样技术是imblearn.over_sampling.RandomOverSampler,imblearn.under_sampling.RandomUnderSampler和imblearn.SMOTE . 对于这些库,有一个很好的参数,允许用户更改采样率 .

    例如,在SMOTE中,要更改输入字典的比率,并且所有值必须大于或等于最大类(因为SMOTE是一种过采样技术) . 我根据自己的经验发现SMOTE更适合模型性能的原因可能是因为使用RandomOverSampler复制行,这意味着模型可以开始记忆数据而不是推广到新数据 . SMOTE使用K-Nearest-Neighbors算法将“相似”数据点与采样下的数据点进行对比 .

    有时盲目使用SMOTE,将比率设置为默认值(甚至是类 balancer )并不是好习惯,因为模型可能会过度拟合一个或多个少数类,即使SMOTE使用最近邻居进行“相似”观察 . 以类似于调整ML模型的超参数的方式,您将调整SMOTE算法的超参数,例如比率和/或knn . 下面是如何正确使用SMOTE的工作示例 .

    NOTE: It is vital that you do not use SMOTE on the full data set. You MUST use SMOTE on the training set only (i.e. after you split), and then validate on the validation set and test sets to see if your SMOTE model out performed your other model(s). If you do not do this there will be data leakage and you will get a totally irrelevant model.

    from collections import Counter
    from imblearn.pipeline import Pipeline
    from imblearn.over_sampling import SMOTE
    import numpy as np
    from xgboost import XGBClassifier
    import warnings
    
    warnings.filterwarnings(action='ignore', category=DeprecationWarning)
    sm = SMOTE(random_state=0, n_jobs=8, ratio={'class1':100, 'class2':100, 'class3':80, 'class4':60, 'class5':90})
    X_resampled, y_resampled = sm.fit_sample(X_normalized, y)
    
    print('Original dataset shape:', Counter(y))
    print('Resampled dataset shape:', Counter(y_resampled))
    
    X_train_smote, X_test_smote, y_train_smote, y_test_smote = train_test_split(X_resampled, y_resampled)
    X_train_smote.shape, X_test_smote.shape, y_train_smote.shape, y_test_smote.shape, X_resampled.shape, y_resampled.shape
    
    smote_xgbc = XGBClassifier(n_jobs=8).fit(X_train_smote, y_train_smote)
    
    print('TRAIN')
    print(accuracy_score(smote_xgbc.predict(np.array(X_train_normalized)), y_train))
    print(f1_score(smote_xgbc.predict(np.array(X_train_normalized)), y_train))
    
    print('TEST')
    print(accuracy_score(smote_xgbc.predict(np.array(X_test_normalized)), y_test))
    print(f1_score(smote_xgbc.predict(np.array(X_test_normalized)), y_test))
    

相关问题