GridSearchCV and ValueError: Invalid parameter alpha for estimator Pipeline

GridSearchCV and ValueError: Invalid parameter alpha for estimator Pipeline

我想将 StandardScalerGridSearchCV 一起使用,并找到 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_)

简短的回答是更改此行:

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

至:

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'])

作为旁注,为什么不使用分号而不是在新行上开始所有新语句? Space 块相关代码?这将使您的代码更具可读性。