How to Create a Multi Classifier with Logistic Regression in Sklearn

03.09.2021

Intro

In a previous article, we learned how to build a binary logistic regression model to classify items into one of two categories. This represents the basic model. Now, we want to extend our logistic regression model to classify with multiple categories. In this article, we will learn how to build a multi classifier with logisitc regression in Sklearn.

Building a Multi Classifier

To extend logistic regression to classify with multiple categories, we fit a logisitc regression model as normally by creating an instance of the LogisticRegression class and passing our features and target to the fit method. However, when creating the LogisticRegression instance we pass the multi_class = "ovr" option. This will tell sklearn to use the ovr algorithm.

from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.preprocessing import StandardScaler

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

scaler = StandardScaler()
features_standardized = scaler.fit_transform(features)

logistic_regression = LogisticRegression(multi_class = "ovr")

model = logistic_regression.fit(features_standardized, target)
print(model.score())