Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Training logistic regression using scikit learn for multi-class classification

As per the scikit multiclass classification Logistic regression can be used for multi-class classification by setting multi_class=multinomial in the the constructor. But doing this gives error:

Code:

text_clf = Pipeline([('vect', TfidfVectorizer()),('clf', LogisticRegression(multi_class = 'multinomial')),])
text_clf = text_clf.fit(X_train, Y_train)

Error:

ValueError: Solver liblinear does not support a multinomial backend.

Can you tell me what is wrong here?

Note: Keeping multi_class to blank i.e. "ovr" is working fine but it fits a binary model for each classifier and I want to try mutlinomial feature also.

like image 962
Abhishek Avatar asked Aug 18 '15 14:08

Abhishek


People also ask

How do you train logistic regression for multi-class classification?

One popular approach for adapting logistic regression to multi-class classification problems is to split the multi-class classification problem into multiple binary classification problems and fit a standard logistic regression model on each subproblem.

Can we apply logistic regression on a 3 class classification problem?

Yes, we can apply logistic regression on 3 classification problem, We can use One Vs all method for 3 class classification in logistic regression.

What is the prediction function in logistic regression for multi-class classification?

Logistic regression uses a sigmoid function to predict the output.

What is logistic regression in scikit?

Scikit Learn - Logistic Regression. Advertisements. Previous Page. Next Page. Logistic regression, despite its name, is a classification algorithm rather than regression algorithm. Based on a given set of independent variables, it is used to estimate discrete value (0 or 1, yes/no, true/false). It is also called logit or MaxEnt Classifier.

When to use logistic regression for multiclass classification?

Logistic regression is a very popular machine learning technique. We use logistic regression when the dependent variable is categorical. This article will focus on the implementation of logistic regression for multiclass classification problems. I am assuming that you already know how to implement a binary classification with Logistic Regression.

What is a logit classifier?

Logistic Regression (aka logit, MaxEnt) classifier. In the multiclass case, the training algorithm uses the one-vs-rest (OvR) scheme if the ‘multi_class’ option is set to ‘ovr’, and uses the cross-entropy loss if the ‘multi_class’ option is set to ‘multinomial’.

What are the types of multiclass classification models?

There are several Multiclass Classification Models like Decision Tree Classifier, KNN Classifier, Naive Bayes Classifier, SVM (Support Vector Machine) and Logistic Regression. We will take one of such a multiclass classification dataset named Iris. We will use several models on it.


2 Answers

It looks like you are not providing solver & by default solver is set to 'liblinear' that does not support multi class. As per sklearn version 0.20.1, multiclass is being supported by ‘newton-cg’, ‘lbfgs’, ‘sag’, ‘saga’ not by ‘liblinear’ so change your instance creation for LogisticRegression as per following code

logReg = LogisticRegression(multi_class='multinomial', solver='newton-cg')

solver must be anything from ‘newton-cg’, ‘lbfgs’, ‘sag’, ‘saga’ but can not be left

like image 107
Abhishek Jain Avatar answered Sep 20 '22 14:09

Abhishek Jain


From the doc:

Currently the ‘multinomial’ option is supported only by the ‘lbfgs’ and ‘newton-cg’ solvers.

So you need to explicitly set solver to 'newton-cg' or 'lbfgs', since the default solver is 'liblinear'.

like image 41
yangjie Avatar answered Sep 20 '22 14:09

yangjie