{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plotting Validation Curves\n\nIn this plot you can see the training scores and validation scores of an SVM\nfor different values of the kernel parameter gamma. For very low values of\ngamma, you can see that both the training score and the validation score are\nlow. This is called underfitting. Medium values of gamma will result in high\nvalues for both scores, i.e. the classifier is performing fairly well. If gamma\nis too high, the classifier will overfit, which means that the training score\nis good but the validation score is poor.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.datasets import load_digits\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import validation_curve\n\nX, y = load_digits(return_X_y=True)\nsubset_mask = np.isin(y, [1, 2])  # binary classification: 1 vs 2\nX, y = X[subset_mask], y[subset_mask]\n\nparam_range = np.logspace(-6, -1, 5)\ntrain_scores, test_scores = validation_curve(\n    SVC(),\n    X,\n    y,\n    param_name=\"gamma\",\n    param_range=param_range,\n    scoring=\"accuracy\",\n    n_jobs=2,\n)\ntrain_scores_mean = np.mean(train_scores, axis=1)\ntrain_scores_std = np.std(train_scores, axis=1)\ntest_scores_mean = np.mean(test_scores, axis=1)\ntest_scores_std = np.std(test_scores, axis=1)\n\nplt.title(\"Validation Curve with SVM\")\nplt.xlabel(r\"$\\gamma$\")\nplt.ylabel(\"Score\")\nplt.ylim(0.0, 1.1)\nlw = 2\nplt.semilogx(\n    param_range, train_scores_mean, label=\"Training score\", color=\"darkorange\", lw=lw\n)\nplt.fill_between(\n    param_range,\n    train_scores_mean - train_scores_std,\n    train_scores_mean + train_scores_std,\n    alpha=0.2,\n    color=\"darkorange\",\n    lw=lw,\n)\nplt.semilogx(\n    param_range, test_scores_mean, label=\"Cross-validation score\", color=\"navy\", lw=lw\n)\nplt.fill_between(\n    param_range,\n    test_scores_mean - test_scores_std,\n    test_scores_mean + test_scores_std,\n    alpha=0.2,\n    color=\"navy\",\n    lw=lw,\n)\nplt.legend(loc=\"best\")\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.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}