14

I try to apply this code :

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)

But I have this error :

AttributeError: 'GridSearchCV' object has no attribute 'cv_results_'
user513951
  • 12,445
  • 7
  • 65
  • 82
Cox Tox
  • 661
  • 3
  • 8
  • 22

4 Answers4

24

Update your scikit-learn, cv_results_ has been introduced in 0.18.1, earlier it was called grid_scores_ and had slightly different structure http://scikit-learn.org/0.17/modules/generated/sklearn.grid_search.GridSearchCV.html#sklearn.grid_search.GridSearchCV

lejlot
  • 64,777
  • 8
  • 131
  • 164
  • 1
    I don't understand.. When I check my version in Pycharm, I have version 0.18.1. I try to update it : 1) conda update conda and 2) conda install scikit-learn=0.18.1 But I have the same error – Cox Tox Jan 08 '17 at 11:03
  • Clearly this is not python that is being used by your IDE, but this is not python related issue but with your configuration. Lack of this field can be caused only by the old version of sklearn. – lejlot Jan 08 '17 at 16:08
6

from sklearn.model_selection import GridSearchCV

use this clf.cv_results_

Yatin Arora
  • 169
  • 2
  • 9
4

Solved ! Uninstall and install conda scikit learn in 0.18.1 How to upgrade scikit-learn package in anaconda.

When I import GridSearch :

from sklearn.model_selection import GridSearchCV
Community
  • 1
  • 1
Cox Tox
  • 661
  • 3
  • 8
  • 22
-7

First, you should update your sklearn, using:

pip install -U scikit-learn

After that, check if you are include the wrong module:

from sklearn.grid_search import GridSearchCV

Change to new path:

from sklearn.model_selection import GridSearchCV

(this is the right way)

jtlz2
  • 7,700
  • 9
  • 64
  • 114