.. 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_weighted_samples.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_weighted_samples.py: ===================== 随机梯度下降:加权样本 ===================== 绘制加权数据集的决策函数,其中点的大小与其权重成正比。 .. GENERATED FROM PYTHON SOURCE LINES 9-60 .. image-sg:: /auto_examples/linear_model/images/sphx_glr_plot_sgd_weighted_samples_001.png :alt: plot sgd weighted samples :srcset: /auto_examples/linear_model/images/sphx_glr_plot_sgd_weighted_samples_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn import linear_model # 我们创建了20个点 np.random.seed(0) X = np.r_[np.random.randn(10, 2) + [1, 1], np.random.randn(10, 2)] y = [1] * 10 + [-1] * 10 sample_weight = 100 * np.abs(np.random.randn(20)) # 并且给最后10个样本分配更大的权重 sample_weight[:10] *= 10 # 绘制加权数据点 xx, yy = np.meshgrid(np.linspace(-4, 5, 500), np.linspace(-4, 5, 500)) fig, ax = plt.subplots() ax.scatter( X[:, 0], X[:, 1], c=y, s=sample_weight, alpha=0.9, cmap=plt.cm.bone, edgecolor="black", ) # 拟合无权重模型 clf = linear_model.SGDClassifier(alpha=0.01, max_iter=100) clf.fit(X, y) Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) no_weights = ax.contour(xx, yy, Z, levels=[0], linestyles=["solid"]) # 拟合加权模型 clf = linear_model.SGDClassifier(alpha=0.01, max_iter=100) clf.fit(X, y, sample_weight=sample_weight) Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) samples_weights = ax.contour(xx, yy, Z, levels=[0], linestyles=["dashed"]) no_weights_handles, _ = no_weights.legend_elements() weights_handles, _ = samples_weights.legend_elements() ax.legend( [no_weights_handles[0], weights_handles[0]], ["no weights", "with weights"], loc="lower left", ) ax.set(xticks=(), yticks=()) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.054 seconds) .. _sphx_glr_download_auto_examples_linear_model_plot_sgd_weighted_samples.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_weighted_samples.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sgd_weighted_samples.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sgd_weighted_samples.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sgd_weighted_samples.zip ` .. include:: plot_sgd_weighted_samples.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_