Search code examples
pythonmachine-learningscikit-learnlogistic-regression

problem in the Logistic Regression for solver


I am using the Logistic Regression for modeling. But while trying the multiple solvers when i applied the solver = "multinomial" i got this


import sklearn as skl

skl.__version__
'0.21.2'

X_train, X_test, y_train, y_test = train_test_split(multiclass_logistic_data, labels, test_size = 0.2, random_state = 1)

cv_reg = linear_model.LogisticRegressionCV(solver='multinomial', max_iter=1000)
cv_reg.fit(X_train, y_train)

ValueError                                Traceback (most recent call last)
<ipython-input-54-6d16d00d0653> in <module>
----> 1 cv_reg.fit(X_train, y_train)

E:\Anaconda_Install\lib\site-packages\sklearn\linear_model\logistic.py in fit(self, X, y, sample_weight)
   1970         self : object
   1971         """
-> 1972         solver = _check_solver(self.solver, self.penalty, self.dual)
   1973 
   1974         if not isinstance(self.max_iter, numbers.Number) or self.max_iter < 0:

E:\Anaconda_Install\lib\site-packages\sklearn\linear_model\logistic.py in _check_solver(solver, penalty, dual)
    435     if solver not in all_solvers:
    436         raise ValueError("Logistic Regression supports only solvers in %s, got"
--> 437                          " %s." % (all_solvers, solver))
    438 
    439     all_penalties = ['l1', 'l2', 'elasticnet', 'none']

ValueError: Logistic Regression supports only solvers in ['liblinear', 'newton-cg', 'lbfgs', 'sag', 'saga'], got multinomial.

Solution

  • Replace solver='multinomial' with multi_class='multinomial'. There is no 'multinomial' solver.

    In the comments you mention,

    i read the reference of the solver in the course of educative inc

    Don't read reference/docs anywhere else, use scikit-learn's website, https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegressionCV.html

    Please read the docs.