{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# SVM with custom kernel\n\nSimple usage of `svm` classifier with a custom kernel. It will\nplot the decision surface and highlight the support vectors.\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 datasets, svm\nfrom sklearn.inspection import DecisionBoundaryDisplay\n\n# Import some data to play with.\niris = datasets.load_iris()\nX = iris.data[:, :2]  # We only take the first two features. We could\n# avoid this ugly slicing by using a two-dim dataset.\nY = iris.target\n\n\ndef my_kernel(X, Y):\n    \"\"\"\n    We create a custom kernel:\n\n                 (2  0)\n    k(X, Y) = X  (    ) Y.T\n                 (0  1)\n    \"\"\"\n    M = np.array([[2, 0], [0, 1.0]])\n    return np.dot(np.dot(X, M), Y.T)\n\n\n# We create an instance of SVC with that kernel and fit it on the data.\nclf = svm.SVC(kernel=my_kernel)\nclf.fit(X, Y)\n\nax = plt.gca()\nDecisionBoundaryDisplay.from_estimator(\n    clf,\n    X,\n    multiclass_colors=\"Paired\",\n    ax=ax,\n    response_method=\"predict\",\n    plot_method=\"pcolormesh\",\n    shading=\"auto\",\n    alpha=0.5,\n)\n\n# Plot the training points\nplt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)\n# Highlight the support vectors\nplt.scatter(\n    X[clf.support_, 0],\n    X[clf.support_, 1],\n    facecolor=\"none\",\n    edgecolors=\"k\",\n)\nplt.title(\"3-Class classification using Support Vector Machine with custom kernel\")\nplt.axis(\"tight\")\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.14.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}