Regularization path of L1- Logistic Regression

Train l1-penalized logistic regression models on a binary classification problem derived from the Iris dataset.

The models are ordered from strongest regularized to least regularized. The 4 coefficients of the models are collected and plotted as a “regularization path”: on the left-hand side of the figure (strong regularizers), all the coefficients are exactly 0. When regularization gets progressively looser, coefficients can get non-zero values one after the other.

Here we choose the liblinear solver because it can efficiently optimize for the Logistic Regression loss with a non-smooth, sparsity inducing l1 penalty.

Also note that we set a low value for the tolerance to make sure that the model has converged before collecting the coefficients.

We also use warm_start=True which means that the coefficients of the models are reused to initialize the next model fit to speed-up the computation of the full-path.

# Author: Alexandre Gramfort <>
# License: BSD 3 clause

Load data

from sklearn import datasets

iris = datasets.load_iris()
X =
y =

X = X[y != 2]
y = y[y != 2]

X /= X.max()  # Normalize X to speed-up convergence

Compute regularization path

import numpy as np

from sklearn import linear_model
from sklearn.svm import l1_min_c

cs = l1_min_c(X, y, loss="log") * np.logspace(0, 7, 16)

clf = linear_model.LogisticRegression(
coefs_ = []
for c in cs:
    clf.set_params(C=c), y)

coefs_ = np.array(coefs_)

Plot regularization path

import matplotlib.pyplot as plt

plt.plot(np.log10(cs), coefs_, marker="o")
ymin, ymax = plt.ylim()
plt.title("Logistic Regression Path")
Logistic Regression Path

Total running time of the script: ( 0 minutes 0.105 seconds)

Gallery generated by Sphinx-Gallery