GridSearchCV with Invalid parameter gamma for estimator LogisticRegression

gridsearchcv grid_scores_
gridsearchcv example
gridsearchcv explained
gridsearchcv logistic regression
gridsearchcv random forest
invalid parameter c for estimator onevsrestclassifier
invalid parameter colsample_bytree

I need to perform a grid search on the parameters listed below for a Logistic Regression classifier, using recall for scoring and cross-validation three times.

The data is in a csv file (11,1 MB), this link for download is: https://drive.google.com/file/d/1cQFp7HteaaL37CefsbMNuHqPzkINCVzs/view?usp=sharing

I have grid_values = {'gamma':[0.01, 0.1, 1, 10, 100]} I need to apply penalty L1 e L2 in a Logistic Regression

I couldn't verify if the scores will run because I have the following error: Invalid parameter gamma for estimator LogisticRegression. Check the list of available parameters with estimator.get_params().keys().

This is my code:

from sklearn.model_selection import train_test_split

df = pd.read_csv('fraud_data.csv')

X = df.iloc[:,:-1]
y = df.iloc[:,-1]

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)




def LogisticR_penalty():    
    from sklearn.model_selection import GridSearchCV
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import cross_val_score

    grid_values = {'gamma':[0.01, 0.1, 1, 10, 100]}


    #train de model with many parameters for "C" and penalty='l1'
    lr_l1 = LogisticRegression(penalty='l1')
    grid_lr_l1 = GridSearchCV(lr_l1, param_grid = grid_values, cv=3, scoring = 'recall')
    grid_lr_l1.fit(X_train, y_train)
    y_decision_fn_scores_recall = grid_lr_l1.decision_function(X_test)


    lr_l2 = LogisticRegression(penalty='l2')
    grid_lr_l2 = GridSearchCV(lr_l2, param_grid = grid_values, cv=3 , scoring = 'recall')
    grid_lr_l2.fit(X_train, y_train)
    y_decision_fn_scores_recall = grid_lr_l2.decision_function(X_test)



    #The precision, recall, and accuracy scores for every combination 
    #of the parameters in param_grid are stored in cv_results_
    results = pd.DataFrame()

    results['l1_results'] = pd.DataFrame(grid_lr_l1.cv_results_)
    results['l1_results'] = results['l2_results'].sort_values(by='mean_test_precision_score', ascending=False)

    results['l2_results'] = pd.DataFrame(grid_lr_l2.cv_results_)
    results['l2_results'] = results['l2_results'].sort_values(by='mean_test_precision_score', ascending=False)


    return results
LogisticR_penalty()

I expected from .cv_results_, the average test scores of each parameter combination that I should be available here: mean_test_precision_score but not sure

The output is: ValueError: Invalid parameter gamma for estimator LogisticRegression. Check the list of available parameters with estimator.get_params().keys().

The error message contains the answer for your question. You can use the function estimator.get_params().keys() to see all available parameters for you estimator:

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()

print(lr.get_params().keys())

Output:

dict_keys(['C', 'class_weight', 'dual', 'fit_intercept', 'intercept_scaling', 'l1_ratio', 'max_iter', 'multi_class', 'n_jobs', 'penalty', 'random_state', 'solver', 'tol', 'verbose', 'warm_start'])

Invalid parameter for sklearn estimator pipeline, (1,3)]} grid = GridSearchCV(pipe, param_grid, cv=5) grid.fit(X_train, There should be two underscores between estimator name and it's parameters in a Pipeline logisticregression__C . Do the same for tfidfvectorizer param_grid = { '​p2__svc__kernel': ['rbf', 'poly'], 'p2__svc__gamma': ['scale', 'auto'], }. I hope you have resolved this issue. If you use estimator.get_params()(in your case estimator is LogisticRegression), you can see that possible are: {'bootstrap

From scikit-learn's documentation, the LogisticRegression has no parameter gamma, but a parameter C for the regularization weight.

If you change grid_values = {'gamma':[0.01, 0.1, 1, 10, 100]} for grid_values = {'C':[0.01, 0.1, 1, 10, 100]} your code should work.

C parameter error in pipeline, /use-sklearns-gridsearchcv-with-a-pipeline-preprocessing-just-once/55401454​#55401454 Then, when specifying parameters you need to include this 'clf_' name the error message tells you Invalid parameter C for estimator Pipeline LogisticRegression does take parameters penalty , C and solver . Parameter estimation using grid search with cross-validation¶. This examples shows how a classifier is optimized by cross-validation, which is done using the sklearn.model_selection.GridSearchCV object on a development set that comprises only half of the available labeled data.

My code contained some errors the main error was using param_grid incorrectly. I had to apply L1 and L2 penalties with gamma 0.01, 0.1, 1, 10, 100. The right way to do this is:

grid_values ​​= {'penalty': ['l1', 'l2'], 'C': [0.01, 0.1, 1, 10, 100]}

Then it was necessary to correct the way I was training my logistic regression and to correct the way I retrieved the scores in cv_results_ and averaged those scores. Follow my code:

from sklearn.model_selection import train_test_split

df = pd.read_csv('fraud_data.csv')

X = df.iloc[:,:-1]
y = df.iloc[:,-1]

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

def LogisticR_penalty():    
    from sklearn.model_selection import GridSearchCV
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import cross_val_score

    grid_values = {'penalty': ['l1', 'l2'], 'C': [0.01, 0.1, 1, 10, 100]}


    #train de model with many parameters for "C" and penalty='l1'

    lr = LogisticRegression()
    # We use GridSearchCV to find the value of the range that optimizes a given measurement metric.
    grid_lr_recall = GridSearchCV(lr, param_grid = grid_values, cv=3, scoring = 'recall')
    grid_lr_recall.fit(X_train, y_train)
    y_decision_fn_scores_recall = grid_lr_recall.decision_function(X_test)

    ##The precision, recall, and accuracy scores for every combination 
    #of the parameters in param_grid are stored in cv_results_
    CVresults = []
    CVresults = pd.DataFrame(grid_lr_recall.cv_results_)

    #test scores and mean of them
    split_test_scores = np.vstack((CVresults['split0_test_score'], CVresults['split1_test_score'], CVresults['split2_test_score']))
    mean_scores = split_test_scores.mean(axis=0).reshape(5, 2)

    return mean_scores
LogisticR_penalty()

ValueError: Invalid parameter c for estimator. : learnpython, I searched a bit online but couldn't figure out the problem. Code: # Applying Grid Search. from sklearn.model_selection import GridSearchCV. parameters = [{'c': [1​,  I want to score different classifiers with different parameters. For speedup on LogisticRegression I use LogisticRegressionCV (which at least 2x faster) and plan use GridSearchCV for others. But problem while it give me equal C parameters, but not the AUC ROC scoring. I'll try fix many parameters like scorer, random_state, solver, max_iter, tol

Parameter estimation using grid search with cross-validation , GridSearchCV object on a development set that comprises only half of the available The performance of the selected hyper-parameters and trained model is then estimator performance and Tuning the hyper-parameters of an estimator. parameters set found on development set: {'C': 10, 'gamma': 0.001, '​kernel': 'rbf'}  Get parameters for this estimator. Parameters deep bool, default=True. If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns params mapping of string to any. Parameter names mapped to their values. inverse_transform (self, Xt) [source] ¶ Call inverse_transform on the estimator with the

NameError when using GridSearchCV on Problem Transformed , ValueError: Invalid parameter kernel for estimator degree=3, gamma='auto', kernel='rbf', max_iter=-1, probability=False, random_state=None, fixed name error for when a classifier received an invalid parameter #47. I am implementing an example from the O'Reilly book "Introduction to Machine Learning with Python", using Python 2.7 and sklearn 0.16. The code I am using: pipe = make_pipeline(TfidfVectorizer(),

Pipeline in Pipeline seems to not work well with setting of , Description Using Pipeline in Pipeline in GridSearchCV fails sometimes at __​name__)) ValueError: Invalid parameter alpha for estimator  Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object. Parameters **params dict. Estimator parameters. Returns self object. Estimator instance.

Comments
  • Thank you, it helped me clarify things. I really didn't know you could know all the parameters available to your estimator.
  • In addition to changing from gamma to C, I also had to include the penalties I needed to work : grid_values ​​= {'penalty': ['l1', 'l2'], 'C': [0.01, 0.1, 1, 10, 100]}