.. 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 Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_linear_model_plot_logistic_path.py: ============================================== L1-正则化路径的逻辑回归 ============================================== 在从鸢尾花数据集中提取的二分类问题上训练带有L1惩罚项的逻辑回归模型。 这些模型按从最强正则化到最弱正则化的顺序排列。收集并绘制模型的4个系数作为“正则化路径”:在图的左侧(强正则化器),所有系数都为0。当正则化逐渐减弱时,系数会一个接一个地变为非零值。 在这里我们选择liblinear求解器,因为它可以有效地优化带有非平滑、稀疏诱导的L1惩罚项的逻辑回归损失。 还要注意,我们设置了一个较低的容差值,以确保在收集系数之前模型已经收敛。 我们还使用了warm_start=True,这意味着模型的系数被重用以初始化下一个模型拟合,从而加快全路径的计算。 .. GENERATED FROM PYTHON SOURCE LINES 17-21 .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 22-24 Load data --------- .. GENERATED FROM PYTHON SOURCE LINES 24-36 .. code-block:: Python from sklearn import datasets iris = datasets.load_iris() X = iris.data y = iris.target X = X[y != 2] y = y[y != 2] X /= X.max() # Normalize X to speed-up convergence .. GENERATED FROM PYTHON SOURCE LINES 37-39 计算正则化路径 --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 39-64 .. code-block:: Python 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, 10, 16) clf = linear_model.LogisticRegression( penalty="l1", solver="liblinear", tol=1e-6, max_iter=int(1e6), warm_start=True, intercept_scaling=10000.0, ) coefs_ = [] for c in cs: clf.set_params(C=c) clf.fit(X, y) coefs_.append(clf.coef_.ravel().copy()) coefs_ = np.array(coefs_) .. GENERATED FROM PYTHON SOURCE LINES 65-67 绘制正则化路径 ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 67-78 .. code-block:: Python import matplotlib.pyplot as plt plt.plot(np.log10(cs), coefs_, marker="o") ymin, ymax = plt.ylim() plt.xlabel("log(C)") plt.ylabel("Coefficients") plt.title("Logistic Regression Path") 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.052 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/main?urlpath=lab/tree/notebooks/auto_examples/linear_model/plot_logistic_path.ipynb :alt: Launch binder :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 `_