我正在尝试使用Sklearn库中的管道应用嵌套交叉验证,如下所示:
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_'
我真的不能理解。
有什么想法吗?
在获得最佳参数之前,需要对数据进行拟合。你应该加一行:
grid_search.fit(X_train, y_train)