2
$\begingroup$

I want to use GridSearchCV in Python for my Logistic Regression model, and i want it to check combinations for every possible setting, but i get an error when there is time for penalty: l1 and solver lbfgs because this solver type is not supported for this penalty. My question is: is there a way to say to the gridserach if some combination isn't posible just go to the next one.

parameters = { 
        'penalty'   :   ['l1','l2','elasticnet','none'],
        'solver'    :   ['lbfgs','liblinear','sag','saga']
             }  

clf_A = LogisticRegression(random_state=420)

f1_scorer = make_scorer(f1_score,pos_label=1)

grid_obj = GridSearchCV(clf_A,
                        scoring=f1_scorer,
                        param_grid=parameters,
                        cv=5)

grid_obj = grid_obj.fit(X_train,y_train)

clf_A = grid_obj.best_estimator_
$\endgroup$
5
  • $\begingroup$ AIUI, it already does that by default, with error_score=np.nan. Do you get an error? $\endgroup$
    – Ben Reiniger
    Commented Nov 14, 2020 at 1:53
  • $\begingroup$ yes it says that lbfgs does not support l1 penalty $\endgroup$
    – Pleban
    Commented Nov 15, 2020 at 15:51
  • $\begingroup$ As it should, but GridSearchCV should proceed anyway. I've just tried this with v0.22.2.post1, and the fitting finishes fine: it throws some FitFailedWarning warnings (not errors!) together with some ConvergenceWarnings, but cv_results_ is populated (with some NaNs when the fitting failed), and best_estimator_ is populated. $\endgroup$
    – Ben Reiniger
    Commented Nov 16, 2020 at 2:31
  • $\begingroup$ (and you can hide the warnings if you wish using the warnings package, see e.g. stackoverflow.com/a/14463362/10495893) $\endgroup$
    – Ben Reiniger
    Commented Nov 16, 2020 at 2:37
  • $\begingroup$ it is all working now, i don't know what happend $\endgroup$
    – Pleban
    Commented Nov 16, 2020 at 10:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.