1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-05 07:31:41 +00:00
sitc/ml1/2_3_0_Visualisation.ipynb

390 lines
80 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": [
"First, we are going to inspect the distribution of the samples per feature."
]
},
{
"cell_type": "code",
2016-03-15 15:12:44 +00:00
"execution_count": 2,
2016-03-15 12:55:14 +00:00
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sklearn import datasets\n",
"iris = datasets.load_iris()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"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": "markdown",
"metadata": {},
"source": [
"First we are going to analyse the [histogram](https://en.wikipedia.org/wiki/Histogram). \n",
"\n",
"A histogram is a graphical representation of the distribution of numerical data. It is an estimate of the probability distribution of a continuous variable (quantitative variable). \n",
"\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."
]
},
{
"cell_type": "code",
2016-03-15 15:12:44 +00:00
"execution_count": 4,
2016-03-15 12:55:14 +00:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
2016-03-15 15:12:44 +00:00
"<matplotlib.text.Text at 0x7f5f8c1589b0>"
2016-03-15 12:55:14 +00:00
]
},
2016-03-15 15:12:44 +00:00
"execution_count": 4,
2016-03-15 12:55:14 +00:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEPCAYAAABCyrPIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAE8NJREFUeJzt3X+UZ3V93/HnawGVQYTVlsUTBKMoNlaLNkETOLKNGDVW\n11hBrVo1Nk1y0so59ngEq4HWNkis+WHtyYmGNKuJUeNJAAMWJGRNMRWIQMFfW4iKlcBoXCXLYBTc\nd//4fpYdxp2Z+92d+713d56Pc+6Ze+/3fu99z/d8vvOa++tzU1VIkrRh6AIkSeNgIEiSAANBktQY\nCJIkwECQJDUGgiQJgEP73kCSrwB3A7uA+6rqlCQbgQ8DJwBfAc6qqrv7rkWStLxZ7CHsAjZX1dOq\n6pQ27xzgqqo6CbgaOHcGdUiSVjCLQMhetrMF2NrGtwIvnkEdkqQVzCIQCvhEkuuT/Os2b1NVzQNU\n1V3AMTOoQ5K0gt7PIQCnVtWdSf4hcGWS7UxCYjH7z5CkgfUeCFV1Z/v5jSQXA6cA80k2VdV8kmOB\nr+/tvUkMCknaB1WVad/T6yGjJHNJHt7GjwB+CrgFuBR4bVvsNcAly6+lBhy+ydzcRqrqoBjOO++8\nwWs4WIbh2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jpk9MJF4/PsOcTzDeDwrhto9y18FPhAVV2ye31JNlXVfJJjga8vv4bzF41vboMkaY9t\nbdg/nZ6pvF8bSN4P/G1VvXHRvAuBHVV1YZI3Axur6gfOIfhMZY3VyJ6dO3QRakbWLqZ+pvKqgZDk\nh4F/BzyWRXsUXbq/TnIq8BdM7mGoNrwFuI7JIajHALczuez023t5v4GgURrZF3/oItSMrF1MHQhd\nrjK6GLiIyd3Ju6ZZeVV
"text/plain": [
2016-03-15 15:12:44 +00:00
"<matplotlib.figure.Figure at 0x7f5f8c1a9668>"
2016-03-15 12:55:14 +00:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Plot histogram, the default is 10 bins\n",
"plt.hist(iris.target, bins=10)\n",
"plt.xlabel('iris class')\n",
"plt.ylabel('Number of species')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see we have the same distribution of samples for each class.\n",
"Now we are going to see the distribution of the features"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n"
]
}
],
"source": [
"# We remember the name of the features to see its index\n",
"print(iris.feature_names)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['setosa' 'versicolor' 'virginica']\n"
]
}
],
"source": [
"# We remember the name of target names\n",
"print(iris.target_names)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A [**scatter plot**](https://en.wikipedia.org/wiki/Scatter_plot) (*gráfico de dispersión*) display values for typically two variables for a set of data."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
2016-03-15 15:12:44 +00:00
"<matplotlib.text.Text at 0x7f5f8c093b70>"
2016-03-15 12:55:14 +00:00
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2016-03-15 15:12:44 +00:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEPCAYAAABsj5JaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHY1JREFUeJzt3X+cXXV95/HXG5KpIzQpaSO1JCSgRBMJBtxFIKLTVVJC\n08RdWalL164NK+uaR3jUffjQ7UKS1a7Vtku3iMFSrQmdCRhdHJxQ2/HXPGSyixMTEtAZFDeMUqhM\nWgSlpH2AfPaPe2ZyZ3LvOXe+M3PP3OT9fDzuI/ee74/zOd85uZ97vufecxQRmJmZTdYpZQdgZmat\nyQnEzMySOIGYmVkSJxAzM0viBGJmZkmcQMzMLEmpCUTSIklfk/QdSQ9J2lyjzpskPS3pQPa4sYxY\nzcxsvDklr/8F4H0RcVDS6cB+Sb0R8fCEet+IiPUlxGdmZnWUegQSET+KiIPZ82eBIeCsGlXV1MDM\nzKzQrDkHImkpsAr4Zo3iSyUdlHSvpBVNDczMzGoqewoLgGz66vPADdmRSLX9wNkR8ZyktUA3sKzZ\nMZqZ2Xgq+1pYkuYAe4AvRcSfNlD/UeB1EfFUjTJf2MvMbJIiIuk0wWyYwvoLYLBe8pB0ZtXzi6kk\nveOSx6iImFWPrVu3lh6DYzpxYpqtcTmm1o1pKkqdwpK0GrgWeEjSA0AAvwcsASIibgeulvQe4Hng\nKHBNWfGamdkxpSaQiNgLnFpQ5xPAJ5oTkZmZNWo2TGGd0Do6OsoO4TiOqTGzMSaYnXE5psbMxpim\novST6NNJUpxI22NmNtMkES18Et3MzFqQE4iZmSVxAjEzsyROIGZmlsQJxMzMkjiBmJlZEicQMzNL\n4gRiZmZJnEDMzCyJE4iZmSVxAjEzsyROIGZmlsQJxMzMkjiBmJlZEicQMzNL4gRiZmZJnEDMzCyJ\nE4iZmSVxAjEzsyROIGZm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umo0QSyX1IvlQTyN5J+Hnhx5sIyM7PZThFRXEk6BVgFHI6I\npyX9InBWRDw40wFOhqRoZHvMzKxCEhGRNKPU6BFIACuAzdnr04CXpKzQzMxODI0mkO3ApcA7stc/\nBT4xIxGZmVlLmNNgvddHxEWSHgCIiB9LapvBuMzMbJZr9AjkeUmnUpnKQtJCfBLdzOyk1mgCuQX4\nAvAySf8D6Ac+MmNRmZnZrNfQt7AAJL0aeDOV3398NSKGZjKwFP4WlpnZ5EzlW1i5CUTSvIj4iaQF\ntcoj4qmUlc4UJxAzs8mZya/x7sr+3Q98q+ox+nrKsmtrPSmp7m9KJN0i6RFJByWtmo71mpnZ1OQm\nkIhYl/17TkScW/U4JyLOnaYYPgP8Wr1CSWuBV0TEecD1wCenab0nrCNHjrBv3z6OHDlyXNnQ0BA7\nd+5kaOj4Gci8do2Up8bU1dXFhg0b6Orqqtk2rzyvbO/evWzdupW9e/fW7DdvLPbs2cN1113Hnj17\narbNK8/rtyiuorapf9uiscjrdyb3C2txEVH4AP41ML/q9S8Ab22kbYP9LwEerFP2SeCaqtdDwJl1\n6sbJbteuu6K9fUHMn39RtLcviF277hor27TphoD2gGUB7bFp0+aG2jVSnhrTokXnZDGdF9Aeixcv\nHdc2rzyv7Ior1o4rW7Nm7bh+88bi/PMvHNd25cpV49rmlef1WxRXUdvUv23RWOT1O5P7hc0O2ftm\n2nt3Q5XgYI1lD6SutEZfeQmkB7is6vVXgIvq1J2uMW1JIyMj0d6+IOBQQAQcivb2BTEyMhKDg4PZ\nm8ixMmiPwcHB3HZF/U4lps7OzpoxdXZ2RkTklueV9ff31yzr7++PiMgdi56enpplPT09ERG55Xn9\nRkRuXEVtU/+2RWOR1+9
2016-03-15 12:55:14 +00:00
"text/plain": [
2016-03-15 15:12:44 +00:00
"<matplotlib.figure.Figure at 0x7f5f8c110c18>"
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",
2016-03-15 15:12:44 +00:00
"plt.xlabel(iris.feature_names[0])\n",
"plt.ylabel('species')"
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "code",
2016-03-15 15:12:44 +00:00
"execution_count": 8,
2016-03-15 12:55:14 +00:00
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAEACAYAAABWLgY0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X14VOWZP/DvkxcIBMIEIi8JkBmCWnG7Vn7XirwIARaq\ndksrogUmEnxrUQsIWC+rO5I0trVoUfFllcWaKBRQrC77U9yySDKAK/62aNW6ajeZRJsgihACkRBI\n7t8fGZLJ5IRzZubMmTMz3891zcXknDPPuc9D8uTkOec+txIREBFRYkqJdQBERBQ9HOSJiBIYB3ki\nogTGQZ6IKIFxkCciSmAc5ImIEpjhQV4plaKUOqCU2q6xbppSqtG//oBS6p/NDZOIiMKRFsK2ywF8\nBCCrl/VeEZkTeUhERGQWQ2fySqmRAK4GsOFcm5kSERERmcbodM0jAH4G4FzpsROVUu8ppV5TSo2L\nPDQiIoqU7iCvlPoegEMi8h46zta1ztj/BGC0iHwHwBMAXjU1SiIiCovSe3aNUupXAIoAnAHQD8BA\nAH8QkUXn+IwPwP8RkSNBy/mgHCKiMIhIWFPiumfyInKviIwWkTEA5gN4M3iAV0oNC3h/GTp+eRyB\nBhGx/Wv16tUxj4FxMs54jZFxmv+KRCh313SjlPpJx5gt6wHMU0rdBuA0gJMAfhRRVEREZIqQBnkR\nqQJQ5X//TMDyJwE8aW5oREQUKWa8aigsLIx1CIYwTnPFQ5zxECPAOO1E98KrqTtTSqzcHxFRIlBK\nQcK88Br2nDxRtPl8dfB4ylFf3468vBSUlS2Gy5Uf67ASgtPpRF1dXazDoCD5+fmora01tU2eyZMt\n+Xx1mDXrcVRXlwLIBNCMg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94WrCjhRvYW7s+ie6lbcC8EJQGveyHupW7TYqurq5PM\nzEw5ceKEiIi0tbXJiBEjZP/+/XLNNdfIbbfdJidPnpSvvvpKJkyYIOvXrxcRkfLycklLS5Mnn3xS\n2tra5OTJk7JgwQL51a9+JSIip06dkn379nXuJyUlRaqrq0VE5Pbbb5fp06fLwYMHpb29Xf7rv/5L\nWltb5ZNPPpHMzEzZtWuXnDlzRtasWSNjx46V06dPi4iI0+mUXbt2iYiIx+ORiRMnyuHDh+Xw4cMy\nadIkuf/++0VEpLKyUtLS0uTnP/+5tLa2SktLi+ax9/b/Aj5PnkLBRCUKV31TPdAnaGEfoKGpwbR9\njB49GuPHj8crr7wCANi1axcyMzPhdDrx+uuv45FHHkFGRgZycnJw5513YvPmzZ2fzcvLw+23346U\nlBRkZGQgPT0ddXV1qK+vR58+fTBp0qTObSWgZOBzzz2HdevWYfjw4VBK4fLLL0d6ejpefPFF/NM/\n/RNmzJiB1NRU3HXXXTh58iTeeuutHnH//ve/x+rVqzFkyBAMGTIEq1evxgsvvNC5PjU1FaWlpUhP\nT0ffvn1N6y89HOSTEBOVKFx5WXlAa9DCViA3K9fU/SxYsKBz8N68eTMWLlyIuro6nD59GiNGjMDg\nwYORnZ2NJUuWdCvNN2rUqG7tPPTQQ2hvb8dll12Gb3/723juued67Ovw4cM4deoUxowZ02NdcAlB\npRRGjRqF+vp6zW1Hjx7d+XV+fj4aGrp++Z133nlIT08PoRfMwZ/qJMREJQpX2coyFPy5oGugbwUK\n/lyAspVlpu7nuuuuQ2VlJerr6/HKK6/A7XZj1KhRyMjIwNdff40jR47g6NGjaGxsxPvvv9/5ueBa\nr0OHDsX69etRX1+Pp59+GrfffnvnPPxZOTk5yMjI6FZV6qzc3FzU1XW/VvX5559j5MiRutvW1dUh\nN7frl59WHVorcJBPQla
"text/plain": [
2016-03-15 15:12:44 +00:00
"<matplotlib.figure.Figure at 0x7f5f8c0a8748>"
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": [
"As we can see, the Setosa class seems to be linear separable with these two features.\n",
"\n",
"Another nice visualisation is given below."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
2016-03-15 15:12:44 +00:00
"<matplotlib.text.Text at 0x7f5f5e1f1320>"
2016-03-15 12:55:14 +00:00
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAEPCAYAAAC+35gCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd4VFX6+D9nZjItjZIQCL33DqEKAVFEVlQsYENxdwEV\nu/5s64plZfW7dhQsqOi6InZBpBN67whCKAlNegnJTNrM+/vjDjDJJDAhk4SQ83me++TOue89970l\n973nPe95jxIRNBqNRqMJBlNZK6DRaDSa8oM2GhqNRqMJGm00NBqNRhM02mhoNBqNJmi00dBoNBpN\n0GijodFoNJqgKXGjoZRKUUptUEqtU0qtLETmXaVUslJqvVKqXUnrpNFoNKWNUqqGUmrKRez3kVKq\n2QVkRiql7rx47YqgT0mP01BK7QI6isiJQrYPAEaLyEClVBfgHRHpWqJKaTQazSWCUsosIp6y1iNY\nSsM9pS5wnOuBLwBEZAUQrZSKKwW9NBqNpkRQSo1VSt3v9/sFpdTjSqlNvt93K6V+VkrNBeYogw+U\nUluUUjOVUr8qpQb7ZOcrpTr41k8rpV7xeWWWKqVi/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/vm/8/TT/6+sVdRUMEIWPfVDkLKDdfRUKHkLeBIo7I1fE9jr93u/r0xTTlizZg0pKQf9DAaAA5fr\nSt58892yVE2jKR46eiqAEj1dpdRA4JCIrFdKJQLFssRjxow5u56YmEhiYmJxqtOEiH379mE2VyPw\nGySWY8cOloVKmgpGUlISSUlJoa+4ghmEYChR95RS6lXgTiAXcACRwA8iMsxPJr976g+gt3ZPlR9S\nUlJo3rwtmZmjAf9Bhdt
"text/plain": [
2016-03-15 15:12:44 +00:00
"<matplotlib.figure.Figure at 0x7f5f5e2412b0>"
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",
"plt.ylabel(iris.feature_names[y_index])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we can also check that the Setosa class seems to be linear separable.\n",
"\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",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}