{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# One-Class SVM versus One-Class SVM using Stochastic Gradient Descent\n\nThis example shows how to approximate the solution of\n:class:`sklearn.svm.OneClassSVM` in the case of an RBF kernel with\n:class:`sklearn.linear_model.SGDOneClassSVM`, a Stochastic Gradient Descent\n(SGD) version of the One-Class SVM. A kernel approximation is first used in\norder to apply :class:`sklearn.linear_model.SGDOneClassSVM` which implements a\nlinear One-Class SVM using SGD.\n\nNote that :class:`sklearn.linear_model.SGDOneClassSVM` scales linearly with\nthe number of samples whereas the complexity of a kernelized\n:class:`sklearn.svm.OneClassSVM` is at best quadratic with respect to the\nnumber of samples. It is not the purpose of this example to illustrate the\nbenefits of such an approximation in terms of computation time but rather to\nshow that we obtain similar results on a toy dataset.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib\nimport matplotlib.lines as mlines\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.kernel_approximation import Nystroem\nfrom sklearn.linear_model import SGDOneClassSVM\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.svm import OneClassSVM\n\nfont = {\"weight\": \"normal\", \"size\": 15}\n\nmatplotlib.rc(\"font\", **font)\n\nrandom_state = 42\nrng = np.random.RandomState(random_state)\n\n# Generate train data\nX = 0.3 * rng.randn(500, 2)\nX_train = np.r_[X + 2, X - 2]\n# Generate some regular novel observations\nX = 0.3 * rng.randn(20, 2)\nX_test = np.r_[X + 2, X - 2]\n# Generate some abnormal novel observations\nX_outliers = rng.uniform(low=-4, high=4, size=(20, 2))\n\n# OCSVM hyperparameters\nnu = 0.05\ngamma = 2.0\n\n# Fit the One-Class SVM\nclf = OneClassSVM(gamma=gamma, kernel=\"rbf\", nu=nu)\nclf.fit(X_train)\ny_pred_train = clf.predict(X_train)\ny_pred_test = clf.predict(X_test)\ny_pred_outliers = clf.predict(X_outliers)\nn_error_train = y_pred_train[y_pred_train == -1].size\nn_error_test = y_pred_test[y_pred_test == -1].size\nn_error_outliers = y_pred_outliers[y_pred_outliers == 1].size\n\n# Fit the One-Class SVM using a kernel approximation and SGD\ntransform = Nystroem(gamma=gamma, random_state=random_state)\nclf_sgd = SGDOneClassSVM(\n    nu=nu, shuffle=True, fit_intercept=True, random_state=random_state, tol=1e-4\n)\npipe_sgd = make_pipeline(transform, clf_sgd)\npipe_sgd.fit(X_train)\ny_pred_train_sgd = pipe_sgd.predict(X_train)\ny_pred_test_sgd = pipe_sgd.predict(X_test)\ny_pred_outliers_sgd = pipe_sgd.predict(X_outliers)\nn_error_train_sgd = y_pred_train_sgd[y_pred_train_sgd == -1].size\nn_error_test_sgd = y_pred_test_sgd[y_pred_test_sgd == -1].size\nn_error_outliers_sgd = y_pred_outliers_sgd[y_pred_outliers_sgd == 1].size"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.inspection import DecisionBoundaryDisplay\n\n_, ax = plt.subplots(figsize=(9, 6))\n\nxx, yy = np.meshgrid(np.linspace(-4.5, 4.5, 50), np.linspace(-4.5, 4.5, 50))\nX = np.concatenate([xx.ravel().reshape(-1, 1), yy.ravel().reshape(-1, 1)], axis=1)\nDecisionBoundaryDisplay.from_estimator(\n    clf,\n    X,\n    response_method=\"decision_function\",\n    plot_method=\"contourf\",\n    ax=ax,\n    cmap=\"PuBu\",\n)\nDecisionBoundaryDisplay.from_estimator(\n    clf,\n    X,\n    response_method=\"decision_function\",\n    plot_method=\"contour\",\n    ax=ax,\n    linewidths=2,\n    colors=\"darkred\",\n    levels=[0],\n)\nDecisionBoundaryDisplay.from_estimator(\n    clf,\n    X,\n    response_method=\"decision_function\",\n    plot_method=\"contourf\",\n    ax=ax,\n    colors=\"palevioletred\",\n    levels=[0, clf.decision_function(X).max()],\n)\n\ns = 20\nb1 = plt.scatter(X_train[:, 0], X_train[:, 1], c=\"white\", s=s, edgecolors=\"k\")\nb2 = plt.scatter(X_test[:, 0], X_test[:, 1], c=\"blueviolet\", s=s, edgecolors=\"k\")\nc = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c=\"gold\", s=s, edgecolors=\"k\")\n\nax.set(\n    title=\"One-Class SVM\",\n    xlim=(-4.5, 4.5),\n    ylim=(-4.5, 4.5),\n    xlabel=(\n        f\"error train: {n_error_train}/{X_train.shape[0]}; \"\n        f\"errors novel regular: {n_error_test}/{X_test.shape[0]}; \"\n        f\"errors novel abnormal: {n_error_outliers}/{X_outliers.shape[0]}\"\n    ),\n)\n_ = ax.legend(\n    [mlines.Line2D([], [], color=\"darkred\", label=\"learned frontier\"), b1, b2, c],\n    [\n        \"learned frontier\",\n        \"training observations\",\n        \"new regular observations\",\n        \"new abnormal observations\",\n    ],\n    loc=\"upper left\",\n)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "_, ax = plt.subplots(figsize=(9, 6))\n\nxx, yy = np.meshgrid(np.linspace(-4.5, 4.5, 50), np.linspace(-4.5, 4.5, 50))\nX = np.concatenate([xx.ravel().reshape(-1, 1), yy.ravel().reshape(-1, 1)], axis=1)\nDecisionBoundaryDisplay.from_estimator(\n    pipe_sgd,\n    X,\n    response_method=\"decision_function\",\n    plot_method=\"contourf\",\n    ax=ax,\n    cmap=\"PuBu\",\n)\nDecisionBoundaryDisplay.from_estimator(\n    pipe_sgd,\n    X,\n    response_method=\"decision_function\",\n    plot_method=\"contour\",\n    ax=ax,\n    linewidths=2,\n    colors=\"darkred\",\n    levels=[0],\n)\nDecisionBoundaryDisplay.from_estimator(\n    pipe_sgd,\n    X,\n    response_method=\"decision_function\",\n    plot_method=\"contourf\",\n    ax=ax,\n    colors=\"palevioletred\",\n    levels=[0, pipe_sgd.decision_function(X).max()],\n)\n\ns = 20\nb1 = plt.scatter(X_train[:, 0], X_train[:, 1], c=\"white\", s=s, edgecolors=\"k\")\nb2 = plt.scatter(X_test[:, 0], X_test[:, 1], c=\"blueviolet\", s=s, edgecolors=\"k\")\nc = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c=\"gold\", s=s, edgecolors=\"k\")\n\nax.set(\n    title=\"Online One-Class SVM\",\n    xlim=(-4.5, 4.5),\n    ylim=(-4.5, 4.5),\n    xlabel=(\n        f\"error train: {n_error_train_sgd}/{X_train.shape[0]}; \"\n        f\"errors novel regular: {n_error_test_sgd}/{X_test.shape[0]}; \"\n        f\"errors novel abnormal: {n_error_outliers_sgd}/{X_outliers.shape[0]}\"\n    ),\n)\nax.legend(\n    [mlines.Line2D([], [], color=\"darkred\", label=\"learned frontier\"), b1, b2, c],\n    [\n        \"learned frontier\",\n        \"training observations\",\n        \"new regular observations\",\n        \"new abnormal observations\",\n    ],\n    loc=\"upper left\",\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
}