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

Python GridSearchCV和ValueError:估计器管道的参数alpha无效,python,parameters,gridsearchcv,Python,Parameters,Gridsearchcv,我想将StandardScaler与GridSearchCV一起使用,并为Ridge回归模型找到最佳参数 但我得到了以下错误: raise VALUERROR('参数%s对于估计器%s无效' ValueError:估计器管道的参数alpha无效(内存=无, 步骤=[('standardscaler',standardscaler(copy=True,with_mean=True,with_std=True)),('ridge',ridge(alpha=1.0,copy=True,fit=True

我想将
StandardScaler
GridSearchCV
一起使用,并为
Ridge
回归模型找到最佳参数

但我得到了以下错误:

raise VALUERROR('参数%s对于估计器%s无效' ValueError:估计器管道的参数alpha无效(内存=无, 步骤=[('standardscaler',standardscaler(copy=True,with_mean=True,with_std=True)),('ridge',ridge(alpha=1.0,copy=True,fit=True,max=None,normalize=False,random=None, solver='auto',tol=0.001)),verbose=False)。使用估计器检查可用参数列表。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)}
致:

通常,所有可在
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'])
另一方面,为什么不使用分号在新行上启动所有新语句?相关代码的空格块?这将使代码更具可读性

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