.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/svm/plot_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 JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_svm_plot_weighted_samples.py: ===================== SVM: Weighted samples ===================== Plot decision function of a weighted dataset, where the size of points is proportional to its weight. The sample weighting rescales the C parameter, which means that the classifier puts more emphasis on getting these points right. The effect might often be subtle. To emphasize the effect here, we particularly increase the weight of the positive class, making the deformation of the decision boundary more visible. .. GENERATED FROM PYTHON SOURCE LINES 16-90 .. image-sg:: /auto_examples/svm/images/sphx_glr_plot_weighted_samples_001.png :alt: Constant weights, Modified weights :srcset: /auto_examples/svm/images/sphx_glr_plot_weighted_samples_001.png :class: sphx-glr-single-img .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_classification from sklearn.inspection import DecisionBoundaryDisplay from sklearn.svm import SVC X, y = make_classification( n_samples=1_000, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, class_sep=1.1, weights=[0.9, 0.1], random_state=0, ) # down-sample for plotting rng = np.random.RandomState(0) plot_indices = rng.choice(np.arange(X.shape[0]), size=100, replace=True) X_plot, y_plot = X[plot_indices], y[plot_indices] def plot_decision_function(classifier, sample_weight, axis, title): """Plot the synthetic data and the classifier decision function. Points with larger sample_weight are mapped to larger circles in the scatter plot.""" axis.scatter( X_plot[:, 0], X_plot[:, 1], c=y_plot, s=100 * sample_weight[plot_indices], alpha=0.9, cmap=plt.cm.bone, edgecolors="black", ) DecisionBoundaryDisplay.from_estimator( classifier, X_plot, response_method="decision_function", alpha=0.75, ax=axis, cmap=plt.cm.bone, ) axis.axis("off") axis.set_title(title) # we define constant weights as expected by the plotting function sample_weight_constant = np.ones(len(X)) # assign random weights to all points sample_weight_modified = abs(rng.randn(len(X))) # assign bigger weights to the positive class positive_class_indices = np.asarray(y == 1).nonzero()[0] sample_weight_modified[positive_class_indices] *= 15 # This model does not include sample weights. clf_no_weights = SVC(gamma=1) clf_no_weights.fit(X, y) # This other model includes sample weights. clf_weights = SVC(gamma=1) clf_weights.fit(X, y, sample_weight=sample_weight_modified) fig, axes = plt.subplots(1, 2, figsize=(14, 6)) plot_decision_function( clf_no_weights, sample_weight_constant, axes[0], "Constant weights" ) plot_decision_function(clf_weights, sample_weight_modified, axes[1], "Modified weights") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.271 seconds) .. _sphx_glr_download_auto_examples_svm_plot_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/1.7.X?urlpath=lab/tree/notebooks/auto_examples/svm/plot_weighted_samples.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/svm/plot_weighted_samples.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_weighted_samples.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_weighted_samples.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_weighted_samples.zip ` .. include:: plot_weighted_samples.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_