Python Multiclass Classifier with Logistic Regression using Sklearn

2020-12-11

Intro

Logistic Regression by default classifies data into two categories. With some modifications though, we can change the algorithm to predict multiple classifications. The two alterations are one-vs-rest (OVR) and multinomial logistic regression (MLR). In this article we will see how to make these alterations in skelearn.

MultiClassifier

To alter logistic regression for multi class, we can pass the multi_class parameters. The first example is one-vs-rest.

from sklearn.linear_model import LogisticRegression
from sklearn import datasets

# Get data
iris = datasets.load_iris()
features = iris.data
target = iris.target

# Create one-vs-rest model
logistic_regression = LogisticRegression(multi_class="ovr")

# Train
model = logistic_regression.fit(features, target)

# Check score
model.score()

In a similar way, we can also train MLR.

from sklearn.linear_model import LogisticRegression
from sklearn import datasets

# Get data
iris = datasets.load_iris()
features = iris.data
target = iris.target

# Create MLR model
logistic_regression = LogisticRegression(multi_class="multinomial")

# Train
model = logistic_regression.fit(features, target)

# Check score
model.score()
GoTea - KoalaTea