{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# SVM Tie Breaking Example\nTie breaking is costly if ``decision_function_shape='ovr'``, and therefore it\nis not enabled by default. This example illustrates the effect of the\n``break_ties`` parameter for a multiclass classification problem and\n``decision_function_shape='ovr'``.\n\nThe two plots differ only in the area in the middle where the classes are\ntied. If ``break_ties=False``, all input in that area would be classified as\none class, whereas if ``break_ties=True``, the tie-breaking mechanism will\ncreate a non-convex decision boundary in that area.\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.datasets import make_blobs\nfrom sklearn.svm import SVC\n\nX, y = make_blobs(random_state=27)\n\nfig, sub = plt.subplots(2, 1, figsize=(5, 8))\ntitles = (\"break_ties = False\", \"break_ties = True\")\n\nfor break_ties, title, ax in zip((False, True), titles, sub.flatten()):\n svm = SVC(\n kernel=\"linear\", C=1, break_ties=break_ties, decision_function_shape=\"ovr\"\n ).fit(X, y)\n\n xlim = [X[:, 0].min(), X[:, 0].max()]\n ylim = [X[:, 1].min(), X[:, 1].max()]\n\n xs = np.linspace(xlim[0], xlim[1], 1000)\n ys = np.linspace(ylim[0], ylim[1], 1000)\n xx, yy = np.meshgrid(xs, ys)\n\n pred = svm.predict(np.c_[xx.ravel(), yy.ravel()])\n\n colors = [plt.cm.Accent(i) for i in [0, 4, 7]]\n\n points = ax.scatter(X[:, 0], X[:, 1], c=y, cmap=\"Accent\")\n classes = [(0, 1), (0, 2), (1, 2)]\n line = np.linspace(X[:, 1].min() - 5, X[:, 1].max() + 5)\n ax.imshow(\n -pred.reshape(xx.shape),\n cmap=\"Accent\",\n alpha=0.2,\n extent=(xlim[0], xlim[1], ylim[1], ylim[0]),\n )\n\n for coef, intercept, col in zip(svm.coef_, svm.intercept_, classes):\n line2 = -(line * coef[1] + intercept) / coef[0]\n ax.plot(line2, line, \"-\", c=colors[col[0]])\n ax.plot(line2, line, \"--\", c=colors[col[1]])\n ax.set_xlim(xlim)\n ax.set_ylim(ylim)\n ax.set_title(title)\n ax.set_aspect(\"equal\")\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.21" } }, "nbformat": 4, "nbformat_minor": 0 }