intro-to-data-science/workshop.ipynb

1669 lines
216 KiB
Text
Raw Normal View History

2018-03-21 17:37:55 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"# Workshop: Machine Learning for Beginners"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## What is Machine Learning"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Machine learning is the process of **extracting knowledge from data** in an automated fashion.\n",
2018-03-21 17:37:55 +01:00
"\n",
2019-12-05 12:27:39 +01:00
"The use cases usually are making predictions on new and unseen data or simply understanding a given dataset better by finding patterns.\n",
2018-03-21 17:37:55 +01:00
"\n",
2019-12-05 12:27:39 +01:00
"Central to machine learning is the idea of **automating** the **decision making** from data **without** the user specifying **explicit rules** how these decisions should be made."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"raw/what_is_machine_learning.png\" width=\"60%\">"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"### Examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"raw/examples.png\" width=\"60%\">"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## Types of Machine Learning"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"raw/3_types_of_machine_learning.png\" width=\"60%\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- **Supervised** (focus of this workshop): Each entry in the dataset comes with a \"label\". Examples are a list of emails where spam mail is already marked as such or a sample of handwritten digits. The goal is to use the historic data to make predictions.\n",
2018-03-21 17:37:55 +01:00
"\n",
"- **Unsupervised**: There is no desired output associated with a data entry. In a sense, one can think of unsupervised learning as a means of discovering labels from the data itself. A popular example is the clustering of customer data.\n",
"\n",
"- **Reinforcement**: Conceptually, this can be seen as \"learning by doing\". Some kind of \"reward function\" tells how good a predicted outcome is. For example, chess computers are typically programmed with this approach."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"### Types of Supervised Learning"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"raw/classification_vs_regression.png\" width=\"60%\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- In **classification** tasks, the labels are *discrete*, such as \"spam\" or \"no spam\" for emails. Often, labels are nominal (e.g., colors of something), or ordinal (e.g., T-shirt sizes in S, M, or L).\n",
"- In **regression**, the labels are *continuous*. For example, given a person's age, education, and position, infer his/her salary."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Case Study: Iris Flower Classification"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"raw/iris_data.png\" width=\"60%\">"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## Python for Scientific Computing: A brief Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python itself does not come with any scientific algorithms. However, over time, many open source libraries emerged that are useful to build machine learning applications.\n",
2018-03-21 17:37:55 +01:00
"\n",
2019-12-05 12:27:39 +01:00
"Among the popular ones are [numpy](https://numpy.org/) (numerical computations, linear algebra), [pandas](https://pandas.pydata.org/) (data processing), [matplotlib](https://matplotlib.org/) (visualisations), and [scikit-learn](https://scikit-learn.org/stable/index.html) (machine learning algorithms).\n",
2018-03-21 17:37:55 +01:00
"\n",
"First, import the libraries:"
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 1,
2018-03-21 17:37:55 +01:00
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following line is needed so that this Jupyter notebook creates the visiualizations in the notebook and not in a new window. This has nothing to do with Python."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 2,
2018-03-21 17:37:55 +01:00
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Standard Python can do basic arithmetic operations ..."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"a = 1\n",
"b = 2\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"c = a + b\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... and provides some simple **data structures**, such as a list of values."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"l = [a, b, c, 4]\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"l"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"Numpy provides a data structure called an **n-dimensional array**. This may sound fancy at first but when used with only 1 or 2 dimensions, it basically represents vectors and matrices. Arrays allow for much faster computations as they are implemented in the very fast [C language](https://en.wikipedia.org/wiki/C_%28programming_language%29)."
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"To create an array, we use the [array()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy-array) function from the imported `np` module and provide it with a `list` of values."
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"v1 = np.array([1, 2, 3])\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"v1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A vector can be multiplied with a scalar."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 6, 9])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"v2 = v1 * 3\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"v2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create a matrix, just use a list of (row) list of values instead."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"m1 = np.array([\n",
" [1, 2, 3],\n",
" [4, 5, 6],\n",
"])\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"m1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use numpy to multiply a matrix with a vector to obtain a new vector ..."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([14, 32])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"v3 = np.dot(m1, v1)\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"v3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... or simply transpose it."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 4],\n",
" [2, 5],\n",
" [3, 6]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"m1.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The rules from maths still apply and it makes a difference if a vector is multiplied from the left or the right by a matrix. The following operation will fail."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 10,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "shapes (3,) and (2,3) not aligned: 3 (dim 0) != 2 (dim 0)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-10-c170f5d663e1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mdot\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
2018-03-21 17:42:59 +01:00
"\u001b[0;31mValueError\u001b[0m: shapes (3,) and (2,3) not aligned: 3 (dim 0) != 2 (dim 0)"
]
}
],
2018-03-21 17:37:55 +01:00
"source": [
"np.dot(v1, m1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to retrieve only a slice (= subset) of an array's data, we can \"index\" into it. For example, the first row of the matrix is ..."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"m1[0, :]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... while the second column is:"
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 5])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"m1[:, 1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To acces the lowest element in the right column, two indices can be used."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"m1[1, 2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy also provides various other functions and constants, such as sinus or pi. To further illustrate the concept of **vectorization**, let us calculate the sinus curve over a range of values."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-9.42477796, -9.23437841, -9.04397885, -8.8535793 , -8.66317974,\n",
" -8.47278019, -8.28238063, -8.09198108, -7.90158152, -7.71118197,\n",
" -7.52078241, -7.33038286, -7.1399833 , -6.94958375, -6.75918419,\n",
" -6.56878464, -6.37838508, -6.18798553, -5.99758598, -5.80718642,\n",
" -5.61678687, -5.42638731, -5.23598776, -5.0455882 , -4.85518865,\n",
" -4.66478909, -4.47438954, -4.28398998, -4.09359043, -3.90319087,\n",
" -3.71279132, -3.52239176, -3.33199221, -3.14159265, -2.9511931 ,\n",
" -2.76079354, -2.57039399, -2.37999443, -2.18959488, -1.99919533,\n",
" -1.80879577, -1.61839622, -1.42799666, -1.23759711, -1.04719755,\n",
" -0.856798 , -0.66639844, -0.47599889, -0.28559933, -0.09519978,\n",
" 0.09519978, 0.28559933, 0.47599889, 0.66639844, 0.856798 ,\n",
" 1.04719755, 1.23759711, 1.42799666, 1.61839622, 1.80879577,\n",
" 1.99919533, 2.18959488, 2.37999443, 2.57039399, 2.76079354,\n",
" 2.9511931 , 3.14159265, 3.33199221, 3.52239176, 3.71279132,\n",
" 3.90319087, 4.09359043, 4.28398998, 4.47438954, 4.66478909,\n",
" 4.85518865, 5.0455882 , 5.23598776, 5.42638731, 5.61678687,\n",
" 5.80718642, 5.99758598, 6.18798553, 6.37838508, 6.56878464,\n",
" 6.75918419, 6.94958375, 7.1399833 , 7.33038286, 7.52078241,\n",
" 7.71118197, 7.90158152, 8.09198108, 8.28238063, 8.47278019,\n",
" 8.66317974, 8.8535793 , 9.04397885, 9.23437841, 9.42477796])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"x = np.linspace(-3*np.pi, 3*np.pi, 100)\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"x"
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-3.67394040e-16, -1.89251244e-01, -3.71662456e-01, -5.40640817e-01,\n",
" -6.90079011e-01, -8.14575952e-01, -9.09631995e-01, -9.71811568e-01,\n",
" -9.98867339e-01, -9.89821442e-01, -9.45000819e-01, -8.66025404e-01,\n",
" -7.55749574e-01, -6.18158986e-01, -4.58226522e-01, -2.81732557e-01,\n",
" -9.50560433e-02, 9.50560433e-02, 2.81732557e-01, 4.58226522e-01,\n",
" 6.18158986e-01, 7.55749574e-01, 8.66025404e-01, 9.45000819e-01,\n",
" 9.89821442e-01, 9.98867339e-01, 9.71811568e-01, 9.09631995e-01,\n",
" 8.14575952e-01, 6.90079011e-01, 5.40640817e-01, 3.71662456e-01,\n",
" 1.89251244e-01, -1.22464680e-16, -1.89251244e-01, -3.71662456e-01,\n",
" -5.40640817e-01, -6.90079011e-01, -8.14575952e-01, -9.09631995e-01,\n",
" -9.71811568e-01, -9.98867339e-01, -9.89821442e-01, -9.45000819e-01,\n",
" -8.66025404e-01, -7.55749574e-01, -6.18158986e-01, -4.58226522e-01,\n",
" -2.81732557e-01, -9.50560433e-02, 9.50560433e-02, 2.81732557e-01,\n",
" 4.58226522e-01, 6.18158986e-01, 7.55749574e-01, 8.66025404e-01,\n",
" 9.45000819e-01, 9.89821442e-01, 9.98867339e-01, 9.71811568e-01,\n",
" 9.09631995e-01, 8.14575952e-01, 6.90079011e-01, 5.40640817e-01,\n",
" 3.71662456e-01, 1.89251244e-01, 1.22464680e-16, -1.89251244e-01,\n",
" -3.71662456e-01, -5.40640817e-01, -6.90079011e-01, -8.14575952e-01,\n",
" -9.09631995e-01, -9.71811568e-01, -9.98867339e-01, -9.89821442e-01,\n",
" -9.45000819e-01, -8.66025404e-01, -7.55749574e-01, -6.18158986e-01,\n",
" -4.58226522e-01, -2.81732557e-01, -9.50560433e-02, 9.50560433e-02,\n",
" 2.81732557e-01, 4.58226522e-01, 6.18158986e-01, 7.55749574e-01,\n",
" 8.66025404e-01, 9.45000819e-01, 9.89821442e-01, 9.98867339e-01,\n",
" 9.71811568e-01, 9.09631995e-01, 8.14575952e-01, 6.90079011e-01,\n",
" 5.40640817e-01, 3.71662456e-01, 1.89251244e-01, 3.67394040e-16])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"y = np.sin(x)\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"With matplotlib's [plot()](https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot) function we can visualize the sinus curve."
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2019-12-05 12:27:39 +01:00
"[<matplotlib.lines.Line2D at 0x7ff8934ee2e8>]"
2018-03-21 17:42:59 +01:00
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2019-12-05 12:27:39 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYcAAAD4CAYAAAAHHSreAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAgAElEQVR4nO29d3hc13Xo+1sYdGDQKwk2kADYSUkURTdJlmRZtPMk90h5eZFj++o6ieMb5yaO9Pk9x5/T5OTFzkuuHVtxkx1Hkq246DqUZUmW6CJTYjELQKKxohDAoM+gY2a/P8454AgESAAzc8rM/n3ffJg5deGUvfYqe21RSqHRaDQaTTRpTgug0Wg0GvehlYNGo9ForkIrB41Go9FchVYOGo1Go7kKrRw0Go1GcxXpTguwEsrKytT69eudFkOj0Wg8xdGjR/uVUuVL2daTymH9+vUcOXLEaTE0Go3GU4jIxaVuq91KGo1Go7kKrRw0Go1GcxVaOWg0Go3mKrRy0Gg0Gs1VaOWg0Wg0mquIi3IQka+LSJ+INC6yXkTkn0WkXUROisiNUeseFJE28/NgPOTRaDQaTWzEy3L4JnDPNdbvB+rMz0PAvwKISAnwl8AtwF7gL0WkOE4yaTQajWaFxEU5KKV+DgxeY5P7gG8pg0NAkYhUA28HnldKDSqlhoDnubaS0djIpYFx/uPVS5zsHHZaFI3JxHSYl5r7eOrwJaZnI06LowGUUrT3B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SqnHlFJ7lFJ7ysvjr81TgWOXhlhdlEP
2018-03-21 17:42:59 +01:00
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
2018-03-21 17:42:59 +01:00
"output_type": "display_data"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"plt.plot(x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us quickly generate some random data and draw a scatter plot."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2019-12-05 12:27:39 +01:00
"<matplotlib.collections.PathCollection at 0x7ff892db3240>"
2018-03-21 17:42:59 +01:00
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2019-12-05 12:27:39 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAfH0lEQVR4nO3df5Ac9Xnn8fejZW0v2PGKoBC0IIuKOXG2KdBli5DC5wJsIyzjoCPOBYpzQYxPjs+us10+xeLOFRJyVyilihPfcWei2JxxHcHEQWw4QyyrDFUYn3+tWHECgw5i41gjgkRgMZh1vJKe+2N7xOyoe7Z7uqf72z2fV9XW7vTMzjwz0/30t5/vt79t7o6IiDTXsqoDEBGRwVKiFxFpOCV6EZGGU6IXEWk4JXoRkYY7ruoA4px00km+evXqqsMQEamNXbt2PevuK+LuCzLRr169munp6arDEBGpDTP7UdJ9Kt2IiDTckonezE4zs/vN7Ptm9qiZfTRafqKZ7TSzJ6LfyxP+/+roMU+Y2dVFvwEREektTYv+EPAJd38TcB7wYTN7E7AZ+Lq7nwF8Pbq9iJmdCFwP/BpwLnB90g5BREQGY8lE7+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/ycxOBK4HJllo+Owys7vd/fm8gYtUIaksYCxu2Q97C7JqOjpeLFWL3t0fAJ7rWnwZcGv0963Ahph/XQfsdPfnouS+E7ikz1iF5o3uqJuk0ShXnbcqiBak1g+Jk6dGf7K7Px39/Q/AyTGPmQB+3HF7X7TsGGa2EdgIsGrVqhxhNZdOS69eyJ3madcPnUw0fArpjHV3N7OkPqm0z7EN2AYwOTmZ67maSmfPhiHUskCa9UO
2018-03-21 17:42:59 +01:00
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
2018-03-21 17:42:59 +01:00
"output_type": "display_data"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"x = np.random.normal(42, 3, 100)\n",
"y = np.random.gamma(7, 1, 100)\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"plt.scatter(x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## Case Study (continued): Importing the Iris data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-03-21 17:37:55 +01:00
"The sklearn library provides several sample datasets, among which is also the Iris dataset.\n",
"\n",
"As a table, the dataset would look like:\n",
"<img src=\"raw/iris.png\" width=\"50%\">\n",
2018-03-21 17:37:55 +01:00
"\n",
"However, the data object imported from sklearn is organized slightly different. In particular, the so-called **features** are seperated from the **labels**."
]
},
{
"cell_type": "code",
2018-03-21 17:42:59 +01:00
"execution_count": 18,
2018-03-21 17:37:55 +01:00
"metadata": {},
"outputs": [],
"source": [
2019-12-05 12:27:39 +01:00
"from sklearn.datasets import load_iris"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
2018-03-21 17:37:55 +01:00
"iris = load_iris()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using Python's **dir()** function we can inspect the data object, i.e. find out what **attributes** it has."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 20,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['DESCR', 'data', 'feature_names', 'filename', 'target', 'target_names']"
2018-03-21 17:42:59 +01:00
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 20,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"dir(iris)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"iris.data provides us with a Numpy array, where the first dimension equals the number of observed flowers (**instances**) and the second dimension lists the various features of a flower."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 21,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[5.1, 3.5, 1.4, 0.2],\n",
" [4.9, 3. , 1.4, 0.2],\n",
" [4.7, 3.2, 1.3, 0.2],\n",
" [4.6, 3.1, 1.5, 0.2],\n",
" [5. , 3.6, 1.4, 0.2],\n",
" [5.4, 3.9, 1.7, 0.4],\n",
" [4.6, 3.4, 1.4, 0.3],\n",
" [5. , 3.4, 1.5, 0.2],\n",
" [4.4, 2.9, 1.4, 0.2],\n",
" [4.9, 3.1, 1.5, 0.1],\n",
" [5.4, 3.7, 1.5, 0.2],\n",
" [4.8, 3.4, 1.6, 0.2],\n",
" [4.8, 3. , 1.4, 0.1],\n",
" [4.3, 3. , 1.1, 0.1],\n",
" [5.8, 4. , 1.2, 0.2],\n",
" [5.7, 4.4, 1.5, 0.4],\n",
" [5.4, 3.9, 1.3, 0.4],\n",
" [5.1, 3.5, 1.4, 0.3],\n",
" [5.7, 3.8, 1.7, 0.3],\n",
" [5.1, 3.8, 1.5, 0.3],\n",
" [5.4, 3.4, 1.7, 0.2],\n",
" [5.1, 3.7, 1.5, 0.4],\n",
" [4.6, 3.6, 1. , 0.2],\n",
" [5.1, 3.3, 1.7, 0.5],\n",
" [4.8, 3.4, 1.9, 0.2],\n",
" [5. , 3. , 1.6, 0.2],\n",
" [5. , 3.4, 1.6, 0.4],\n",
" [5.2, 3.5, 1.5, 0.2],\n",
" [5.2, 3.4, 1.4, 0.2],\n",
" [4.7, 3.2, 1.6, 0.2],\n",
" [4.8, 3.1, 1.6, 0.2],\n",
" [5.4, 3.4, 1.5, 0.4],\n",
" [5.2, 4.1, 1.5, 0.1],\n",
" [5.5, 4.2, 1.4, 0.2],\n",
" [4.9, 3.1, 1.5, 0.2],\n",
2018-03-21 17:42:59 +01:00
" [5. , 3.2, 1.2, 0.2],\n",
" [5.5, 3.5, 1.3, 0.2],\n",
" [4.9, 3.6, 1.4, 0.1],\n",
2018-03-21 17:42:59 +01:00
" [4.4, 3. , 1.3, 0.2],\n",
" [5.1, 3.4, 1.5, 0.2],\n",
" [5. , 3.5, 1.3, 0.3],\n",
" [4.5, 2.3, 1.3, 0.3],\n",
" [4.4, 3.2, 1.3, 0.2],\n",
" [5. , 3.5, 1.6, 0.6],\n",
" [5.1, 3.8, 1.9, 0.4],\n",
" [4.8, 3. , 1.4, 0.3],\n",
" [5.1, 3.8, 1.6, 0.2],\n",
" [4.6, 3.2, 1.4, 0.2],\n",
" [5.3, 3.7, 1.5, 0.2],\n",
" [5. , 3.3, 1.4, 0.2],\n",
" [7. , 3.2, 4.7, 1.4],\n",
" [6.4, 3.2, 4.5, 1.5],\n",
" [6.9, 3.1, 4.9, 1.5],\n",
" [5.5, 2.3, 4. , 1.3],\n",
" [6.5, 2.8, 4.6, 1.5],\n",
" [5.7, 2.8, 4.5, 1.3],\n",
" [6.3, 3.3, 4.7, 1.6],\n",
" [4.9, 2.4, 3.3, 1. ],\n",
" [6.6, 2.9, 4.6, 1.3],\n",
" [5.2, 2.7, 3.9, 1.4],\n",
" [5. , 2. , 3.5, 1. ],\n",
" [5.9, 3. , 4.2, 1.5],\n",
" [6. , 2.2, 4. , 1. ],\n",
" [6.1, 2.9, 4.7, 1.4],\n",
" [5.6, 2.9, 3.6, 1.3],\n",
" [6.7, 3.1, 4.4, 1.4],\n",
" [5.6, 3. , 4.5, 1.5],\n",
" [5.8, 2.7, 4.1, 1. ],\n",
" [6.2, 2.2, 4.5, 1.5],\n",
" [5.6, 2.5, 3.9, 1.1],\n",
" [5.9, 3.2, 4.8, 1.8],\n",
" [6.1, 2.8, 4. , 1.3],\n",
" [6.3, 2.5, 4.9, 1.5],\n",
" [6.1, 2.8, 4.7, 1.2],\n",
" [6.4, 2.9, 4.3, 1.3],\n",
" [6.6, 3. , 4.4, 1.4],\n",
" [6.8, 2.8, 4.8, 1.4],\n",
" [6.7, 3. , 5. , 1.7],\n",
" [6. , 2.9, 4.5, 1.5],\n",
" [5.7, 2.6, 3.5, 1. ],\n",
" [5.5, 2.4, 3.8, 1.1],\n",
" [5.5, 2.4, 3.7, 1. ],\n",
" [5.8, 2.7, 3.9, 1.2],\n",
" [6. , 2.7, 5.1, 1.6],\n",
" [5.4, 3. , 4.5, 1.5],\n",
" [6. , 3.4, 4.5, 1.6],\n",
" [6.7, 3.1, 4.7, 1.5],\n",
" [6.3, 2.3, 4.4, 1.3],\n",
" [5.6, 3. , 4.1, 1.3],\n",
" [5.5, 2.5, 4. , 1.3],\n",
" [5.5, 2.6, 4.4, 1.2],\n",
" [6.1, 3. , 4.6, 1.4],\n",
" [5.8, 2.6, 4. , 1.2],\n",
" [5. , 2.3, 3.3, 1. ],\n",
" [5.6, 2.7, 4.2, 1.3],\n",
" [5.7, 3. , 4.2, 1.2],\n",
" [5.7, 2.9, 4.2, 1.3],\n",
" [6.2, 2.9, 4.3, 1.3],\n",
" [5.1, 2.5, 3. , 1.1],\n",
" [5.7, 2.8, 4.1, 1.3],\n",
" [6.3, 3.3, 6. , 2.5],\n",
" [5.8, 2.7, 5.1, 1.9],\n",
" [7.1, 3. , 5.9, 2.1],\n",
" [6.3, 2.9, 5.6, 1.8],\n",
" [6.5, 3. , 5.8, 2.2],\n",
" [7.6, 3. , 6.6, 2.1],\n",
" [4.9, 2.5, 4.5, 1.7],\n",
" [7.3, 2.9, 6.3, 1.8],\n",
" [6.7, 2.5, 5.8, 1.8],\n",
" [7.2, 3.6, 6.1, 2.5],\n",
" [6.5, 3.2, 5.1, 2. ],\n",
" [6.4, 2.7, 5.3, 1.9],\n",
" [6.8, 3. , 5.5, 2.1],\n",
" [5.7, 2.5, 5. , 2. ],\n",
" [5.8, 2.8, 5.1, 2.4],\n",
" [6.4, 3.2, 5.3, 2.3],\n",
" [6.5, 3. , 5.5, 1.8],\n",
" [7.7, 3.8, 6.7, 2.2],\n",
" [7.7, 2.6, 6.9, 2.3],\n",
" [6. , 2.2, 5. , 1.5],\n",
" [6.9, 3.2, 5.7, 2.3],\n",
" [5.6, 2.8, 4.9, 2. ],\n",
" [7.7, 2.8, 6.7, 2. ],\n",
" [6.3, 2.7, 4.9, 1.8],\n",
" [6.7, 3.3, 5.7, 2.1],\n",
" [7.2, 3.2, 6. , 1.8],\n",
" [6.2, 2.8, 4.8, 1.8],\n",
" [6.1, 3. , 4.9, 1.8],\n",
" [6.4, 2.8, 5.6, 2.1],\n",
" [7.2, 3. , 5.8, 1.6],\n",
" [7.4, 2.8, 6.1, 1.9],\n",
" [7.9, 3.8, 6.4, 2. ],\n",
" [6.4, 2.8, 5.6, 2.2],\n",
" [6.3, 2.8, 5.1, 1.5],\n",
" [6.1, 2.6, 5.6, 1.4],\n",
" [7.7, 3. , 6.1, 2.3],\n",
" [6.3, 3.4, 5.6, 2.4],\n",
" [6.4, 3.1, 5.5, 1.8],\n",
" [6. , 3. , 4.8, 1.8],\n",
" [6.9, 3.1, 5.4, 2.1],\n",
" [6.7, 3.1, 5.6, 2.4],\n",
" [6.9, 3.1, 5.1, 2.3],\n",
" [5.8, 2.7, 5.1, 1.9],\n",
" [6.8, 3.2, 5.9, 2.3],\n",
" [6.7, 3.3, 5.7, 2.5],\n",
" [6.7, 3. , 5.2, 2.3],\n",
" [6.3, 2.5, 5. , 1.9],\n",
" [6.5, 3. , 5.2, 2. ],\n",
" [6.2, 3.4, 5.4, 2.3],\n",
" [5.9, 3. , 5.1, 1.8]])"
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 21,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"iris.data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To find out what the four features are, we can list them:"
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 22,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['sepal length (cm)',\n",
" 'sepal width (cm)',\n",
" 'petal length (cm)',\n",
" 'petal width (cm)']"
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 22,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"iris.feature_names"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, we can also print the flowers' labels (a.k.a. targets):"
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 23,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
" 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
" 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])"
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 23,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"iris.target"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The three flower classes are encoded with integers. Let's show the corresponding names:"
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 24,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['setosa', 'versicolor', 'virginica'], dtype='<U10')"
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 24,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"iris.target_names"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## Case Study (continued): Simple Visualizations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-03-21 17:37:55 +01:00
"Since the data is four dimensional, we cannot visualize all features together. Instead, we can plot the distribution of the flower classes by a single feature using histograms."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 25,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
2019-12-05 12:27:39 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAEGCAYAAAB8Ys7jAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAZxUlEQVR4nO3df3RU9bnv8fcDiYYItShpjYQIPS0gEDAQBY+noiDVo6nWpVzai1agLkQUtVot2lawq7ftWbdLj+teqkVFvMpBadD21NVbBQ7Uq6VKkoLhh4hi1ATkZ0VAfhh57h+zoUmA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M5gMXAT3MrAaYQeKsk5OBRdFdlP/q7lNiqUhEpAXGjh1LWVkZH330EePGjeP999/n3nvv5aabbmowX3V1NaeccsqR1927d2fVqlW89NJLPProoyxYsIA5c+a0er31a0hXMmehfMfd8909290L3P0Jd/+qu/dy93Oih8JbRDJi3LhxPPvss5SVlTF27FguvfRS5syZw549ewCora1l69atRy23fft2Dh06xDXXXMPPfvYzKisrG7zfr18/Nm/ezIoVKwDYvXs3dXV1fP3rX2fevMTHfm+//TYffPAB/fr1a7Bs/XmWLVtGjx49jhzBx0ntZEUkPhlo4zlw4EB2795Nz549yc/PJz8/n3Xr1nH++ecD0LVrV5555hk6d+7cYLna2lomTpzIoUOHAPjFL37R4P2TTjqJ5557jmnTprFv3z66dOnC4sWLmTp1KjfffDNFRUVkZWUxd+5cTj755AbLzpw5k0mTJjF48GByc3NbrX+42skeh9rJSuja4iwUtZONn9rJioicABTgIiKBUoCLiARKAS4iEigFuIhIoBTgIiKB0nngIhKbVE5dbEoqzbU2bdrEbbfdRllZWYuWu/HGG7nzzjsZMGDAced59NFHyc3NbdDbJJMU4CLSoZx55pnHDO/m2rc+/vjjza57ypT2ddG5hlBEJFjTp09n1qxZR17PnDmTX/3qVwwaNAiAuXPncuWVVzJq1ChGjx7NoUOHmDp1Kv3792fMmDFcfvnlR8L+oosu4vCFhl27duVHP/oRQ4YMYcSIEWzZsqXB+gHeeecdLrnkEoYMGcLQoUN599132bNnD6NHj2bo0KEUFRXx+9//vlX3XwEuIsE6VjvZ4cOHN5insrKSsrIy/vznP/P8889TXV3N2rVrefrpp1m+fPkx17t3715GjBjBqlWruPDCC3nssceOmmf8+PHccsstrFq1ir/85S/k5+eTk5PDCy+8QGVlJUuXLuWuu+6iNa921xCKiATrWO1ke/Xq1WCeMWPGcNpppwGJNq9jx46lU6dOnHHGGUfdiOGwk0466cjt0YYNG8aiRYsavL97925qa2u5+uqrAcjJyQHgs88+47777uOVV16hU6dO1NbWsmXLFs4444xY9/swBbiIBK1xO9nGUmnfmp2dTdQqm86dO1NXV9fMEgnz5s1j27ZtVFRUkJ2dTe/evY+62UOcNIQiIkFr3E62KRdccAELFy7k0KFDbNmyhWXLlqW0zW7dulFQUMDvfvc7AA4cOMCnn37Krl27+NKXvkR2djZLly7l/fffT2n9ydIRuIjEJhP31GzcTra6uvq4815zzTUsWbKEAQMG0KtXL4YOHcqpp56a0naffvppbrrpJu6//36ys7P57W9/y/jx4/nmN79JUVERJSUl9O/fP8W9So7ayR6H2slK6NRO9tj27NlD165d2bFjB+eddx6vvfZaq41Rp6Il7WR1BC4iJ5TS0lI+/vhjDh48yE9+8pN2Fd4tpQAXkRNKquPe7ZE+xBSRtLTlMGxH19LvpQJcRFK
2018-03-21 17:42:59 +01:00
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
2018-03-21 17:42:59 +01:00
"output_type": "display_data"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"feature_index = 2\n",
"colors = ['blue', 'red', 'green']\n",
"\n",
"for label, color in zip(range(len(iris.target_names)), colors):\n",
" plt.hist(iris.data[iris.target==label, feature_index], \n",
" label=iris.target_names[label],\n",
" color=color)\n",
"\n",
"plt.xlabel(iris.feature_names[feature_index])\n",
"plt.legend(loc='upper right')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also, we can draw scatter plots of two features."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 26,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
2019-12-05 12:27:39 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEGCAYAAABhMDI9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAgAElEQVR4nO3de3wU9bn48c+TTShEF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TlpksIh3dlpmTr/rsliq/dpZ5t6dPn87tt9/OwoULmTZtGscffzw33XQTW7duBWDDhg28+eabPdZ766236Ozs5LTTTuOHP/why5Yti/t8+PDhvPbaazzzzDMAvPfee+zcuZPPf/7ztLqPv1544QXWrVvH8OHD49btvszixYvZb7/9etyRRJ31gDHGu7zdMajqGmAMgIjEgA3APUkWfUxVs+9fmak85d0eMWIE7733HgcccACDBw9m8ODBrF69mokTJwLQv39/WlpaiMXi3w/YsGED55xzzu7eST/+8Y/jPu/Tpw933HEH559/Ptu3b6dfv34sWrSIxsZGGhoaqKmpoby8nJtvvplPfOITcevOmzePr371q4waNYrKysqSHL+hq6fL7Idns65jHUMHDKWptsl6wBiTRiBpt0Xk34C5qjopoXwycLGXhiEXabct73bvLO22MdESxrTbZwCpur9MFJEVwEacRqLHIMYiUg/UAwzNdqg1sLzbxhiTRt57JYlIH2Aq8LskHy8DqlR1NPAL4PfJtqGqzao
2018-03-21 17:42:59 +01:00
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
2018-03-21 17:42:59 +01:00
"output_type": "display_data"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"first_feature_index = 1\n",
"second_feature_index = 0\n",
"\n",
"colors = ['blue', 'red', 'green']\n",
"\n",
"for label, color in zip(range(len(iris.target_names)), colors):\n",
" plt.scatter(iris.data[iris.target==label, first_feature_index], \n",
" iris.data[iris.target==label, second_feature_index],\n",
" label=iris.target_names[label],\n",
" c=color)\n",
"\n",
"plt.xlabel(iris.feature_names[first_feature_index])\n",
"plt.ylabel(iris.feature_names[second_feature_index])\n",
"plt.legend(loc='upper left')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the higher level library pandas, one can easily create a so-called **scatterplot matrix**."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 27,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
2019-12-05 12:27:39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Od7sjXB9LMnBdUHqfQtLE3srdd90GSCE+DkhhH/a+4AQ4mcrOO6dQogfFF+DQognK5LewGAFOH59gmg6z/Vwks5wsmwZs0lhU52HkLv8wF0pHruFLfVew9r2LsRqVthY4+HVa2N0jiY53j3BpjoPbVUuY1BeBmq8djbWelAUMWeZpoDzlgblZLbA9y6O0Dma5PsXRxclX6V1V6Kf+6SUMjL5Rko5AXzyZgdJKb8jpXxQSvkg0AP8ewV1GRisCFXFwdasCAJOw6/ZYPlQFEGwaJ9Q5bl1OwWD1cVqVvA69GdElXtl2q8S/ZxJCCFk0bZcCGECKpZOCLEeGJZSrkwiSwODCtjR5KPWa8NmMeFzlB+Ye8dTvHh5lHqfnYc31yDE3LNwgzuD0XiWf78wjNdu4dFttZjnsZy9FX58fzOjiSy1c2ybGNycl6+Mcn0sxb3tIdpvYcW7WCwmhQ/f08J4Mkedd2W8Kirpdd8B/kUI8YgQ4hHgC8XPKuVp4KsLEc7AYDmp8drnHJQB3ugaZzSe5UxflNFEds5yBncOJ3smGIpmuDwcp2cJk0xYzQqNfseSDfR3G/FMnuPXJwjHs7xybWzF67dbTDT4HfOqyJeSSnrJr6EHGPkPxdcLwH++hTqeAG7I2SyE+JQQ4rgQ4vjo6OL09gYGy0FblR4NKOC0zDuAG9w5tIacCAEum4lqY3W7ZnBazdQWV6vrQnf+3nwlVtka8JfF1y0hhKgDclLKG6Y4UsrPAp8F2L9//9oNwWJwV5DLqfz03x+nL5LimXta+cSR9XjsFoQAt92MWTFWOncDm+u8tASdWEwKFpPC9y4Oc2EwzoG2IJvrPXz1ZD8FTfL
2018-03-21 17:42:59 +01:00
"text/plain": [
"<Figure size 576x576 with 16 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
2018-03-21 17:42:59 +01:00
"output_type": "display_data"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)\n",
2019-12-05 12:27:39 +01:00
"\n",
"pd.plotting.scatter_matrix(iris_df, figsize=(8, 8));"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## Concept of Generalization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-03-21 17:37:55 +01:00
"The goal of a supervised machine learning model is to make predictions on new (i.e., previously unseen) data.\n",
"\n",
"In a real-world application, we are not interested in marking an already labeled email as spam or not. Instead, we want to make the user's life easier by automatically classifying new incoming mail.\n",
"\n",
"In order to get an idea of how good a model generalizes, a best practice is to split the available data into a training and a test set. Only the former is used to train the model. Then predictions are made on the test data and the predictions can be compared with the actual labels.\n",
"\n",
2019-12-05 12:27:39 +01:00
"Common splits are 75/25 or 60/40."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"raw/generalization.png\" width=\"60%\">"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## Case Study (continued): Train/Test Split for the Iris data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-03-21 17:37:55 +01:00
"It is common practice to refer to the feature matrix as X and the vector of labels as y."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 28,
2018-03-21 17:37:55 +01:00
"metadata": {},
"outputs": [],
"source": [
"X, y = iris.data, iris.target"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A naive splitting approach could be to use array slicing."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 29,
2018-03-21 17:37:55 +01:00
"metadata": {},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = X[0:100, :], X[100:150, :], y[0:100], y[100:150]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, this would lead to unbalanced label distributions. For example, the test set would only be made up of flowers of the same type."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 30,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
" 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
" 2, 2, 2, 2, 2, 2])"
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 30,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"y_test"
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 31,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 0, 50])"
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 31,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"np.bincount(y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"sklearn provides a function that not only randomizes the split but also ensures that the resulting label distribution is proportionate to the overall distribution (called **stratification**)."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": 33,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2019-12-05 12:27:39 +01:00
"array([1, 0, 1, 2, 2, 2, 2, 1, 0, 1, 0, 0, 2, 0, 0, 1, 0, 1, 2, 2, 0, 2,\n",
" 1, 2, 0, 0, 2, 2, 1, 2, 1, 1, 0, 0, 1, 1, 1, 0, 1, 2, 0, 0, 2, 2,\n",
" 1])"
2018-03-21 17:42:59 +01:00
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 33,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, test_size=0.3, stratify=y)\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"y_test"
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 34,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([15, 15, 15])"
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 34,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"np.bincount(y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## A simple Classification Model: k-Nearest Neighbors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To predict the label for any observation, just determine the k \"nearest\" observations in the training set (e.g., by Euclidean distance) and use a simple majority vote."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"raw/knn.png\" width=\"60%\">"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## Case Study (continued): Train and Predict the Iris data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2018-03-21 17:37:55 +01:00
"sklearn provides a uniform interface for all its classification models. They all have a **fit()** and a **predict()** method that abstract away the actual machine learning algorithm."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.neighbors import KNeighborsClassifier"
]
},
{
"cell_type": "code",
"execution_count": 36,
2018-03-21 17:37:55 +01:00
"metadata": {},
"outputs": [],
"source": [
"knn = KNeighborsClassifier(n_neighbors=5)\n",
"\n",
"knn.fit(X_train, y_train)\n",
"\n",
"y_pred = knn.predict(X_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us list the labels predicted for the test set ..."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 37,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2019-12-05 12:27:39 +01:00
"array([1, 0, 1, 1, 2, 2, 2, 1, 0, 1, 0, 0, 2, 0, 0, 1, 0, 1, 2, 2, 0, 2,\n",
" 1, 1, 0, 0, 1, 2, 1, 2, 1, 1, 0, 0, 1, 1, 1, 0, 1, 2, 0, 0, 2, 2,\n",
" 1])"
2018-03-21 17:42:59 +01:00
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 37,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"y_pred"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... and compare them with the actual labels."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 38,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2019-12-05 12:27:39 +01:00
"array([1, 0, 1, 2, 2, 2, 2, 1, 0, 1, 0, 0, 2, 0, 0, 1, 0, 1, 2, 2, 0, 2,\n",
" 1, 2, 0, 0, 2, 2, 1, 2, 1, 1, 0, 0, 1, 1, 1, 0, 1, 2, 0, 0, 2, 2,\n",
" 1])"
2018-03-21 17:42:59 +01:00
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 38,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"y_test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy can show us the indices where the predictions are wrong."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 39,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2019-12-05 12:27:39 +01:00
"(array([ 3, 23, 26]),)"
2018-03-21 17:42:59 +01:00
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 39,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"np.where(y_pred != y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, we can calculate the fraction of correctly predicted flowers."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 40,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2019-12-05 12:27:39 +01:00
"0.9333333333333333"
2018-03-21 17:42:59 +01:00
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 40,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"np.sum(y_pred == y_test) / len(y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is important to mention that we can also \"predict\" the training set. Surprisingly, the model does not get the training set 100% correct."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 41,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2019-12-05 12:27:39 +01:00
"0.9523809523809523"
2018-03-21 17:42:59 +01:00
]
},
2019-12-05 12:27:39 +01:00
"execution_count": 41,
2018-03-21 17:42:59 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"y_train_pred = knn.predict(X_train)\n",
2019-12-05 12:27:39 +01:00
"\n",
2018-03-21 17:37:55 +01:00
"np.sum(y_train_pred == y_train) / len(y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A visualization reveals that the misclassified flowers are right \"at the borderline\" between two neighboring clusters of flower classes."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 42,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"data": {
2019-12-05 12:27:39 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEWCAYAAABi5jCmAAAABHNCS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+cd999l4yMDNq0acNVV13Fiy++SHp6OuB8Z+Pss88mJyeH+++/n88++6xJcRtjQpORkRFSebjHxFLSJYYNG3aFVB6s7t27s2LFCnJycvjDH/7ACy+8QO/evVm5ciUrV66krKyMhQsXBl1famoqH330EWPGjOHVV1/lnHPOAeDGG2/khhtuoKysjD//+c/2fQFjoiwvL++gb/WnpaWRl5fXrMfEUtIlhszMdiGVB2vLli2kp6dz6aWXcuutt/Lhhx9SVVXFBx98ADjtiZ5P94cffji7d+8GoEePHlRUVLB+/XoAZs+ezWmnncaePXuorq7mvPPO46GHHmLVqlWAc+vZpUsXAGbNmtWkmI0xocvJyWHUqFF1n/YzMjIYNWpUwBFG4RwTS0nXx1BUNNynjwEgPT2VoqLhTaq3rKyMW2+9lZSUFNLS0njiiSdITU3lpptuorq6mn379nHzzTfTu3dvJkyYwLXXXkvbtm354IMPeOaZZxg7diz79u1jyJAhXHvttezYsYMLL7yQvXv3oqo8+OCDANx1112MHTuW9u3bc+aZZ/L11183KW5jTOhycnJCflMP55hYEVWNdQwhGTx4sNZfqOfzzz/nF7/4RdB1lJSUU1i4hA0bdpGZ2Y6iouFN6ng2Bwv1d2KMiSwRWa6qg4PZN+nuGMAZfWSJwBhj/Eu6PgZjjDGBRSwxiEgPEVnp9dglIjfX2+d0Ean22ueOSMVjjDEmOBFrSlLVtUB/ABFpBWw
2018-03-21 17:42:59 +01:00
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
2018-03-21 17:42:59 +01:00
"output_type": "display_data"
}
],
2018-03-21 17:37:55 +01:00
"source": [
"first_feature_index = 3\n",
"second_feature_index = 0\n",
"\n",
"correct_idx = np.where(y_pred == y_test)[0]\n",
"incorrect_idx = np.where(y_pred != y_test)[0]\n",
"\n",
"colors = [\"darkblue\", \"darkgreen\", \"gray\"]\n",
"\n",
"for n, color in enumerate(colors):\n",
" idx = np.where(y_test == n)[0]\n",
" plt.scatter(X_test[idx, first_feature_index], X_test[idx, second_feature_index], color=color,\n",
" label=iris.target_names[n])\n",
"\n",
"plt.scatter(X_test[incorrect_idx, first_feature_index], X_test[incorrect_idx, second_feature_index],\n",
" color=\"darkred\", label='misclassified')\n",
"\n",
"plt.xlabel('sepal width [cm]')\n",
"plt.ylabel('petal length [cm]')\n",
"plt.legend(loc='best')\n",
"plt.title(\"Iris Classification results\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In practice, the number of neighbors is to chosen before the model is trained. Therefore, it is possible to \"optimize\" it. This process is referred to as **hyper-parameter** tuning. For the Iris dataset this does not make much of a difference."
]
},
{
"cell_type": "code",
2019-12-05 12:27:39 +01:00
"execution_count": 43,
2018-03-21 17:42:59 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2019-12-05 12:27:39 +01:00
"1 0.9777777777777777\n",
"2 0.9333333333333333\n",
"3 0.9555555555555556\n",
2019-12-05 12:27:39 +01:00
"4 0.8888888888888888\n",
"5 0.9333333333333333\n",
"6 0.9333333333333333\n",
"7 0.9333333333333333\n",
"8 0.9333333333333333\n",
"9 0.9555555555555556\n",
"10 0.9333333333333333\n",
2019-12-05 12:27:39 +01:00
"11 0.9555555555555556\n",
"12 0.9333333333333333\n",
"13 0.9333333333333333\n",
"14 0.9333333333333333\n",
"15 0.9333333333333333\n",
"16 0.9333333333333333\n",
"17 0.9333333333333333\n",
"18 0.9333333333333333\n",
"19 0.9333333333333333\n",
"20 0.9333333333333333\n",
"21 0.9333333333333333\n",
2019-12-05 12:27:39 +01:00
"22 0.9333333333333333\n",
"23 0.9111111111111111\n",
2019-12-05 12:27:39 +01:00
"24 0.9333333333333333\n",
"25 0.9111111111111111\n",
2019-12-05 12:27:39 +01:00
"26 0.9333333333333333\n",
"27 0.9111111111111111\n",
"28 0.9111111111111111\n",
2019-12-05 12:27:39 +01:00
"29 0.9333333333333333\n",
"30 0.9333333333333333\n"
2018-03-21 17:42:59 +01:00
]
}
],
2018-03-21 17:37:55 +01:00
"source": [
"for i in range(1, 31):\n",
" knn = KNeighborsClassifier(n_neighbors=i)\n",
" knn.fit(X_train, y_train)\n",
" y_pred = knn.predict(X_test)\n",
" correct = np.sum(y_pred == y_test) / len(y_test)\n",
" print(i, correct)"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"## WHU's Python Course in the BSc program"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- free [online book](https://github.com/webartifex/intro-to-python) by the author of this workshop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Literature on Machine Learning"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Depending on the programming language one chooses, the following books are recommended:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [Python Machine Learning](https://www.amazon.de/Python-Machine-Learning-scikit-learn-TensorFlow/dp/1787125939/ref=sr_1_1?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&keywords=python+machine+learning&qid=1575545025&sr=8-1) by Sebastian Raschka\n",
2018-03-21 17:37:55 +01:00
"\n",
2019-12-05 12:27:39 +01:00
"<img src=\"raw/python_ml.png\">"
2018-03-21 17:37:55 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-12-05 12:27:39 +01:00
"- [An Introduction to Statistical Learning](http://faculty.marshall.usc.edu/gareth-james/ISL/)\n",
"\n",
"<img src=\"raw/r.png\">"
]
2018-03-21 17:37:55 +01:00
}
],
"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",
2019-12-05 12:27:39 +01:00
"version": "3.6.9"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
2018-03-21 17:37:55 +01:00
}
},
"nbformat": 4,
"nbformat_minor": 2
}