使用Sklearn管道进行嵌套交叉验证

4

我正在尝试使用Sklearn库中的管道(pipeline)进行嵌套交叉验证,如下所示:

pipeline = imbpipeline(steps=[['smote', SMOTE(random_state=11)],                               
                              ['scaler', MinMaxScaler()],
                              ['classifier', LogisticRegression(random_state=11,
                                                                max_iter=1000)]])
cv_inner = KFold(n_splits=3, shuffle=True, random_state=1)
cv_outer = KFold(n_splits=10, shuffle=True, random_state=1)

param_grid = {'classifier__C':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]}
grid_search = GridSearchCV(estimator=pipeline,
                           param_grid=param_grid,
                           scoring='accuracy',
                           cv=cv_inner,
                           n_jobs=-1,
                           refit=True)


scores = cross_val_score(grid_search, 
                         train_set, 
                         train_labels, 
                         scoring='accuracy', 
                         cv=cv_outer, 
                         n_jobs=-1)
print('Accuracy: %.3f (%.3f)' % (np.mean(scores), np.std(scores)))

这段代码可以正常工作,但我无法找到如何提取上述过程中找到的最佳参数。

根据文档,我尝试了以下方法:

grid_search.best_params_

但是我得到了:
AttributeError: 'GridSearchCV' object has no attribute 'best_params_'

这个我真的不太理解。
你有什么想法吗?


https://scikit-learn.org/stable/auto_examples/model_selection/plot_grid_search_digits.html - Shivam Miglani
1
cross_val_score 不会生成最终拟合模型。 - Ben Reiniger
2
这个回答解决了你的问题吗?cross_val_score不适用于实际输入模型吗? - Ben Reiniger
1个回答

1

在获取最佳参数之前,您需要拟合数据。您应该添加一行代码:

grid_search.fit(X_train, y_train)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接