.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/linear_model/plot_sgd_separating_hyperplane.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_sgd_separating_hyperplane.py: ========================================= SGD:最大间隔分离超平面 ========================================= 使用线性支持向量机分类器在一个可分的双类数据集中绘制最大间隔分离超平面,该分类器使用随机梯度下降(SGD)进行训练。 .. GENERATED FROM PYTHON SOURCE LINES 9-43 .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_sgd_separating_hyperplane_001.png :alt: plot sgd separating hyperplane :srcset: /auto_examples/linear_model/images/sphx_glr_plot_sgd_separating_hyperplane_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_blobs from sklearn.linear_model import SGDClassifier # 我们创建50个可分离点 X, Y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60) # 拟合模型 clf = SGDClassifier(loss="hinge", alpha=0.01, max_iter=200) clf.fit(X, Y) # 绘制直线、点和最近的向量到平面 xx = np.linspace(-1, 5, 10) yy = np.linspace(-1, 5, 10) X1, X2 = np.meshgrid(xx, yy) Z = np.empty(X1.shape) for (i, j), val in np.ndenumerate(X1): x1 = val x2 = X2[i, j] p = clf.decision_function([[x1, x2]]) Z[i, j] = p[0] levels = [-1.0, 0.0, 1.0] linestyles = ["dashed", "solid", "dashed"] colors = "k" plt.contour(X1, X2, Z, levels, colors=colors, linestyles=linestyles) plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired, edgecolor="black", s=20) plt.axis("tight") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.032 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_sgd_separating_hyperplane.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_sgd_separating_hyperplane.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sgd_separating_hyperplane.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sgd_separating_hyperplane.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sgd_separating_hyperplane.zip ` .. include:: plot_sgd_separating_hyperplane.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_