{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7a0c752a",
   "metadata": {},
   "source": [
    "### Load MNIST dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e07d82fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Python ≥3.5 is required\n",
    "import sys\n",
    "assert sys.version_info >= (3, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1f97dcb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# scikit-learn ≥0.20 is required\n",
    "import sklearn\n",
    "assert sklearn.__version__ >= \"0.20\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "01f83832",
   "metadata": {},
   "outputs": [],
   "source": [
    "# common imports\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "affa0e2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sklearn.utils.Bunch"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# import function to scikit-learn datasets\n",
    "from sklearn.datasets import fetch_openml\n",
    "\n",
    "# load specified dataset (MNIST)\n",
    "mnist = fetch_openml('mnist_784', version=1, as_frame=False)\n",
    "\n",
    "# print type of dataset\n",
    "type(mnist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d51fd43",
   "metadata": {},
   "source": [
    "Bunch objects are sometimes used as an output for functions and methods. They extend dictionaries by enabling values to be accessed by key, bunch[\"value_key\"], or by an attribute, bunch.value_key.\\\n",
    "=> dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "78be57ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['name', 'age'])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Reminder of how dicts work\n",
    "example = {'name': 'somename', 'age': 15}\n",
    "example.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d0450c41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['data', 'target', 'frame', 'categories', 'feature_names', 'target_names', 'DESCR', 'details', 'url'])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let us check out the keys of the mnist dataset\n",
    "mnist.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e61e2adb",
   "metadata": {},
   "source": [
    "Datasets loaded by Scikit-Learn generally have a similar dictionary structure, including the following:\\\n",
    "* __DESCR__ a key describing the dataset\n",
    "* __data__ a key containing an array with one row per instance and one column per feature\n",
    "* __target__ a key containing an array with labels, one for each row of the data key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fe285433",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"**Author**: Yann LeCun, Corinna Cortes, Christopher J.C. Burges  \\n**Source**: [MNIST Website](http://yann.lecun.com/exdb/mnist/) - Date unknown  \\n**Please cite**:  \\n\\nThe MNIST database of handwritten digits with 784 features, raw data available at: http://yann.lecun.com/exdb/mnist/. It can be split in a training set of the first 60,000 examples, and a test set of 10,000 examples  \\n\\nIt is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image. It is a good database for people who want to try learning techniques and pattern recognition methods on real-world data while spending minimal efforts on preprocessing and formatting. The original black and white (bilevel) images from NIST were size normalized to fit in a 20x20 pixel box while preserving their aspect ratio. The resulting images contain grey levels as a result of the anti-aliasing technique used by the normalization algorithm. the images were centered in a 28x28 image by computing the center of mass of the pixels, and translating the image so as to position this point at the center of the 28x28 field.  \\n\\nWith some classification methods (particularly template-based methods, such as SVM and K-nearest neighbors), the error rate improves when the digits are centered by bounding box rather than center of mass. If you do this kind of pre-processing, you should report it in your publications. The MNIST database was constructed from NIST's NIST originally designated SD-3 as their training set and SD-1 as their test set. However, SD-3 is much cleaner and easier to recognize than SD-1. The reason for this can be found on the fact that SD-3 was collected among Census Bureau employees, while SD-1 was collected among high-school students. Drawing sensible conclusions from learning experiments requires that the result be independent of the choice of training set and test among the complete set of samples. Therefore it was necessary to build a new database by mixing NIST's datasets.  \\n\\nThe MNIST training set is composed of 30,000 patterns from SD-3 and 30,000 patterns from SD-1. Our test set was composed of 5,000 patterns from SD-3 and 5,000 patterns from SD-1. The 60,000 pattern training set contained examples from approximately 250 writers. We made sure that the sets of writers of the training set and test set were disjoint. SD-1 contains 58,527 digit images written by 500 different writers. In contrast to SD-3, where blocks of data from each writer appeared in sequence, the data in SD-1 is scrambled. Writer identities for SD-1 is available and we used this information to unscramble the writers. We then split SD-1 in two: characters written by the first 250 writers went into our new training set. The remaining 250 writers were placed in our test set. Thus we had two sets with nearly 30,000 examples each. The new training set was completed with enough examples from SD-3, starting at pattern # 0, to make a full set of 60,000 training patterns. Similarly, the new test set was completed with SD-3 examples starting at pattern # 35,000 to make a full set with 60,000 test patterns. Only a subset of 10,000 test images (5,000 from SD-1 and 5,000 from SD-3) is available on this site. The full 60,000 sample training set is available.\\n\\nDownloaded from openml.org.\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mnist[\"DESCR\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a70a746",
   "metadata": {},
   "source": [
    "### Prepare the MNIST dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9b7a120",
   "metadata": {},
   "source": [
    "$f(X) = y$\n",
    "\n",
    "$X$ is the data that we have and\\\n",
    "$y$ is what we want to predict\n",
    "\n",
    "In this example, we have images of handwritten digits $X$ and want to predict the digit $y$. In ML, we show the algorithm examples of X and y so that it learns the function $f(X) = y$. If it is successful, we can present $X$ to the algorithm that we did not train with and still get the $y$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e02cf2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = mnist[\"data\"], mnist[\"target\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "001d736f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b344be1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(70000, 784)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cef23e9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(70000,)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe3b1259",
   "metadata": {},
   "source": [
    "### Plot data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "953d9415",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import plotting libraries\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b68f6cee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "(784,)\n"
     ]
    }
   ],
   "source": [
    "# numpy type\n",
    "print(type(X))\n",
    "\n",
    "example_digit = X[0]\n",
    "print(example_digit.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8779b1a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28, 28)\n"
     ]
    }
   ],
   "source": [
    "# change shape\n",
    "example_digit = example_digit.reshape(28, 28)\n",
    "print(example_digit.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "dcc605cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 5084 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot example digit\n",
    "plt.imshow(example_digit, cmap=mpl.cm.binary)\n",
    "#plt.axis(\"off\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6d41d752",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# plot label of example image\n",
    "print(y[0])\n",
    "print(type(y[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "230cfd35",
   "metadata": {},
   "outputs": [],
   "source": [
    "# convert string labels to int\n",
    "y = y.astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "25a3a2e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# function to quickly plot an image\n",
    "def plot_digit(data):\n",
    "    image = data.reshape(28, 28)\n",
    "    plt.imshow(image, cmap = mpl.cm.binary, interpolation=\"nearest\")\n",
    "    plt.axis(\"off\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f1552762",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAGrUlEQVR4nO3dX2jPexzH8e90kqIt+VNTcuWeceVmw40kLtBcrJSUKBRyIRcLF3KhFBcuTflTEjXXuKKVNbnb7RQXUlsiUjvXp/Z7/zqbP69tj8elV1/7NufZt86n3/fXMT093QB5lvzt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with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# quickly plot a single digit\n",
    "plot_digit(X[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "74b3a063",
   "metadata": {},
   "outputs": [],
   "source": [
    "# function to quickly plot several digits\n",
    "def plot_digits(instances, **options):\n",
    "    size = 28\n",
    "    images = [instance.reshape(size,size) for instance in instances]\n",
    "    image = np.concatenate(images, axis=1)\n",
    "    plt.imshow(image, cmap = mpl.cm.binary, **options)\n",
    "    plt.axis(\"off\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "949b3914",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17704 characters]\n",
      "text/plain": [
       "<Figure size 648x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# quickly plot several digits\n",
    "plt.figure(figsize=(9,9))\n",
    "plot_digits(X[100:110])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec8a9d34",
   "metadata": {},
   "source": [
    "### Prepare data for machine learning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "febbd286",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "70000"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# how many images do we have\n",
    "len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fff839b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# we use the first 60000 for training and test with the other 10000 images\n",
    "X_train, X_test, y_train, y_test = X[:100], X[100:], y[:100], y[100:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bdbeb4e",
   "metadata": {},
   "source": [
    "### Train classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4c32ae9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import support vector machine\n",
    "import sklearn.svm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "fe06ae55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=10, kernel='poly')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# specify the parameter of the SVM\n",
    "classifier = sklearn.svm.SVC(C=10, gamma=\"scale\", kernel=\"poly\") #gamma=0.1 degree=3\n",
    "\n",
    "# train the SVM\n",
    "classifier.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e6209258",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAGJUlEQVR4nO3dO2gUexjGYVds7BIlaVRsbLQTLUQRC0G0shBBCxtFwRtY2WjlpbeJlSBio2UQrMSAKIJgFwUxsdLGIuAFiRfYUx9O9pvDTja+yT5P6cvsjpEfA/7ZTafb7a4C8qz+2zcALEycEEqcEEqcEEqcEGpNw+6/cmHwOgv9oScnhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhBInhFrzt29gGM3NzfXc1q1bt4R3srTm5+fL/dy5cz23Bw8elNe+ePGi3Ldv317uiTw5IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZRzzgG4c+dOuU9MTPTcHj16VF67cePGvu4pwczMTLnfu3ev79eenZ0td+ecwKIRJ4QSJ4QSJ4QSJ4QSJ4RylNKHd+/elfvFixfL/devXz23qamp8toTJ06Ue7I2RyXDyJMTQokTQokTQokTQokTQokTQokTQjnn7MPv37/LvTrHbHL37t1yTz7n/PTpU7lPTk72/dpbt24t9127dvX92qk8OSGUOCGUOCGUOCGUOCGUOCGUOCGUc04WTdM5ZtNXY3Y6nZ7btWvXymuX81eG9uLJCaHECaHECaHECaHECaHECaHECaGccw5At9vt+9rR0dFFvJOldfv27XJv83PZu3dv39cuV56cEEqcEEqcEEqcEEqcEEqcEEqcEMo5Zx8+fPhQ7tXnEpscP36872sH7fPnz+X+7du3cm/6ubT5ua1EnpwQSpwQSpwQSpwQSpwQSpwQylHKAl69elXux44dW6I7yXL58uVy//jxY6vXv379es9t/fr1rV57OfLkhFDihFDihFDihFDihFDihFDihFBDec45PT1d7leuXCn3+fn5cm/z0adbt26V+9u3b8v91KlT5b5hw4Zy//LlS89tamqqvLbJpk2byv306dM9t9Wrh+85Mnx/Y1gmxAmhxAmhxAmhxAmhxAmhxAmhVuw557Nnz3puhw8fLq/9+vXrYt/O//by5ctW+5MnT8q9+rmsWlV/ZrPt5zV3795d7mNjY61ef6Xx5IRQ4oRQ4oRQ4oRQ4oRQ4oRQ4oRQK/ac8+jRoz236jOL/0e32211/fj4eM/t58+f5bVN9970WdULFy6U+/3793tubf/ee/bsaXX9sPHkhFDihFDihFDihFDihFDihFDihFCdhrOrdgdbAzQzM1PuO3bs6Ll9//691Xvv37+/3Ju+93bz5s09tx8/fpTXXrp0qdybPs/Z5jt1m2zZsqXcnz59Wu5N36m7gi34j+LJCaHECaHECaHECaHECaHECaGW7UfGmv7b/uDBgz239+/fl9devXq13A8dOlTua9euLfc2Jicny/3169flvm/fvsW8nX85cOBAuQ/xUUlfPDkhlDghlDghlDghlDghlDghlDgh1LI952zy8OHDv30LA9F0hjoyMrI0N8LAeXJCKHFCKHFCKHFCKHFCKHFCKHFCqBV7zjms3rx5M7DXbvo85tmzZwf23sPIkxNCiRNCiRNCiRNCiRNCiRNCiRNCOedcZubm5sr95s2b5d7wKx9LJ0+eLPdt27b1/dr8lycnhBInhBInhBInhBInhBInhHKUsszcuHGj3Kenp8u90+mU+86dO3tu58+fL69lcXlyQihxQihxQihxQihxQihxQihxQijnnGGeP39e7hMTEwN9/yNHjvTcxsbGBvre/JsnJ4QSJ4QSJ4QSJ4QSJ4QSJ4QSJ4Ryzhnm8ePH5f7nz59Wrz8+Pl7uZ86cafX6LB5PTgglTgglTgglTgglTgglTgglTgjlnDPM7Oxsq+tHR0fLvelXBI6MjLR6fxaPJyeEEieEEieEEieEEieEEieEEieE6nS73WovR2BRLPhLUz05IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IZQ4IVTTrwBc8Cv7gMHz5IRQ4oRQ4oRQ4oRQ4oRQ4oRQ/wAj3eVzPh6F+gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# take a test digit\n",
    "test_digit = X[12121]\n",
    "plot_digit(test_digit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "62773b1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "# see label for test digit\n",
    "print(y[12121])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0ce21474",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4]\n"
     ]
    }
   ],
   "source": [
    "# see prediction for test digit\n",
    "print(classifier.predict([X[12121]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "78a8e8a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.27831745,  5.82217802,  0.72414032,  2.85240395,  9.30320665,\n",
       "         3.83670072,  4.8744213 ,  7.20637122,  1.74812151,  8.27550294]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# see propability for all classes\n",
    "classifier.decision_function([X[12121]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "45d93a99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# see the classes to understand which received which score\n",
    "classifier.classes_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc739051",
   "metadata": {},
   "source": [
    "### Evaluation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "990a5b7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy Train 100.0\n"
     ]
    }
   ],
   "source": [
    "# trainings accuracy\n",
    "wrong_images = X_train[(classifier.predict(X_train)-y_train) != 0]\n",
    "percentage = ((1-len(wrong_images)/len(X_train)) * 100)\n",
    "print(\"Accuracy Train \" + str(percentage))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f125a37d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy Test 58.92989985693848\n"
     ]
    }
   ],
   "source": [
    "# test accuracy\n",
    "wrong_images = X_test[(classifier.predict(X_test)-y_test) != 0]\n",
    "percentage = ((1-len(wrong_images)/len(X_test)) * 100)\n",
    "print(\"Accuracy Test \" + str(percentage))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdcb6e6e",
   "metadata": {},
   "source": [
    "Accuracy is strongly influenced by the distribution of the classes in the test data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be858cd5",
   "metadata": {},
   "source": [
    "#### Cross Validation\n",
    "[Find more information on cross validation here.](https://scikit-learn.org/stable/modules/cross_validation.html#cross-validation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7adb1ea7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.67647059 0.63636364 0.84848485]\n"
     ]
    }
   ],
   "source": [
    "# cross validation score\n",
    "from sklearn.model_selection import cross_val_score\n",
    "\n",
    "print(cross_val_score(classifier, X_train, y_train, cv=3, scoring=\"accuracy\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "11d22c5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 0 1 1 9 9 1 3 1 4 3 1 3 6 1 7 1 9 1 9 4 0 9 1 1 2 1 3 7 1 1 1 1 9 0 1 6\n",
      " 0 7 6 1 8 1 9 1 9 1 1 1 3 1 0 7 1 4 8 0 9 4 1 4 1 6 0 6 5 6 1 1 0 1 7 1 6\n",
      " 3 0 1 1 1 7 6 0 2 6 7 8 1 9 0 4 6 7 4 6 8 0 7 8 3 1]\n"
     ]
    }
   ],
   "source": [
    "# prediction of classifier\n",
    "from sklearn.model_selection import cross_val_predict\n",
    "\n",
    "y_train_pred = cross_val_predict(classifier, X_train, y_train, cv=3)\n",
    "print(y_train_pred)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b54e83a5",
   "metadata": {},
   "source": [
    "#### Precision"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ef7a9e7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8456190476190476"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import precision_score\n",
    "\n",
    "precision_score(y_train, y_train_pred, average='weighted')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da723740",
   "metadata": {},
   "source": [
    "#### Recall"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "cb77bf58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.72"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import recall_score\n",
    "\n",
    "recall_score(y_train, y_train_pred, average='weighted')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28867d1b",
   "metadata": {},
   "source": [
    "#### F1 Score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0674e0de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7283140672193305"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import f1_score\n",
    "\n",
    "f1_score(y_train, y_train_pred, average='weighted')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da59da11",
   "metadata": {},
   "source": [
    "#### Confusion Matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "adbdeece",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[12  1  0  0  0  0  0  0  0  0]\n",
      " [ 0 14  0  0  0  0  0  0  0  0]\n",
      " [ 0  2  2  0  0  0  0  1  0  1]\n",
      " [ 0  4  0  7  0  0  0  0  0  0]\n",
      " [ 0  4  0  0  6  0  1  0  0  0]\n",
      " [ 0  4  0  0  0  1  0  0  0  0]\n",
      " [ 0  2  0  0  0  0  9  0  0  0]\n",
      " [ 0  2  0  0  0  0  0  8  0  0]\n",
      " [ 0  2  0  0  0  0  0  0  5  1]\n",
      " [ 0  1  0  0  1  0  1  0  0  8]]\n"
     ]
    }
   ],
   "source": [
    "# confusing matrix\n",
    "from sklearn.metrics import confusion_matrix\n",
    "\n",
    "print(confusion_matrix(y_train, y_train_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "fb50c5a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.92307692 0.07692308 0.         0.         0.         0.\n",
      "  0.         0.         0.         0.        ]\n",
      " [0.         1.         0.         0.         0.         0.\n",
      "  0.         0.         0.         0.        ]\n",
      " [0.         0.33333333 0.33333333 0.         0.         0.\n",
      "  0.         0.16666667 0.         0.16666667]\n",
      " [0.         0.36363636 0.         0.63636364 0.         0.\n",
      "  0.         0.         0.         0.        ]\n",
      " [0.         0.36363636 0.         0.         0.54545455 0.\n",
      "  0.09090909 0.         0.         0.        ]\n",
      " [0.         0.8        0.         0.         0.         0.2\n",
      "  0.         0.         0.         0.        ]\n",
      " [0.         0.18181818 0.         0.         0.         0.\n",
      "  0.81818182 0.         0.         0.        ]\n",
      " [0.         0.2        0.         0.         0.         0.\n",
      "  0.         0.8        0.         0.        ]\n",
      " [0.         0.25       0.         0.         0.         0.\n",
      "  0.         0.         0.625      0.125     ]\n",
      " [0.         0.09090909 0.         0.         0.09090909 0.\n",
      "  0.09090909 0.         0.         0.72727273]]\n"
     ]
    }
   ],
   "source": [
    "cm = confusion_matrix(y_train, y_train_pred, normalize='true')\n",
    "print(cm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "2f0d536a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "dddf5fe8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27840 characters]\n",
      "text/plain": [
       "<Figure size 720x504 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "set_digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }\n",
    "\n",
    "df_cm = pd.DataFrame(cm, index=set_digits, columns=set_digits)\n",
    "plt.figure(figsize = (10,7))\n",
    "sn_plot = sn.heatmap(df_cm, annot=True, cmap=\"Greys\")\n",
    "plt.ylabel(\"True Label\")\n",
    "plt.xlabel(\"Predicted Label\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "44537aae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      0.92      0.96        13\n",
      "           1       0.39      1.00      0.56        14\n",
      "           2       1.00      0.33      0.50         6\n",
      "           3       1.00      0.64      0.78        11\n",
      "           4       0.86      0.55      0.67        11\n",
      "           5       1.00      0.20      0.33         5\n",
      "           6       0.82      0.82      0.82        11\n",
      "           7       0.89      0.80      0.84        10\n",
      "           8       1.00      0.62      0.77         8\n",
      "           9       0.80      0.73      0.76        11\n",
      "\n",
      "    accuracy                           0.72       100\n",
      "   macro avg       0.88      0.66      0.70       100\n",
      "weighted avg       0.85      0.72      0.73       100\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "\n",
    "print(classification_report(y_train, y_train_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57d96f56",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
