.. 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.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.py: ========================================================= 逻辑函数 ========================================================= 图中显示了在这个合成数据集中,逻辑回归如何使用逻辑曲线将值分类为0或1,即类别一或类别二。 .. GENERATED FROM PYTHON SOURCE LINES 9-65 .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_logistic_001.png :alt: plot logistic :srcset: /auto_examples/linear_model/images/sphx_glr_plot_logistic_001.png :class: sphx-glr-single-img .. code-block:: Python # 代码来源:Gael Varoquaux # SPDX-License-Identifier: BSD-3-Clause import matplotlib.pyplot as plt import numpy as np from scipy.special import expit from sklearn.linear_model import LinearRegression, LogisticRegression # 生成一个玩具数据集,它只是一条带有一些高斯噪声的直线: xmin, xmax = -5, 5 n_samples = 100 np.random.seed(0) X = np.random.normal(size=n_samples) y = (X > 0).astype(float) X[X > 0] *= 4 X += 0.3 * np.random.normal(size=n_samples) X = X[:, np.newaxis] # 训练分类器 clf = LogisticRegression(C=1e5) clf.fit(X, y) # 并绘制结果 plt.figure(1, figsize=(4, 3)) plt.clf() plt.scatter(X.ravel(), y, label="example data", color="black", zorder=20) X_test = np.linspace(-5, 10, 300) loss = expit(X_test * clf.coef_ + clf.intercept_).ravel() plt.plot(X_test, loss, label="Logistic Regression Model", color="red", linewidth=3) ols = LinearRegression() ols.fit(X, y) plt.plot( X_test, ols.coef_ * X_test + ols.intercept_, label="Linear Regression Model", linewidth=1, ) plt.axhline(0.5, color=".5") plt.ylabel("y") plt.xlabel("X") plt.xticks(range(-5, 10)) plt.yticks([0, 0.5, 1]) plt.ylim(-0.25, 1.25) plt.xlim(-4, 10) plt.legend( loc="lower right", fontsize="small", ) plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.046 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_logistic.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.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_logistic.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_logistic.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_logistic.zip ` .. include:: plot_logistic.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_