{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plot different SVM classifiers in the iris dataset\n\nComparison of different linear SVM classifiers on a 2D projection of the iris\ndataset. We only consider the first 2 features of this dataset:\n\n- Sepal length\n- Sepal width\n\nThis example shows how to plot the decision surface for four SVM classifiers\nwith different kernels.\n\nThe linear models ``LinearSVC()`` and ``SVC(kernel='linear')`` yield slightly\ndifferent decision boundaries. This can be a consequence of the following\ndifferences:\n\n- ``LinearSVC`` minimizes the squared hinge loss while ``SVC`` minimizes the\n  regular hinge loss.\n\n- ``LinearSVC`` uses the One-vs-All (also known as One-vs-Rest) multiclass\n  reduction while ``SVC`` uses the One-vs-One multiclass reduction.\n\nBoth linear models have linear decision boundaries (intersecting hyperplanes)\nwhile the non-linear kernel models (polynomial or Gaussian RBF) have more\nflexible non-linear decision boundaries with shapes that depend on the kind of\nkernel and its parameters.\n\n.. NOTE:: while plotting the decision function of classifiers for toy 2D\n   datasets can help get an intuitive understanding of their respective\n   expressive power, be aware that those intuitions don't always generalize to\n   more realistic high-dimensional problems.\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\n\nfrom sklearn import datasets, svm\nfrom sklearn.inspection import DecisionBoundaryDisplay\n\n# import some data to play with\niris = datasets.load_iris()\n# Take the first two features. We could avoid this by using a two-dim dataset\nX = iris.data[:, :2]\ny = iris.target\n\n# we create an instance of SVM and fit out data. We do not scale our\n# data since we want to plot the support vectors\nC = 1.0  # SVM regularization parameter\nmodels = (\n    svm.SVC(kernel=\"linear\", C=C),\n    svm.LinearSVC(C=C, max_iter=10000),\n    svm.SVC(kernel=\"rbf\", gamma=0.7, C=C),\n    svm.SVC(kernel=\"poly\", degree=3, gamma=\"auto\", C=C),\n)\nmodels = (clf.fit(X, y) for clf in models)\n\n# title for the plots\ntitles = (\n    \"SVC with linear kernel\",\n    \"LinearSVC (linear kernel)\",\n    \"SVC with RBF kernel\",\n    \"SVC with polynomial (degree 3) kernel\",\n)\n\n# Set-up 2x2 grid for plotting.\nfig, sub = plt.subplots(2, 2)\nplt.subplots_adjust(wspace=0.4, hspace=0.4)\n\nX0, X1 = X[:, 0], X[:, 1]\n\nfor clf, title, ax in zip(models, titles, sub.flatten()):\n    disp = DecisionBoundaryDisplay.from_estimator(\n        clf,\n        X,\n        response_method=\"predict\",\n        cmap=plt.cm.coolwarm,\n        alpha=0.8,\n        ax=ax,\n        xlabel=iris.feature_names[0],\n        ylabel=iris.feature_names[1],\n    )\n    ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors=\"k\")\n    ax.set_xticks(())\n    ax.set_yticks(())\n    ax.set_title(title)\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.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}