1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-25 15:52:29 +00:00
sitc/ml1/2_3_0_Visualisation.ipynb

345 lines
74 KiB
Plaintext
Raw Normal View History

2016-03-15 12:55:14 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](files/images/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [Introduction to Machine Learning](2_0_0_Intro_ML.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Table of Contents\n",
"* [Visualisation](#Visualisation)\n",
"* [Exploratory visualisation](#Exploratory-visualisation)\n",
"* [References](#References)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualisation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The goal of this notebook is to learn how to analyse a dataset. We will cover other tasks such as cleaning or munging (changing the format) the dataset in other sessions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exploratory visualisation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-03-28 10:26:20 +00:00
"This section covers different ways to inspect the distribution of samples per feature.\n",
"\n",
2017-12-11 17:12:06 +00:00
"First of all, let's see how many samples of each class we have, using a [histogram](https://en.wikipedia.org/wiki/Histogram). \n",
2016-03-28 10:26:20 +00:00
"\n",
2017-12-11 17:12:06 +00:00
"A histogram is a graphical representation of the distribution of numerical data. It is an estimation of the probability distribution of a continuous variable (quantitative variable). \n",
2016-03-28 10:26:20 +00:00
"\n",
"For building a histogram, we need first to 'bin' the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval. \n",
"\n",
"In our case, since the values are not continuous and we have only three values, we do not need to bin them."
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "code",
2017-04-20 14:07:10 +00:00
"execution_count": 1,
2018-02-27 15:14:18 +00:00
"metadata": {},
2016-03-15 12:55:14 +00:00
"outputs": [],
"source": [
"from sklearn import datasets\n",
"iris = datasets.load_iris()"
]
},
{
"cell_type": "code",
2018-02-27 15:14:18 +00:00
"execution_count": 2,
"metadata": {},
"outputs": [],
2016-03-15 12:55:14 +00:00
"source": [
"# library for displaying plots\n",
"import matplotlib.pyplot as plt\n",
"# display plots in the notebook\n",
"# if this is not set, you will not see the graphic here\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
2018-02-27 15:14:18 +00:00
"execution_count": 3,
"metadata": {},
2016-03-15 12:55:14 +00:00
"outputs": [
{
"data": {
2018-02-27 15:14:18 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAFk1JREFUeJzt3Xm4JXV95/H3hwYEZRNo2waERkUS3LEh7kHckuDCKEISh4AyEjMmg6KOxHEhYhRkMBEziRA1tIkGEBdaxCBBFqOodAORRVBCYAIBaUeURUWW7/xRdYvrpft29e2uc27ffr+e5zyn9vqe7rrnc2r7VaoKSZIANhp3AZKk2cNQkCR1DAVJUsdQkCR1DAVJUsdQkCR1DAVJUsdQkCR1DAVJUmfjcRfQx/bbb1+LFi0adxmStF5Zvnz5j6pq/prMs16EwqJFi1i2bNm4y5Ck9UqSG9d0Hg8fSZI6hoIkqWMoSJI6hoIkqWMoSJI6g159lOQG4E7gfuC+qlqcZFvgNGARcANwYFXdPmQdkqR+RrGn8IKqelpVLW77jwLOq6rdgPPafknSLDCOw0evBJa03UuA/cdQgyRpJYYOhQK+mmR5ksPbYQuq6pa2+1ZgwcA1SJJ6Gv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7h8Bvtt0rgM1Xt+AkAT4BfK+qPjxp1FLgEODY9v3MNSlYkjScVYZCVb1uLZf9HOBg4Iokl7fD3kkTBqcnOQy4EThwLdcjSVpHVnuiOcmuwJ8AiyZPv7qms6vqX4CsYvQL+5coSRqVPlcffZHmMNCXgAeGLUeSNE59QuEXVXXi4JVIksauTyh8JMl7ga8C90wMnLgxTZI0d/QJhSf
2016-03-15 12:55:14 +00:00
"text/plain": [
2018-02-27 15:14:18 +00:00
"<matplotlib.figure.Figure at 0x7fd9e04f14e0>"
2016-03-15 12:55:14 +00:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Plot histogram, the default is 10 bins\n",
2018-02-27 15:14:18 +00:00
"plt.hist(iris.target)\n",
"plt.ylabel('Number of instances')\n",
"plt.xlabel('iris class')\n",
"plt.xticks(range(len(iris.target_names)), iris.target_names);"
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2017-12-11 17:12:06 +00:00
"As can be seen, we have the same distribution of samples for every class.\n",
2016-03-28 10:26:20 +00:00
"The next step is to see the distribution of the features"
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "code",
2018-02-27 15:14:18 +00:00
"execution_count": 4,
"metadata": {},
2016-03-15 12:55:14 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n"
]
}
],
"source": [
2016-03-28 10:26:20 +00:00
"# This is a reminder of the name and index of each feature\n",
2016-03-15 12:55:14 +00:00
"print(iris.feature_names)"
]
},
{
"cell_type": "code",
2018-02-27 15:14:18 +00:00
"execution_count": 5,
"metadata": {},
2016-03-15 12:55:14 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['setosa' 'versicolor' 'virginica']\n"
]
}
],
"source": [
2016-03-28 10:26:20 +00:00
"# A reminder of feature names and indexes\n",
2016-03-15 12:55:14 +00:00
"print(iris.target_names)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-03-28 10:26:20 +00:00
"A [**scatter plot**](https://en.wikipedia.org/wiki/Scatter_plot) (*gráfico de dispersión*) displays the value of typically two variables for a set of data."
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "code",
2018-02-27 15:14:18 +00:00
"execution_count": 6,
"metadata": {},
2016-03-15 12:55:14 +00:00
"outputs": [
{
"data": {
"text/plain": [
2018-02-27 15:14:18 +00:00
"Text(0,0.5,'iris class')"
2016-03-15 12:55:14 +00:00
]
},
2018-02-27 15:14:18 +00:00
"execution_count": 6,
2016-03-15 12:55:14 +00:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2018-02-27 15:14:18 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAaYAAAEKCAYAAABZr/GWAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAHLNJREFUeJzt3X2UFfWd5/H3xwa0RZQYkRGUkChhj7FVsFckOAmOEpzoKlETZTWJSUaTOXmaccOcdSWJmWCIQ+JMVjNxfZpkRsdxJco4mpWQB4wxioGgoI74MD4g+EBGUVSigN/9o+q2t2/f7qqmb/f9QX9e53Do+tXv/upbv759P133VlcpIjAzM0vFLs0uwMzMrJqDyczMkuJgMjOzpDiYzMwsKQ4mMzNLioPJzMyS4mAyM7OkOJjMzCwpDiYzM0vKkGYXsCPaZ599Yvz48c0uw8xsh7JixYrfR8Soon4Opu0wfvx4li9f3uwyzMx2KJKeKtPPb+WZmVlSHExmZpYUB5OZmSXFwWRmZklxMJmZWVIcTGZmlpSmB5OkMZIWbsfjrpJ0cEGfz0n6xPZXZ2ZmA63pf8cUEeuB02rbJQ2JiK09PO7PSox9eR/LszoWrVzHgsVrWL9xM2NGtjJn5kRmTRrb7LL6RZl9Lep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9nGgsVrOpavX7a27mMr7WXGKNOnr3XWC5Tq9qL1ZfrUe5GtbS+ar3qhU91etL7MNhpRZ9GcFz2+TB2NeO709bll/a/MyQ8/Jzv5oaIV6PFznohYCewraYykw4CXgDbgQ8BK4HfAfyELJICnIuKe/Ov/AN4j6VJJxwOv1Aw/EXi
2016-03-15 12:55:14 +00:00
"text/plain": [
2018-02-27 15:14:18 +00:00
"<matplotlib.figure.Figure at 0x7fd9e04f1470>"
2016-03-15 12:55:14 +00:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# scatter makes a plot of x vs y\n",
"plt.scatter(iris.data[:,0], iris.target)\n",
2018-02-27 15:14:18 +00:00
"plt.yticks(range(len(iris.target_names)), iris.target_names);\n",
"plt.xlabel(iris.feature_names[0])\n",
"plt.ylabel('iris class')"
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "code",
2018-02-27 15:14:18 +00:00
"execution_count": 7,
2016-03-15 12:55:14 +00:00
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
2018-02-27 15:14:18 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzt3Xt0VeWd//H3NyGaqAhemBIBBTojjhI0GBWl0zqgxQqlqGTQ5Q219VaVTh1ntEXNOPzGztilQ3/j0vHSYpWhQBRGYdTWC9OfVrHhGhXxAigEUIQhggU15Pn9cZKQHHLY++Ts7LP3Pp/XWixz9tl5zvdsw5eTvT/7ecw5h4iIJEtRvgsQEZHgqbmLiCSQmruISAKpuYuIJJCau4hIAqm5i4gkkJq7iEgC+W7uZlZsZsvMbEEnz002sy1mtrzlz/eDLVNERLLRI4t9pwCrgEMzPD/bOXdD7iWJiEi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HYYcdxptvvsnZZ58NwJ49eygvL9/n+1avXs2gQYM49thjAbj88su5//77ueGGGygtLeWqq65i3LhxjBs3DoANGzYwadIkNm3axJdffrlPFj0K9MldRELXXddZjj32WJYuXUpFRQVTp07lySef5IQTTmD58uUsX76c+vp6fvvb3/oer0ePHrzxxhtMnDiRBQsWcM455wBw4403csMNN1BfX89//Md/RPKuXDV3EQldd11n2bhxIwcddBCXXHIJt9xyC4sXL2bLli289tprQGqKhLfeeguAnj17smPHDgCGDBnCunXreP/99wF4/PHH+da3vsXOnTtpbGzk3HPP5b777mPFihUANDY20q9f6jfNxx7rdBLcvNNpGREJXespuKDvSq6vr+eWW26hqKiIkpISHnjgAXr06MFNN91EY2MjTU1N/OhHP+KEE05g8uTJXHvttZSVlfHaa6/xq1/9iurqapqamjjllFO49tpr2bZtG9/73vfYvXs3zjnuvfdeAGpqaqiuruawww5j1KhRrF27NudjEjTNLSMigVi1ahV/+Zd/me8yEqWzY+p3bhmdlhERSSA1dxGRBFJzFxFJIDV3EZEEUnMXEUkgRSElZ1poQyR69MldctJdE0CJRMUdd9zBCy+8kPX3LVq0qG26gnzQJ3fJyf4mgNKnd9mvlXPgxbugcQP06g+j74Bhf5OXUpxzOOcoKtr38+5dd90VSg1NTU306BFcS9Ynd8mJFtqQLlk5B565CRrXAy7132duSm3Pwa233sr999/f9rimpoaf//zn3HPPPZxyyikMGzaMO++8E4B169YxZMgQLrvsMoYOHcr69euZPHkyQ4cOpaKigvvuuw+AyZMnU1tbC8Af//hHzjjjDE488UROPfVUduzYwe7du7niiiuoqKigsrKSl19+eZ+6tm3bxoQJExg2bBgjRoxg5cqVbfVdeumljBw5kksvvTSn955OzV1yooU2pEtevAu+SvsA8NWu1PYcTJo0iTlz9v4DMWfOHPr06cN7773HG2+8wfLly1myZAm///3vAXjvvfe4/vrreeutt/j0009paGjgzTffpL6+niuuuKLD2F9++SWTJk1i+vTprFixghdeeIGysjLuv/9+zIz6+npmzZrF5Zdfvs9EYnfeeSeV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E1FeP4kWrllIzR9q2PT5JhyOTZ9vouYPNSxcszCncTNN+Tt06FAAZsyYwfjx4xk1ahSjR4+mubmZ66+/nuOOO46zzz6bc889t+0fgjPPPJPWGy0POeQQfvrTn3LiiScyYsQIPv744w7jA7z//vucddZZnHjiiQwfPpwPPviAnTt3Mnr0aIYPH05FRQX/9V//ldP7y4bf0zL/Bvw90Jzh+X7AegDnXBPQCByRvpO
2016-03-15 12:55:14 +00:00
"text/plain": [
2018-02-27 15:14:18 +00:00
"<matplotlib.figure.Figure at 0x7fd9de43e400>"
2016-03-15 12:55:14 +00:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Plot the distribution of the dataset\n",
"names = set(iris.target)\n",
"\n",
"# x and y are all the samples from column 0 (sepal_length) and 1 (sepal_width) respectively\n",
"x,y = iris.data[:,0], iris.data[:,1]\n",
"\n",
"for name in names:\n",
" cond = iris.target == name\n",
" plt.plot(x[cond], y[cond], linestyle='none', marker='o', label=iris.target_names[name])\n",
"\n",
"plt.legend(numpoints=1)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-03-28 10:26:20 +00:00
"As we can see, the Setosa class seems to be linearly separable with these two features.\n",
2016-03-15 12:55:14 +00:00
"\n",
"Another nice visualisation is given below."
]
},
{
"cell_type": "code",
2018-02-27 15:14:18 +00:00
"execution_count": 8,
2016-03-15 12:55:14 +00:00
"metadata": {
2018-02-27 15:14:18 +00:00
"scrolled": true
2016-03-15 12:55:14 +00:00
},
"outputs": [
{
"data": {
2018-02-27 15:14:18 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAZIAAAEKCAYAAAA4t9PUAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzsnXecFEX2wL9v8mxmyTkI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2Q+4VFVPqHTaNOAk4BtgPPCxaopYH4ZGjdPl5OY3r2L6W3P47MWvcXvdHHDicPoO72UstgzbPfEmPNvIhHr3IxGRG4FZqjoNeAx4RkQWA+uBY+tbHgN8PvUbXr3/bTSmHH7uQYz6+z51Mo7T6WTvMXuw95g96qR/g6EuUIVI3CiSdNSLIlHVT4FPE5+vK1ceBI6qDxkM9py/51X8PGPxlu8/fP0zz096lSlz/51mg9xgaDpYS1vm30I6zN1pwkx7+L0KSmQzy35Yzv/d9loDSGQwbJ+YWFvpMYqkCfPqvW+lrHvzP+/XoyQGw/aLMf+tGhNrqwkTCqbOnBhOU2cwNC3M0lZVmLvThNl3/J4p67bXwI8GQ0MQT+Rtr+poqhhF0oQ59ebjyMrzJ5V7szyceceJDSCRwbD9YVltOTM6mipGkTRhPD4Pz//xX/Y7Zm982V68WR6GjR3Mc7//h+y8rIYWz2DYLjAOiVVj9kgaIZ+99DWv3vc22XlZnP/AabTt2rrGfWXl+Lnm+YtrUToIlYWY/tYcSjcF6Du8F+27t63V/g2NF40uhfAMcOSDdz+sTBJVtNEQhD6F+Cbw7IG4uta5nJVpystWmWAUSSMiFot
2016-03-15 12:55:14 +00:00
"text/plain": [
2018-02-27 15:14:18 +00:00
"<matplotlib.figure.Figure at 0x7fd9de38a278>"
2016-03-15 12:55:14 +00:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x_index = 0\n",
"y_index = 1\n",
"formatter = plt.FuncFormatter(lambda i, *args: iris.target_names[int(i)])\n",
"plt.scatter(iris.data[:, x_index], iris.data[:, y_index], s=40,\n",
"c=iris.target)\n",
"plt.colorbar(ticks=[0, 1, 2], format=formatter)\n",
"plt.xlabel(iris.feature_names[x_index])\n",
2018-02-27 15:14:18 +00:00
"plt.ylabel(iris.feature_names[y_index]);"
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-03-28 10:26:20 +00:00
"This alternate visualisation also suggests that the Setosa class seems to be linearly separable.\n",
2016-03-15 12:55:14 +00:00
"\n",
"Students interested in practicing advanced visualisations can check [Advanced visualisation notebook](2_3_1_Advanced_Visualisation.ipynb).\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# References"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* [Feature selection](http://scikit-learn.org/stable/modules/feature_selection.html)\n",
"* [Classification probability](http://scikit-learn.org/stable/auto_examples/classification/plot_classification_probability.html)\n",
"* [Mastering Pandas](http://proquest.safaribooksonline.com/book/programming/python/9781783981960), Femi Anthony, Packt Publishing, 2015.\n",
"* [Matplotlib web page](http://matplotlib.org/index.html)\n",
"* [Using matlibplot in IPython](http://ipython.readthedocs.org/en/stable/interactive/plotting.html)\n",
"* [Seaborn Tutorial](https://stanford.edu/~mwaskom/software/seaborn/tutorial.html)\n",
"* [Iris dataset visualisation notebook](https://www.kaggle.com/benhamner/d/uciml/iris/python-data-visualizations/notebook)\n",
"* [Tutorial plotting with Seaborn](https://stanford.edu/~mwaskom/software/seaborn/tutorial/axis_grids.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Licence\n",
"\n",
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
"© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
]
}
],
"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",
2018-02-27 15:14:18 +00:00
"version": "3.6.4"
2016-03-15 12:55:14 +00:00
}
},
"nbformat": 4,
2018-02-27 15:14:18 +00:00
"nbformat_minor": 1
2016-03-15 12:55:14 +00:00
}