AttributeError:'GridSearchCV'对象没有属性'cv_results_'

Cox*_*Tox 6 python machine-learning text-mining scikit-learn

我尝试应用此代码:

pipe = make_pipeline(TfidfVectorizer(min_df=5), LogisticRegression())
param_grid = {'logisticregression__C': [ 0.001, 0.01, 0.1, 1, 10, 100],
              "tfidfvectorizer__ngram_range": [(1, 1),(1, 2),(1, 3)]} 

grid = GridSearchCV(pipe, param_grid, cv=5)
grid.fit(text_train, Y_train)

scores = grid.cv_results_['mean_test_score'].reshape(-1, 3).T
# visualize heat map
heatmap = mglearn.tools.heatmap(
scores, xlabel="C", ylabel="ngram_range", cmap="viridis", fmt="%.3f",
xticklabels=param_grid['logisticregression__C'],
yticklabels=param_grid['tfidfvectorizer__ngram_range'])
plt.colorbar(heatmap)
Run Code Online (Sandbox Code Playgroud)

但我有这个错误:

AttributeError: 'GridSearchCV' object has no attribute 'cv_results_'
Run Code Online (Sandbox Code Playgroud)

lej*_*lot 14

更新你的scikit-learn,cv_results_已在0.18.1中引入,之前它被调用grid_scores_并且结构略有不同http://scikit-learn.org/0.17/modules/generated/sklearn.grid_search.GridSearchCV.html#sklearn.grid_search .GridSearchCV


Yat*_*ora 6

从 sklearn.model_selection 导入 GridSearchCV

用这个 clf.cv_results_

  • 在您发帖前 2 年,按照[最高投票答案](https:/ /stackoverflow.com/a/41527345/4685471)上面(您未能解决);请不要添加重复的答案。 (4认同)

Cox*_*Tox 4

解决了 !在 0.18.1如何在 anaconda 中升级 scikit-learn 包中卸载并安装conda scikit learn

当我导入 GridSearch 时:

from sklearn.model_selection import GridSearchCV
Run Code Online (Sandbox Code Playgroud)