How to Create a Binary Classifier with Logistic Regression in Sklearn

03.08.2021

Intro

Logisitic Regression, despite its name, is used as a classifier (puts items into cateogires). It uses a similar algorithm to linear regression, hence the name. The basic logistic regression is used as a binary classifier and can categorize items into two groups. In this article, we will learn how to build a Binary Classifier with Logisitic Regression in Sklearn.

Creating a Binary Classifier

To create a Binary Classifier, we use the LogisticRegression class from the linear_model package. We create an instance of this class, then pass our features (x values) and target (y value) to the fit method. Below, we use a subset of the iris dataset to classify into two groups.

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

iris = datasets.load_iris()
features = iris.data[:100,:]
target = iris.target[:100]

logistic_regression = LogisticRegression()

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