iui-group-l-name-zensiert/0-pilot-project/MNIST-template.ipynb

1037 lines
79 KiB
Plaintext
Raw Permalink Normal View History

{
"cells": [
{
"cell_type": "markdown",
"id": "6904e7ae",
"metadata": {},
"source": [
"### Load MNIST dataset"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e3d41c8f",
"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": "55990ccc",
"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": "933f52fb",
"metadata": {},
"outputs": [],
"source": [
"# common imports\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "41435175",
"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": "fe14558d",
"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": "80a39a2e",
"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": "5c211b9c",
"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": "a4c5fb8d",
"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": "7c077427",
"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": "f3f2e42a",
"metadata": {},
"source": [
"### Prepare the MNIST dataset"
]
},
{
"cell_type": "markdown",
"id": "6d5d2658",
"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": "99784cec",
"metadata": {},
"outputs": [],
"source": [
"X, y = mnist[\"data\"], mnist[\"target\"]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "923676c7",
"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": "ed44fc7a",
"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": "94ee3e59",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(70000,)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y.shape"
]
},
{
"cell_type": "markdown",
"id": "478cb336",
"metadata": {},
"source": [
"### Plot data"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f3cfebc6",
"metadata": {},
"outputs": [],
"source": [
"# import plotting libraries\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "6d799c25",
"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": "72c7305b",
"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": "f5b0b349",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAOUElEQVR4nO3dX4xUdZrG8ecF8R+DCkuHtAyRGTQmHY1AStgEg+hk8U+iwI2BGERjxAuQmQTiolzAhRdGd2YyihnTqAE2IxPCSITErIMEY4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9914yf+aZZ5L5nj17qmZHjhypa6a8XH755VWzZcuWJfet9XbNI0eOrGsmtB/O7EAQlB0IgrIDQVB2IAjKDgRB2YEgKDsQRJjr7Fu3bm0ob0RXV1cyv+eee5L58OHDk/mKFSuqZldddVVyX8TBmR0IgrIDQVB2IAjKDgRB2YEgKDsQBGUHgjB3T9/BbIKkjZLGSXJJ3e7+BzNbI+kRSZXsrk+6+xup71UqlbxcLjc8NICBlUollcvlAVddHswv1Xwvabm7v29moyTtNbMdWfZ7d/+vvAYF0DyDWZ/9mKRj2e2vzexjSeObPRiAfF3Qz+xmNlHSFEnn3uNpqZn1mNkrZja6yj6LzaxsZuVKpTLQXQC0wKDLbmY/k/QXSb9x979L+qOkSZImq+/M/9uB9nP3bncvuXupo6Oj8YkB1GVQZTezEeor+p/c/TVJcvcT7n7W3f8paZ2kac0bE0CjapbdzEzSy5I+dvff9dve2e9u8yTtz388AHkZzKvxMyQtlPShme3Ltj0paYGZTVbf5bheSel1iwEUajCvxu+WNNB1u+Q
"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": "c70641f8",
"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": "98f8561b",
"metadata": {},
"outputs": [],
"source": [
"# convert string labels to int\n",
"y = y.astype(np.uint8)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "33e244b2",
"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": "20043b74",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAGrUlEQVR4nO3dX2jPexzH8e90kqIt+VNTcuWeceVmw40kLtBcrJSUKBRyIRcLF3KhFBcuTflTEjXXuKKVNbnb7RQXUlsiUjvXp/Z7/zqbP69tj8elV1/7NufZt86n3/fXMT093QB5lvztGwBmJk4IJU4IJU4IJU4I9U+b3f/Khd+vY6Y/9OSEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUOKEUO2+ApAF5s2bN+V+8+bNltudO3fKaw8fPlzuJ0+eLPeenp5yX2w8OSGUOCGUOCGUOCGUOCGUOCGUOCFUx/T0dLWXI3nGxsbKffv27eU+NTX1C+/mv7q6usr98+fPv+1nh+uY6Q89OSGUOCGUOCGUOCGUOCGUOCGUOCGUz3POMyMjI+W+f//+cp+cnCz3jo4Zj9yapmmazs7O8tqlS5eW+6dPn8r91atXLbctW7bM6WfPR56cEEqcEEqcEEqcEEqcEEqcEMpHxv6Cr1+/ttxGR0fLawcGBsp9YmKi3Nv8e5dHKe2OM86fP1/u/f395V7d25UrV8prL1y4UO7hfGQM5hNxQihxQihxQihxQihxQihxQigfGfsLjh071nK7d+/eH7yT/6fd1wd++fKl3Ht7e8v9xYsXLbd3796V1y5EnpwQSpwQSpwQSpwQSpwQSpwQSpwQyjnnb9DuPHB4eLjl1u7zlu309fWV+549e8r93LlzLbd169aV127evLncV65cWe7Pnz9vuc319zI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SJ4QSJ4TqaPOayPodksHev3/fcjt48GB57cjIyJx+drtXb7b7yFpl9erV5X7o0KFyn8+v9VzAZvwPwpMTQokTQokTQokTQokTQokTQokTQi3Yc87Khw8fyv327dvlXn1NXtPM7Zzz9OnT5bXHjx8v940bN5Y7kZxzwnwiTgglTgglTgglTgglTgglTgi1KM85IYxzTphPxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmhxAmh/mmzz/jVZMDv58kJocQJocQJocQJocQJocQJof4Ftv8iCGE1mZwAAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 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": "6e4369de",
"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": "fb3f6d95",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAgQAAABACAYAAABlVG/FAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAzS0lEQVR4nO2d6XOb13X/v9gXYt8IkABJkAT3nYoWyhYt2fJuxU5aN46dzmT8sm2SV/0nMm3euW4nXTxTN5mkit1kPJEsK5Jsi1pISlzAfQUBEgCx7zue3wv97g0pUbIckwAoP58ZTyZcxAvgee5z7jnf8z0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ths9nKvPrDRS6Xg9/vRyQSgcfj2fU9EnzF43EsLi7S7gmfz4dgMIhwOIx0Oo2FhQUEAgFks1kUi0V0dnZCIBCUPVUNAOl0GsFgEF6vF263GzqdruwPmW9CIpHAxsYGHA4H1tfXd4kJSbBfW1sLPp9f9uA4Ho9jZWUFd+7cwZdffont7W1wOBxayhgeHkZjY+OuWnpbWxvUajW0Wm1FdBhUVVXBYDDgzJkz6OjoAMMwqK6uRktLC+rr61FTU7Pn7yWTSYRCIdy5c2fX18neRu6t/SSbzSKVSmFsbAxTU1NIJpOQSqWw2Wzo6enB8ePHYTaboVaraQYjm82Cz+fTa6e9vR1WqxVKpRITExMIhUIIh8PgcrmQy+X0utvY2EAqlcLvf/97NDY2YmhoCFKpdN+fKQzDYHR0FBMTE1RfQjojlEolfD4fQqEQzWbkcjlks1nE43Fks1kIBIKSZ5lKtusFg0FcvXoVIyMjD9RPCFwuF2KxGBqNBnq9Hq+88gq
"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": "3a69d0fd",
"metadata": {},
"source": [
"### Prepare data for machine learning"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "dcc31672",
"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": "282ba914",
"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": "5c34bbd5",
"metadata": {},
"source": [
"### Train classifier"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "f0b6e1c9",
"metadata": {},
"outputs": [],
"source": [
"# import support vector machine\n",
"import sklearn.svm"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "54e3fb64",
"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": "29446c32",
"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": "5030ccc3",
"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": "22c780fb",
"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": "990d3925",
"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": "b6e1d70c",
"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": "a65ed630",
"metadata": {},
"source": [
"### Evaluation"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "4c93c5f9",
"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": "f01ec3dd",
"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": "c41db6b0",
"metadata": {},
"source": [
"Accuracy is strongly influenced by the distribution of the classes in the test data."
]
},
{
"cell_type": "markdown",
"id": "3f830f36",
"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": "eeec5311",
"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": "d1ed46a3",
"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": "539cfa0c",
"metadata": {},
"source": [
"#### Precision"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "abfe8383",
"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": "d899dd6f",
"metadata": {},
"source": [
"#### Recall"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "15d30ae5",
"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": "393c3b1c",
"metadata": {},
"source": [
"#### F1 Score"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "53fa1823",
"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": "08b6bdc2",
"metadata": {},
"source": [
"#### Confusion Matrix"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "e205d359",
"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": "4ec777ac",
"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": "9c53a0a7",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import seaborn as sn"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "c47e7c69",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjEAAAGtCAYAAADnIyVRAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAABQ/UlEQVR4nO3deXhU5d3/8fc3G8giazJsMaLgguIKoqCyFxQFFHF70IJFHp+fVrQiUq3Qggtai/uWKqhFsdhSHwQLPiCUCoqiyA6KiIQt7CiKhszcvz8yTgkkmURn5sxhPq/rmss5c+455zO3CfPNfZ/FnHOIiIiI+E2a1wFEREREfgoVMSIiIuJLKmJERETEl1TEiIiIiC+piBERERFfUhEjIiIivqQiRkREROLOzMab2TYzW17OejOzJ8xsrZktNbOzom1TRYyIiIgkwktAzwrWXwS0DD+GAM9G26CKGBEREYk759w8YFcFTfoAr7gSHwB1zaxxRdvMiGXAWAoEAr67lPD69eu9jlAlRx11lNcRRESkNEvozsxi+V3735SMoPwo3zmXX4X3NwUKDlreGH5tS3lvSNoiRkRERPwjXLBUpWj52VTEiIiIpCizhA78RLMJyD1ouVn4tXLpmBgREZEUZWYxe8TAVOD68FlK5wJ7nXPlTiWBRmJEREQkAcxsEtAJaGhmG4FRQCaAc+454G3gYmAt8B0wKNo2VcSIiIikqEROJznnromy3gE3V2WbKmJERERSVFqav48q8Xd6ERERSVkaiREREUlRSXZ2UpWpiBEREUlRfi9iNJ0kIiIivqSRGBERkRTl95EYFTEiIiIpyu9FjKaTR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
"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": "ef09fc40",
"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": "6f9816f1",
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}