Search code examples
pythonparametersgridsearchcv

GridSearchCV and ValueError: Invalid parameter alpha for estimator Pipeline


I want to use StandardScaler with GridSearchCV and find the best parameter for Ridge regression model.

But I got the following error:

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()

Can anyone help me?

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_)

Solution

  • The short answer is change this line:

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

    to:

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

    In general all the params available for tuning in GridSearchCV are available through estimator.get_params().keys()

    In your case its:

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

    As a side note, why not instead of using semicolons start all new statements on a new line? Space blocks of related code? It will make your code more readable.