我正在尝试使用GridSearchCV找到最佳参数。
这是我的代码:
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import MaxAbsScaler
clf = SVC(kernel="rbf")
n_components = [25,30,35,40]
Cs = [ 0.1, 1, 10,15,20,25,30,50,60,70,100,150,200]
gammas = [0.001, 0.01,0.05,0.08, 0.1,0.3,0.5, 0.7,1]
scalar = MaxAbsScaler()
pipe = Pipeline(steps=[('transformer', scalar),('pca', pca), ('estimator', clf)])
estimator = GridSearchCV(pipe,
dict(pca__n_components=n_components,
estimator__C=Cs,estimator__gamma=gammas),cv=5,n_jobs=-1)
scores = cross_val_score(estimator, X, y, cv = 5)
print('average accuracy : ',np.array(scores).mean(),np.std(np.array(scores)))
我想知道通过网格搜索选择的参数的最佳组合。当我尝试estimator.best_params_
时出现以下错误
AttributeError: 'GridSearchCV' object has no attribute 'best_params_'
尝试一下?
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import MaxAbsScaler
clf = SVC(kernel="rbf")
scalar = MaxAbsScaler()
paramgrid = {'n_components': [25,30,35,40],
'Cs': [ 0.1, 1, 10,15,20,25,30,50,60,70,100,150,200],
'gammas': [0.001, 0.01,0.05,0.08, 0.1,0.3,0.5, 0.7,1],
}
pipe = Pipeline(steps=[('transformer', scalar),
('pca', pca),
('estimator', clf)])
estimator = GridSearchCV(pipe,
paramgrid,
cv=5,
n_jobs=-1)
scores = cross_val_score(estimator, X, y, cv = 5)
print('average accuracy : ',np.array(scores).mean(),np.std(np.array(scores)))