AttributeError 'GridSearchCV' object has no attribute 'cv_results_'

Viewed 103

I'm working through the load_boston() data for a scikit-learn tutorial. I'm running into this attribute error:

AttributeError 'GridSearchCV' object has no attribute 'cv_results_'

Does anyone know if there is a bug? I am using 1.1.1 version of scikit-learn.

import sklearn
from sklearn.datasets import load_boston
from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import StandardScaler   
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV    
from sklearn.linear_model import LinearRegression
import matplotlib.pylab as plt 
import pandas as pd 
from sklearn.model_selection import cross_val_score

print(sklearn.__version__)

X, y = load_boston(return_X_y=True)      

mod = KNeighborsRegressor().fit(X, y)     

pipe = Pipeline([
    ("scale", StandardScaler()),
    ("model", KNeighborsRegressor(n_neighbors=3))  
    ])
print(pipe.get_params())  

mod1 = GridSearchCV(estimator=pipe, param_grid={'model__n_neighbors': [1,2,3,4,5,6,7,8,9,10]},cv = 3)

pipe.fit(X, y)    
pred = pipe.predict(X)  
df = pd.DataFrame(mod1.cv_results_)   

plt.scatter(pred, y)  #pred instead of X
plt.title("Boston Housing Market")
plt.show()
1 Answers

Point is that cv_results_ is an attribute of the fitted GridSearchCV instance, while you've only fitted the pipeline (its base estimator). Therefore, you should fit mod1 to make it work.

import sklearn
from sklearn.datasets import load_boston
from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import StandardScaler   
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV    
from sklearn.linear_model import LinearRegression
import matplotlib.pylab as plt 
import pandas as pd 
from sklearn.model_selection import cross_val_score

X, y = load_boston(return_X_y=True)      

mod = KNeighborsRegressor().fit(X,y)     

pipe = Pipeline([
    ("scale", StandardScaler()),
    ("model", KNeighborsRegressor(n_neighbors=3))  
])
print(pipe.get_params())   
mod1 = GridSearchCV(estimator=pipe,param_grid={'model__n_neighbors': 
    [1,2,3,4,5,6,7,8,9,10]},cv = 3)
mod1.fit(X, y)

df = pd.DataFrame(mod1.cv_results_)

Be aware, though, that method .fit() of GridSearchCV does not return the fitted base estimator (despite fitting it, of course). Therefore, you won't be able to call pipe.predict(X) if you just substitute pipe.fit(X, y) via mod1.fit(X, y).

Related