GridSearchCV和ValueError:管道估计器的无效参数alpha。

3
我想在使用GridSearchCV时,结合StandardScaler来寻找最佳的参数,用于Ridge回归模型。但是出现了以下错误:

raise ValueError('Invalid parameter %s for estimator %s. ' ValueError: Invalid parameter alpha for estimator Pipeline(memory=None, steps=[('standardscaler', StandardScaler(copy=True, with_mean=True, with_std=True)),('ridge', Ridge(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=None, normalize=False, random_state=None, solver='auto', tol=0.001))], verbose=False). Check the list of available parameters with estimator.get_params().keys()

请问有谁能帮我解决这个问题?
import  numpy   as   np; import  pandas  as   pd; import  matplotlib.pyplot  as  plt;
import  plotly.express   as  px
from sklearn.linear_model import LinearRegression, Ridge,Lasso, ElasticNet
from sklearn.model_selection import cross_val_score,GridSearchCV, train_test_split
from sklearn.metrics import mean_squared_error
x_data=pd.read_excel('Input-15.xlsx')
y_data=pd.read_excel('Output-15.xlsx')
X_train, X_test,Y_train,Y_test=train_test_split(x_data,y_data,test_size=0.2,random_state=42)
###########    Ridge regression model     ########### 
rige=Ridge(normalize=True)
rige.fit(X_train,Y_train["Acc"]);rige.score(X_test,Y_test["Acc"])
score=format(rige.score(X_test,Y_test["Acc"]),'.4f')
print ('Ridge Reg Score with Normalization:',score)
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.preprocessing import StandardScaler
pip=make_pipeline(StandardScaler(),Ridge())
pip.fit(X_train,Y_train["Acc"])
score_pipe=format(pip.score(X_test,Y_test["Acc"]),'.4f')
print ('Standardized Ridge Score:',score_pipe)
######  performing the GridSearchCV /the value of α that maximizes the R2 ####
param_grid = {'alpha': np.logspace(-3,3,10)}
grid = GridSearchCV(estimator=pip, param_grid=param_grid, cv=2,return_train_score=True)
grid.fit(X_train,Y_train["Acc"])### barayeh har khoroji  ********
best_score = float(format(grid.best_score_, '.4f'))
print('Best CV score: {:.4f}'.format(grid.best_score_))
print('Best parameter :',grid.best_params_)
1个回答

3
简短回答是更改此行代码:
param_grid = {'alpha': np.logspace(-3,3,10)}

to:

param_grid = {'ridge__alpha': np.logspace(-3,3,10)}

一般来说,GridSearchCV 中可用于调整的所有参数都可以通过 estimator.get_params().keys() 获得。

在您的情况下,它是:

pip.get_params().keys()

dict_keys(['memory', 'steps', 'verbose', 'standardscaler', 'ridge',
 'standardscaler__copy', 'standardscaler__with_mean',
 'standardscaler__with_std', 'ridge__alpha', 'ridge__copy_X',
 'ridge__fit_intercept', 'ridge__max_iter', 'ridge__normalize',
 'ridge__random_state', 'ridge__solver', 'ridge__tol'])

作为一个附带说明,为什么不使用分号而是在新的一行上开始所有新语句呢?将相关代码块空格出来?这会使你的代码更易读。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接