我尝试应用此代码:
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)
但是我有这个错误:
AttributeError: 'GridSearchCV' object has no attribute 'cv_results_'
更新你的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
来自 sklearn.model_selection import GridSearchCV
使用此clf.cv_results_
已解决!卸载并安装 conda scikit learn 在 0.18.1 如何在 anaconda 中升级 scikit-learn 包。
当我导入网格搜索时:
from sklearn.model_selection import GridSearchCV
首先,你应该使用以下命令更新你的 sklearn:
pip install -U scikit-learn
之后,检查您是否包含错误的模块:
from sklearn.grid_search import GridSearchCV
更改为新路径:
from sklearn.model_selection import GridSearchCV
(这是正确的方法)