我想将StandardScaler
与GridSearchCV
一起使用,并找到Ridge
回归模型的最佳参数。
但是我遇到了以下错误:
提高ValueError('估算器%s的参数%s无效。' ValueError:估算程序管道的无效参数alpha(内存=无, 步骤= [('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))],详细= False)。使用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_)
答案 0 :(得分:0)
简短的答案是更改此行:
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'])
在旁注中,为什么不使用分号而不是在新行上开始所有新语句?相关代码的空格?它将使您的代码更具可读性。