{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# SVM: Weighted samples\n\nPlot decision function of a weighted dataset, where the size of points\nis proportional to its weight.\n\nThe sample weighting rescales the C parameter, which means that the classifier\nputs more emphasis on getting these points right. The effect might often be\nsubtle.\nTo emphasize the effect here, we particularly weight outliers, making the\ndeformation of the decision boundary very visible.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn import svm\n\n\ndef plot_decision_function(classifier, sample_weight, axis, title):\n # plot the decision function\n xx, yy = np.meshgrid(np.linspace(-4, 5, 500), np.linspace(-4, 5, 500))\n\n Z = classifier.decision_function(np.c_[xx.ravel(), yy.ravel()])\n Z = Z.reshape(xx.shape)\n\n # plot the line, the points, and the nearest vectors to the plane\n axis.contourf(xx, yy, Z, alpha=0.75, cmap=plt.cm.bone)\n axis.scatter(\n X[:, 0],\n X[:, 1],\n c=y,\n s=100 * sample_weight,\n alpha=0.9,\n cmap=plt.cm.bone,\n edgecolors=\"black\",\n )\n\n axis.axis(\"off\")\n axis.set_title(title)\n\n\n# we create 20 points\nnp.random.seed(0)\nX = np.r_[np.random.randn(10, 2) + [1, 1], np.random.randn(10, 2)]\ny = [1] * 10 + [-1] * 10\nsample_weight_last_ten = abs(np.random.randn(len(X)))\nsample_weight_constant = np.ones(len(X))\n# and bigger weights to some outliers\nsample_weight_last_ten[15:] *= 5\nsample_weight_last_ten[9] *= 15\n\n# Fit the models.\n\n# This model does not take into account sample weights.\nclf_no_weights = svm.SVC(gamma=1)\nclf_no_weights.fit(X, y)\n\n# This other model takes into account some dedicated sample weights.\nclf_weights = svm.SVC(gamma=1)\nclf_weights.fit(X, y, sample_weight=sample_weight_last_ten)\n\nfig, axes = plt.subplots(1, 2, figsize=(14, 6))\nplot_decision_function(\n clf_no_weights, sample_weight_constant, axes[0], \"Constant weights\"\n)\nplot_decision_function(clf_weights, sample_weight_last_ten, axes[1], \"Modified weights\")\n\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.20" } }, "nbformat": 4, "nbformat_minor": 0 }