python-3.x - 使用 Imblearn 管道和 GridSearchCV 进行交叉验证

标签 python-3.x scikit-learn pipeline imblearn

我正在尝试使用 Pipelineimblearn 中的 GridSearchCV 类来获得对不平衡数据集进行分类的最佳参数。根据提到的答案 here ,我想省略验证集的重采样,只重采样训练集,imblearnPipeline 似乎正在这样做。但是,在实现已接受的解决方案时出现错误。请让我知道我做错了什么。下面是我的实现:

def imb_pipeline(clf, X, y, params):

    model = Pipeline([
        ('sampling', SMOTE()),
        ('classification', clf)
    ])

    score={'AUC':'roc_auc', 
           'RECALL':'recall',
           'PRECISION':'precision',
           'F1':'f1'}

    gcv = GridSearchCV(estimator=model, param_grid=params, cv=5, scoring=score, n_jobs=12, refit='F1',
                       return_train_score=True)
    gcv.fit(X, y)

    return gcv

for param, classifier in zip(params, classifiers):
    print("Working on {}...".format(classifier[0]))
    clf = imb_pipeline(classifier[1], X_scaled, y, param) 
    print("Best parameter for {} is {}".format(classifier[0], clf.best_params_))
    print("Best `F1` for {} is {}".format(classifier[0], clf.best_score_))
    print('-'*50)
    print('\n')

参数:
[{'penalty': ('l1', 'l2'), 'C': (0.01, 0.1, 1.0, 10)},
 {'n_neighbors': (10, 15, 25)},
 {'n_estimators': (80, 100, 150, 200), 'min_samples_split': (5, 7, 10, 20)}]

分类器:
[('Logistic Regression',
  LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                     intercept_scaling=1, l1_ratio=None, max_iter=100,
                     multi_class='warn', n_jobs=None, penalty='l2',
                     random_state=None, solver='warn', tol=0.0001, verbose=0,
                     warm_start=False)),
 ('KNearestNeighbors',
  KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
                       metric_params=None, n_jobs=None, n_neighbors=5, p=2,
                       weights='uniform')),
 ('Gradient Boosting Classifier',
  GradientBoostingClassifier(criterion='friedman_mse', init=None,
                             learning_rate=0.1, loss='deviance', max_depth=3,
                             max_features=None, max_leaf_nodes=None,
                             min_impurity_decrease=0.0, min_impurity_split=None,
                             min_samples_leaf=1, min_samples_split=2,
                             min_weight_fraction_leaf=0.0, n_estimators=100,
                             n_iter_no_change=None, presort='auto',
                             random_state=None, subsample=1.0, tol=0.0001,
                             validation_fraction=0.1, verbose=0,
                             warm_start=False))]

错误:
ValueError: Invalid parameter C for estimator Pipeline(memory=None,
         steps=[('sampling',
                 SMOTE(k_neighbors=5, kind='deprecated',
                       m_neighbors='deprecated', n_jobs=1,
                       out_step='deprecated', random_state=None, ratio=None,
                       sampling_strategy='auto', svm_estimator='deprecated')),
                ('classification',
                 LogisticRegression(C=1.0, class_weight=None, dual=False,
                                    fit_intercept=True, intercept_scaling=1,
                                    l1_ratio=None, max_iter=100,
                                    multi_class='warn', n_jobs=None,
                                    penalty='l2', random_state=None,
                                    solver='warn', tol=0.0001, verbose=0,
                                    warm_start=False))],
         verbose=False). Check the list of available parameters with `estimator.get_params().keys()`. """

最佳答案

请检查此示例如何在管道中使用参数:
- https://scikit-learn.org/stable/auto_examples/compose/plot_compare_reduction.html#sphx-glr-auto-examples-compose-plot-compare-reduction-py

每当使用管道时,您都需要以某种方式发送参数,以便管道可以了解哪个参数用于列表中的哪个步骤。为此,它使用您在管道初始化期间提供的名称。

在您的代码中,例如:

model = Pipeline([
        ('sampling', SMOTE()),
        ('classification', clf)
    ])

要将参数 p1 传递给 SMOTE,您可以使用 sampling__p1作为参数,而不是 p1 .

您使用过 "classification"作为您的名字 clf所以将它附加到应该转到 clf 的参数中.

尝试:
[{'classification__penalty': ('l1', 'l2'), 'classification__C': (0.01, 0.1, 1.0, 10)},
 {'classification__n_neighbors': (10, 15, 25)},
 {'classification__n_estimators': (80, 100, 150, 200), 'min_samples_split': (5, 7, 10, 20)}]

确保名称和参数之间有两个下划线。

关于python-3.x - 使用 Imblearn 管道和 GridSearchCV 进行交叉验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815016/

相关文章:

python - 列表理解 - 默认第一个值

bash - 如何使用 Azure 文件共享中的多个线程将数据复制到 Azure Data Lake 存储?

python - 为什么 Skorch 在每个时期都显示 NAN?

linux - 如何通过 ZIP 管道传输并在存档中使用可用的文件名

powershell - 停止 PowerShell 管道,确保调用 end

c# - 是否可以在ElasticSearch更新中执行Ingest PipeLine?

python - 为什么 scipy 中的 χ2 检验返回较小的检验统计量?

python - 如何在 Python 3 中迭代模块列表并调用它们的方法

python - scikit-learn 的 extract_patches 函数背后的理论/算法是什么?

python - 如何使用 matplotlib 可视化模型性能和 alpha 的依赖性?