.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/linear_model/plot_logistic_path.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_linear_model_plot_logistic_path.py: ============================================== 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. .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 32-34 Load data --------- .. GENERATED FROM PYTHON SOURCE LINES 34-42 .. code-block:: Python from sklearn import datasets iris = datasets.load_iris() X = iris.data y = iris.target feature_names = iris.feature_names .. GENERATED FROM PYTHON SOURCE LINES 43-44 Here we remove the third class to make the problem a binary classification .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: Python X = X[y != 2] y = y[y != 2] .. GENERATED FROM PYTHON SOURCE LINES 48-50 Compute regularization path --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 50-60 .. code-block:: Python import numpy as np from sklearn.linear_model import LogisticRegression from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.svm import l1_min_c cs = l1_min_c(X, y, loss="log") * np.logspace(0, 1, 16) .. GENERATED FROM PYTHON SOURCE LINES 61-65 Create a pipeline with `StandardScaler` and `LogisticRegression`, to normalize the data before fitting a linear model, in order to speed-up convergence and make the coefficients comparable. Also, as a side effect, since the data is now centered around 0, we don't need to fit an intercept. .. GENERATED FROM PYTHON SOURCE LINES 65-84 .. code-block:: Python clf = make_pipeline( StandardScaler(), LogisticRegression( penalty="l1", solver="liblinear", tol=1e-6, max_iter=int(1e6), warm_start=True, fit_intercept=False, ), ) coefs_ = [] for c in cs: clf.set_params(logisticregression__C=c) clf.fit(X, y) coefs_.append(clf["logisticregression"].coef_.ravel().copy()) coefs_ = np.array(coefs_) .. GENERATED FROM PYTHON SOURCE LINES 85-87 Plot regularization path ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 87-104 .. code-block:: Python import matplotlib.pyplot as plt # Colorblind-friendly palette (IBM Color Blind Safe palette) colors = ["#648FFF", "#785EF0", "#DC267F", "#FE6100"] plt.figure(figsize=(10, 6)) for i in range(coefs_.shape[1]): plt.semilogx(cs, coefs_[:, i], marker="o", color=colors[i], label=feature_names[i]) ymin, ymax = plt.ylim() plt.xlabel("C") plt.ylabel("Coefficients") plt.title("Logistic Regression Path") plt.legend() plt.axis("tight") plt.show() .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_logistic_path_001.png :alt: Logistic Regression Path :srcset: /auto_examples/linear_model/images/sphx_glr_plot_logistic_path_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.164 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_logistic_path.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.7.X?urlpath=lab/tree/notebooks/auto_examples/linear_model/plot_logistic_path.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/linear_model/plot_logistic_path.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_logistic_path.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_logistic_path.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_logistic_path.zip ` .. include:: plot_logistic_path.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_