How to Handle Imbalanced Classes with a Logisitic Regression Model in Sklearn

03.12.2021

Intro

When working with data sets, you may come accross data with imbalanced classes, mean that you have more sample of one classe than another. For example, you maybe have more male samples then female. In such cases, it is wise to use imbalance options. In this article, we will learn how to handle imbalanced classes with Logistic Regression in Sklearn.

Logistic Regression with Imbalanced Classes

To handle imbalanced classes with logistic regression, we use the class_weight option and set the balanced value. This will tell sklearn to use stratified sampling techniques and other alogrithms to handle imabalanced classes and fit a better model.

#
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(class_weight="balanced")

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