首页 > 解决方案 > 如何将 Pipeline 与 cross_val_score 结合起来解决多类问题?

问题描述

这是一个非常简单的问题,我认为我不能比直接问题添加更多内容:如何将管道与 cross_val_score 结合起来解决多类问题?

我在工作中处理一个多类问题(这就是为什么我不会共享任何数据,但人们可以将这个问题视为 iris 数据集的问题),我需要根据主题对一些文本进行相应的分类。这就是我正在做的事情:

pipe = Pipeline(
steps=[
    ("vect", CountVectorizer()),
    ("feature_selection", SelectKBest(chi2, k=10)),
    ("reg", RandomForestClassifier()),
])

pipe.fit(X_train, y_train)
y_pred = pipe.predict(X_test)

print(classification_report(y_test, y_pred))

但是,我有点担心过度拟合(即使我正在使用测试集进行评估),我想进行更严格的分析并添加交叉验证。问题是我不知道如何在管道中添加 cross_val_score,也不知道如何使用交叉验证评估多类问题。我看到了这个答案,所以我将它添加到我的脚本中:

cv = KFold(n_splits=5)
scores = cross_val_score(pipe, X_train, y_train, cv = cv)

问题是这会导致准确性,当我们讨论分类问题时,这并不是那么好。

有没有其他选择?是否可以进行交叉验证而不仅仅获得准确性?还是我应该坚持准确性,并且由于任何原因这都不是问题?

我知道这个问题太“宽泛”了,实际上不仅仅是关于交叉验证,我希望这不是问题。

提前致谢

标签: scikit-learnpipelinecross-validationmulticlass-classification

解决方案


几乎总是建议使用交叉验证来选择您的模型/超参数,然后使用独立的保持测试集来评估模型的性能。

好消息是,您可以在 scikit-learn 中做您想做的事!像这样的东西:

pipe = Pipeline(
  steps=[
    ("vect", CountVectorizer()),
    ("feature_selection", SelectKBest(chi2, k=10)),
    ("reg", RandomForestClassifier())])

# Parameters of pipelines can be set using ‘__’ separated parameter names:
param_grid = {
    'feature_selection__k': np.linspace(4, 16, 4), # Test different number of features in SelectKBest
    'reg__n_estimators': [10, 30, 50, 100, 200],  # n_estimators in RandomForestClassifier
    'reg__min_samples_leaf': [2, 5, 10, 50] # min_samples_leaf in RandomForestClassifier
}

# This defines the grid search with "Area Under the ROC Curve" as the scoring metric to use.
# More options here: https://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter
search = GridSearchCV(pipe, param_grid, scoring='roc_auc',)

search.fit(X_train, y_train)
print("Best parameter (CV score={:3f}:".format(search.best_score_))
print(search.best_params_)

有关更多详细信息,请参见此处。

如果您想为多类问题定义自己的评分指标,而不是使用 AUC 或其他一些默认评分指标,请参阅本页参数下的文档以scoring获取更多信息,但这就是我建议您不知道您使用什么指标的全部内容。重新尝试优化。


推荐阅读