2020-06-28 12:50:46 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Coverage plots"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us consider the following function which applies a linear model to the given data. \n",
"Specifically, given a \"model\" vector containing the model coefficients $(a,b)$ and a $n \\times 2$ \"data\" matrix containing the data points to be classified, the function outputs a vector $\\mathbf{z}$, $|\\mathbf{z}| = n$ of booleans where $z_i$ is `True` if $a \\cdot x_{i,1} + b \\cdot x_{i,2} \\geq 0$, it is `False` otherwise."
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 2,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [],
"source": [
"def apply_linear_model(model, data):\n",
" return np.dot(data, np.transpose(model)) > 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us define `data` by generating $1000$ points drawn uniformly from $\\mathcal{X} = [-100,100]^2$."
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 3,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2020-07-03 19:08:23 +02:00
"/home/user/.local/lib/python3.7/site-packages/ipykernel_launcher.py:1: DeprecationWarning: This function is deprecated. Please call randint(-100, 100 + 1) instead\n",
2020-06-28 12:50:46 +02:00
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"data": {
"text/plain": [
2020-07-03 19:08:23 +02:00
"array([[ 63, 42],\n",
" [ 77, -65],\n",
" [ 24, -27],\n",
2020-06-28 12:50:46 +02:00
" ...,\n",
2020-07-03 19:08:23 +02:00
" [ 47, 20],\n",
" [-55, -72],\n",
" [-58, -23]])"
2020-06-28 12:50:46 +02:00
]
},
2020-07-03 19:08:23 +02:00
"execution_count": 3,
2020-06-28 12:50:46 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = np.random.random_integers(-100,100,[1000,2])\n",
"data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and let target_labels be the labeling output by applying `apply_linear_model` with our target model: $4x -y > 0$"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 4,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [],
"source": [
"target_model = [4.,-1.]\n",
"target_labels = apply_linear_model(target_model, data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By using matplotlib.pyplot module it is easy to plot the generated points onto a 2D plot:"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 5,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2020-07-03 19:08:23 +02:00
"<matplotlib.collections.PathCollection at 0x7f9753a8a510>"
2020-06-28 12:50:46 +02:00
]
},
2020-07-03 19:08:23 +02:00
"execution_count": 5,
2020-06-28 12:50:46 +02:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2020-07-03 19:08:23 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAD4CAYAAAAEhuazAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOxdZ5gUxdqtyR1mdslZRMkqQURJBjCgmEWMmDAAKhdQQAx4RQETil4DZhREQTFhQsRMEIElCJIlJ2HdQFg2zdT342zd7ump6q6emXW5H3Oepx+W3ZkO1VVvvfG8HkopySCDDDLI4OiCt6pvIIMMMsggg38eGeGfQQYZZHAUIiP8M8gggwyOQmSEfwYZZJDBUYiM8M8ggwwyOArhr+obkEGtWrVokyZNqvo2Msgggwz+p5CTk5NLKa3N+9v/hPBv0qQJW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knbJ4uWiQ3Jg89xPe1h0Jym8iffyJWM2tW/Pz//nu8OxZfURTEInfudD7nsGH8DalWLWNs33tP3gXs9yM+I3ITXXcdnmHOHCOY/9BDRjo8myPBIKy1UMhIitH1eBcZCzhbs8LiEItR2rKlfZCD+drHj7d3Q+i6cd6mTcWf83hwo++9Z3x+zRqkm51+OqVXXonJPnkyJvJll8HNVBXFUzNmyFsmbAxGj+ZPuFWroKGPGweT/rff5J9pyRJKb7qJ0nPOwWLYvx8BMavWpmnwAacRR53wpxRW19NPQ+AOHCiuiE0WTzwRH/zUdUrPPDPtGzcXsRgEu92aD4VQheqEOnX
2020-06-28 12:50:46 +02:00
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"colors = ['r' if l else 'b' for l in target_labels]\n",
"plt.scatter(data[:,0], data[:,1], color=colors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally let us now generate at random 100 linear models with coefficients in $[-5,5]$:"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 6,
"metadata": {
"scrolled": true
},
2020-06-28 12:50:46 +02:00
"outputs": [
{
"data": {
"text/plain": [
2020-07-03 19:08:23 +02:00
"array([[-3.56708359, 4.2173679 ],\n",
" [-2.24819841, -4.78033511],\n",
" [-2.65217433, -0.6063707 ],\n",
" [ 2.22522724, -1.3737868 ],\n",
" [ 1.14192194, -2.89068754],\n",
" [-2.57784496, -0.48320328],\n",
" [-1.98567784, -4.32466545],\n",
" [ 0.18087823, 3.4818154 ],\n",
" [-1.29875835, 1.51557613],\n",
" [ 4.81972953, -0.42049176],\n",
" [ 0.50982555, 4.85988498],\n",
" [-4.65192691, 4.7090496 ],\n",
" [ 1.76978104, -1.68074194],\n",
" [ 3.27116811, -2.24224267],\n",
" [-4.23830139, -3.18612366],\n",
" [-3.8511695 , -3.53659876],\n",
" [ 4.17130749, -3.99493944],\n",
" [-1.77612232, -2.98547622],\n",
" [ 2.60284283, -3.54389893],\n",
" [ 1.94474263, 0.87574031],\n",
" [-0.77056305, 0.69232701],\n",
" [ 3.34291539, 4.50561588],\n",
" [-2.72563297, 3.53671823],\n",
" [ 4.90984365, 1.68029481],\n",
" [ 3.19955049, 3.70221394],\n",
" [ 1.92131467, 3.118134 ],\n",
" [-1.3720329 , -2.8185771 ],\n",
" [-2.10756157, -3.29110329],\n",
" [ 1.23355252, -4.78030807],\n",
" [-1.43163348, -2.53721308],\n",
" [ 2.31520897, -1.3516775 ],\n",
" [-3.76663448, 0.31355621],\n",
" [ 4.85577408, 0.71145002],\n",
" [-4.3294729 , 1.68046326],\n",
" [-2.59917913, 4.08500999],\n",
" [ 3.67943457, 1.29892268],\n",
" [ 1.20118785, -0.04765288],\n",
" [-3.00013655, -4.55005222],\n",
" [ 4.2866447 , -0.31240087],\n",
" [-1.22791008, -2.15250488],\n",
" [-3.95873121, 2.13560657],\n",
" [ 1.40466773, 1.03243411],\n",
" [ 3.40406949, -0.88692212],\n",
" [ 0.62708997, 3.38264686],\n",
" [-3.48823227, -0.89532811],\n",
" [-4.34004124, 4.93494885],\n",
" [ 3.30806754, -3.92628874],\n",
" [-2.67753461, 0.36983349],\n",
" [-4.17195968, -1.85452959],\n",
" [-4.77495175, -3.33001656],\n",
" [-1.56106315, 4.34240146],\n",
" [ 4.10238174, -4.49656149],\n",
" [ 3.49311758, -1.39028564],\n",
" [-0.40272994, 4.22102051],\n",
" [ 4.50566149, 1.10356814],\n",
" [-2.3856706 , -3.97857028],\n",
" [ 3.72462319, 3.22213904],\n",
" [ 4.25264231, -1.68128306],\n",
" [ 0.73249504, -3.71816993],\n",
" [ 1.81677967, 4.73196931],\n",
" [-1.21442479, -2.84759613],\n",
" [-1.31382345, -4.09759844],\n",
" [-4.77299951, 4.85379267],\n",
" [-3.55287405, 0.95910989],\n",
" [ 0.18951388, 4.43661861],\n",
" [ 4.72987013, -4.09163053],\n",
" [ 0.8149054 , 4.31898901],\n",
" [-0.87583647, 1.16675956],\n",
" [-0.64637859, 3.40167131],\n",
" [-0.62658074, 4.58651024],\n",
" [-1.78645632, 3.85215258],\n",
" [ 1.15926923, -1.75726543],\n",
" [-2.00600541, 4.92990747],\n",
" [ 1.35433823, -2.32709303],\n",
" [-2.73621106, -4.0425546 ],\n",
" [ 3.83111626, 1.8644197 ],\n",
" [-1.22410301, 0.86166692],\n",
" [-1.51047856, -0.90824993],\n",
" [ 4.89797894, -2.64889207],\n",
" [-2.59993126, 4.9208619 ],\n",
" [-4.23181733, 2.23064469],\n",
" [-4.76666042, -3.66915997],\n",
" [-4.88905088, -0.44233631],\n",
" [-2.44930748, 0.56878184],\n",
" [-1.09013812, -2.24695652],\n",
" [ 0.49901451, -2.84495088],\n",
" [ 0.46975656, -0.99109978],\n",
" [-3.190664 , 2.63920782],\n",
" [-2.79980528, -4.23234307],\n",
" [ 0.13665919, -2.78904176],\n",
" [-3.56727479, 0.55472856],\n",
" [-3.59152304, 0.07804187],\n",
" [ 4.38128857, -3.87300022],\n",
" [ 3.84261658, 1.82166838],\n",
" [-3.44899234, -3.68102876],\n",
" [ 1.03133548, 3.09634947],\n",
" [-2.30690512, 0.22837024],\n",
" [ 1.35170795, -0.80041915],\n",
" [-2.70097645, 2.7137504 ],\n",
" [ 4.12379905, 4.98983271]])"
2020-06-28 12:50:46 +02:00
]
},
2020-07-03 19:08:23 +02:00
"execution_count": 6,
2020-06-28 12:50:46 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"models = (np.random.rand(100,2) - 0.5) * 10\n",
"models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Write a function that, taken two list of labellings build the corresponding confusion matrix [[1](#hint1)];\n",
"1. For each model in `models` plot the [FP,TP] pairs on a scatter plot;\n",
"1. Just looking at the plot: which is the best model in the pool?\n",
"1. Find the model with the best accuracy [[2](#hint2)] and compare it with the target model, is it close? Is it the model you would have picked up visually from the scatter plot?\n",
"1. If everything is ok, you should have found a pretty good model for our data. It fits the data quite well and it is quite close to the target model. Did you expect this? If so, why? If not so, why not?\n",
"\n",
"<a name=\"hint1\">Hint 1:</a> it may be helpful to have a way to map TRUE to 0, FALSE to 1 and to use these values as indices in the confusion matrix. \n",
"\n",
"<a name=\"hint2\">Hint 2:</a> one way to proceed is to build a function `accuracy`, use the `map` function to calculate the accuracies of all the models, and then apply the `numpy.argmax` to retrieve the index of the best model."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Es. 1"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 7,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [],
"source": [
"def build_confusion_matrix(labels1, labels2):\n",
" confusion_matrix = np.zeros((2,2))\n",
" for i in range(len(labels1)):\n",
" confusion_matrix[1 - labels1[i], 1 - labels2[i]] += 1\n",
" return confusion_matrix"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 8,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2020-07-03 19:08:23 +02:00
"[[124. 372.]\n",
" [386. 118.]]\n"
2020-06-28 12:50:46 +02:00
]
}
],
"source": [
"print(build_confusion_matrix(apply_linear_model(target_model, data), apply_linear_model(models[0], data)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Es. 2\n"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 9,
2020-06-28 12:50:46 +02:00
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
2020-07-03 19:08:23 +02:00
"<matplotlib.collections.PathCollection at 0x7f977f72ad50>"
2020-06-28 12:50:46 +02:00
]
},
2020-07-03 19:08:23 +02:00
"execution_count": 9,
2020-06-28 12:50:46 +02:00
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
2020-07-03 19:08:23 +02:00
"image/png": "iVBOR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xliqh8/KqlfXXly6N+OTgo/y7SBAruMkW5fPzvf6qfJ/eN1ZUvj6uzV/5dpPHUFVIqiiqd7O/t4cXh1YlLJwvnRdXZF95PRJKJ6wqpkbtUFJcvr6YKZmignxeHV2NVfh8RqZ6Cu1QU95C1ltLJSg9tVQ8vUj8Fd6kobtJTLVUwMzWJSiRLFNylorhJT7WUTs7EJCqRrNEkJkkkatLThjVLyi7pV2kR7VZPohLJGo3cpS5xo/BaNGsSlUjWaOQudYsahUeJK52s9S8BEZlKwV1aqvDANGqh7nKLe6vlsEj1FNylpZI0EEv6l4D6zotEqxjczew84B+Bc8P5T7j7vWa2CPgBcAGwD/gjd/83MzsX+B7wKeBXwG3u/laTrl86TNxC3YuGn+X8nm7MYPxUruJs17i/AESyLskD1d8Cq939KmA5cL2ZrQC+CTzo7p8ETgJ3hvPvBE6G/Q+G80SA+AejDoxP5Dh5Klexxl0lkyLxKgZ3z/t/YbM7fDiwGngi7H8EGAqvbwnbhOOfNbOoGeeSMZUW6i4VFbBVMikSL1EppJl1mdkB4DjwPPAGMO7up8Mp7wKFv4X7gXcAwvEPyKduSt9zvZmNmtnoiRMn6rsL6RilpZNJlAvYKpkUiZcouLv7GXdfDswDrgGW1vuN3X2Luw+6+2BfX1+9bycdpNBA7GcjN9GfIBiXC9hxLQxEpMpJTO4+DuwBrgN6zazwQHYeUEiMjgHzAcLx88k/WBWZplKaJipgN3rylEjaJKmW6QNy7j5uZj3A58g/JN0D3Eq+YmYd8HT4kh1h+8fh+G5vh6bx0pZK69pLq2VWLe1
2020-06-28 12:50:46 +02:00
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"fp = []\n",
"tp = []\n",
"\n",
"for model in models:\n",
" confusion = build_confusion_matrix(target_labels, apply_linear_model(model, data))\n",
" fp.append(confusion[1,0])\n",
" tp.append(confusion[0,0])\n",
"plt.scatter(fp, tp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Es. 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Il modello migliore è quello in alto a sinistra (max TP/FP)"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 10,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2020-07-03 19:08:23 +02:00
"array([ 3.40406949, -0.88692212])"
2020-06-28 12:50:46 +02:00
]
},
2020-07-03 19:08:23 +02:00
"execution_count": 10,
2020-06-28 12:50:46 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"models[np.argmax([t / f for t, f in zip(tp, fp)])]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Es. 4\n"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 11,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [],
"source": [
"def accuracy(tp, tn, total):\n",
" return (tp + tn) / total"
]
},
{
"cell_type": "code",
2020-07-03 19:08:23 +02:00
"execution_count": 12,
2020-06-28 12:50:46 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2020-07-03 19:08:23 +02:00
"model: [ 3.40406949 -0.88692212] accuracy: 0.998\n"
2020-06-28 12:50:46 +02:00
]
}
],
"source": [
"accuracies = []\n",
"\n",
"for model in models:\n",
" confusion = build_confusion_matrix(target_labels, apply_linear_model(model, data))\n",
" accuracies.append(accuracy(confusion[0,0], confusion[1,1], 1000))\n",
"\n",
"print(\"model: \", models[np.argmax(accuracies)], \" accuracy: \", accuracies[np.argmax(accuracies)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Il modello è lo stesso predetto dalla plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Es. 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mi aspettavo di trovare un modello con un'accuracy alta ma non come quella trovata (0.995), perchè su 100 modelli, con due variabili comprese tra 5 e -5, generati con una funzione random uniforme mi aspetto dei valori vicini a quelli target."
]
}
],
"metadata": {
"anaconda-cloud": {},
"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.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 1
}