Last active
December 16, 2015 17:41
-
-
Save pilipolio/5472470 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "GaussianMixtureModels" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Gaussian mixture models and unsupervised clustering\n", | |
"===================================================\n", | |
"\n", | |
"Model and toy data set\n", | |
"----------------------\n", | |
"\n", | |
"A gaussian mixture model in $\\mathbb{R}^d$ is defined by the following parameters:\n", | |
"\n", | |
" - the mixture's number of components $m$;\n", | |
" - the mixture's components probabilities $\\pi_k$ for $k= 1, \\ldots, m$;\n", | |
" - the components' mean vectors $\\mu_k = (\\mu_{k,1}, \\ldots, \\mu_{k,d})$ for $k= 1, \\ldots, m$;\n", | |
" - the components'covariance matrixes $\\Sigma_k \\in \\mathcal{M}_{d,d}$ for $k= 1, \\ldots, m$;\n", | |
"\n", | |
"A data-set of $n$ observations stemming from that model have the following hidden parameters:\n", | |
"\n", | |
"- $(a_i)_{i=1, \\ldots,n}$ the latent allocation of each point $n$ to one of the $m$ components.\n", | |
"\n", | |
"$$ \\pi \\longrightarrow \\underbrace{A \\rightarrow X}_{n} $$\n", | |
"\n", | |
"$$ A | \\pi \\sim \\text{Mult}(k, \\pi)$$\n", | |
"\n", | |
"$$ X | A=k \\sim \\mathcal{N}(\\mu_k, \\Sigma_k)$$\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"d = 2\n", | |
"m = 3\n", | |
"n = 100\n", | |
"\n", | |
"def generate_toy_gmm_data_set(d, m, n):\n", | |
" mu_m = np.random.uniform(0, 10, size= m * d).reshape((m,d))\n", | |
" prob_m = np.random.uniform(0, 1, size= m)\n", | |
" prob_m /= np.sum(prob_m)\n", | |
" assignements_n = np.where(np.random.multinomial(1, prob_m, size = n))[1]\n", | |
" #cov_m = np.random.uniform(0, 3, size= m * d * d).reshape((m, d, d))\n", | |
" iid_n = np.random.normal(size= n * d).reshape((n, d))\n", | |
" x_n = mu_m[assignements_n] + iid_n\n", | |
" return mu_m, prob_m, assignements_n, x_n\n", | |
"\n", | |
"mu_m, prob_m, assignements_n, x_n = generate_toy_gmm_data_set(d, m, n)\n", | |
"print 'Generated n={} points from a mixture of m={} gaussians prob_m = {}'.format(n, m, prob_m)\n", | |
"\n", | |
"colors_m = np.array(['r','g','b','k'])\n", | |
"plt.scatter(x = x_n[:,0], y = x_n[:,1], s = 10, c = colors_m[assignements_n], marker = 'o', faceted=False, alpha=.7)\n", | |
"plt.scatter(x = mu_m[:,0], y = mu_m[:,1], s = 50, lw = 3, c = colors_m, marker = 'x', edgecolors=None);\n", | |
"plt.xlim([0, max(plt.xlim()[1], plt.ylim()[1])]);plt.ylim([0, max(plt.xlim()[1], plt.ylim()[1])]);" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Generated n=100 points from a mixture of m=3 gaussians prob_m = [ 0.27417081 0.41692802 0.30890117]\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"png": "iVBORw0KGgoAAAANSUhEUgAAAWwAAAD5CAYAAAADQw/9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcVOX+B/DPsMmu4AIq4JbIIgIukXb14gIWqZmZV6hE\nyK5WtqmV3eoX3q5hmXW11Exzy0oru+o1xSIb02uYiiLhmoqBgInKJsvAzPn98SiL7MMMhzPzeb9e\nvBjOnDnnexS+88xznuf7qCRJkkBERG2ehdwBEBFR0zBhExEpBBM2EZFCMGETESkEEzYRkUIwYRMR\nKYSVsQ6sUqmMdWgiIpNW32hro7awJUkyma8333xT9hh4LaZ/PaZ0LaZ2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/jJHpT8fHx8kJSWhpKQEkiQhMTERfn5+cofVIhMmTMCGDRsAABs2bFB0gwcQo98WL16M7du3\nw9bWVp4gJCPYtWuX5O3tLfXp00d6++23jXGKVrF//35JpVJJgYGBUlBQkBQUFCTt3r1b7rBaTK1W\nS+PHj5c7jBY7fvy4NHjwYGnAgAHSQw89JOXl5ckdUou88847kp+fn9S/f39p2rRpkkajkTukJps6\ndarUtWtXydraWvLw8JDWrl0rXbt2TRo9erTUt29fKSwsTLpx44bcYTbZndfz6aefSnfddZfk5eVV\nmQueeuqpVo9Lr1oiRETU+pQ/XICIyEwwYRMRKQQTNhGRQjBhExEpBBM2EZFCMGETESnE/wNuoZ7f\nkYrBoAAAAABJRU5ErkJggg==\n" | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"The K-Means algorithm" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Initialisation (step $s = 0$):\n", | |
"\n", | |
"- Choose $m=3$ centroids randomly drawn fron the $n$ points\n", | |
"\n", | |
"As long as $a_n$ at step $s$ differ from previous $a_n$, do :\n", | |
"\n", | |
"- Estimate the assignements $a_n$ by associating each points to its closest centroids according to the $L_2$ norm;\n", | |
"- Re-compute the $k = 1, \\ldots, m$ centroids by averaging coordinates $ \\{ x_i ; a_i = k \\} $;\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"step = 0\n", | |
"centroids_mu_m = x_n[np.random.choice(n, size = m, replace=False),:]\n", | |
"centroids_a_old_n = centroids_a_n = np.repeat(m, n)\n", | |
"dist_n = np.repeat(np.infty, n)\n", | |
"\n", | |
"def kmeans_iteration(x_n, centroids_mu_m):\n", | |
" dist_n_m = np.transpose([np.sum(np.abs(x_n - centroids_mu_m[k,:])**2, axis=1) for k in range(m)])\n", | |
" centroids_new_a_n = np.argmin(dist_n_m, axis=1)\n", | |
" centroids_mu_m = np.array([np.mean(x_n[centroids_new_a_n==k,:], axis=0) for k in range(m)])\n", | |
" return centroids_mu_m, centroids_new_a_n, np.min(dist_n_m, axis=1)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 23 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"plt.scatter(x = x_n[:,0], y = x_n[:,1], s = 10, c = colors_m[centroids_a_n], marker = 'o', faceted=False, alpha=.6)\n", | |
"plt.scatter(x = centroids_mu_m[:,0], y = centroids_mu_m[:,1], s = 50, lw = 3, c = colors_m, marker = 'x', faceted=False)\n", | |
"plt.xlim([0, max(plt.xlim()[1], plt.ylim()[1])]);plt.ylim([0, max(plt.xlim()[1], plt.ylim()[1])]);\n", | |
"plt.title('Step = {}'.format(step))\n", | |
"print 'At step {}, {} assignements changed and total variance equals to {:.2f}'.format(step,\n", | |
" np.sum(np.where(centroids_a_n != centroids_a_old_n)[0]),\n", | |
" np.mean(np.sqrt(dist_n)))\n", | |
"\n", | |
"# Performing step\n", | |
"centroids_mu_m, centroids_new_a_n, dist_n = kmeans_iteration(x_n, centroids_mu_m)\n", | |
"centroids_a_n, centroids_a_old_n = centroids_new_a_n, centroids_a_n\n", | |
"step += 1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"At step 5, 0 assignements changed and total variance equals to 1.08\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"png": "iVBORw0KGgoAAAANSUhEUgAAAWwAAAEHCAYAAACKrHwgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XtcVHXeB/DPwIAgIALCIIJKIgqIYGpeUhszTG1x1WxT\n10va5dG2nuyp9rGtVu2GZW1qWttamm2m9WytmSGVa8NipmUooqKISnKRm9wZLgOc54/fclMuwzDD\nmTN83q8XL8fhzDnf424ffvzO76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g7969uPPOO/HRRx/JXZbJ\nfH19ERAQgNTUVADAoUOHEBYWJnNVphs+fDiOHTuGyspKSJKEQ4cOITQ0VO6yumT27NnYtWsXAGDX\nrl2KbvAAYvTbxo0b8eWXX8LJyUmeIiQLiI2NlYKDg6UhQ4ZIr776qiUu0S0SEhIklUolRURESJGR\nkVJkZKR08OBBucvqMp1OJ0VHR8tdRpedOnVKGjNmjDRy5Ehp7ty5UnFxsdwldclrr70mhYaGSiNG\njJCWLl0q1dTUyF2S0RYsWCD1799fcnBwkPz9/aUdO3ZI169fl6ZNmyYNHTpUioqKkoqKiuQu02g3\n3s8HH3wgBQUFSQMHDmzMglWrVnV7XSatJUJERN1P+cMFiIh6CAY2EZFCMLCJiBSCgU1EpBAMbCIi\nhWBgExEpxP8D6dvukNQMFaYAAAAASUVORK5CYII=\n" | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Once the algorithm has converged, we calculate the misclassified errror has :" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import itertools\n", | |
"import operator\n", | |
"\n", | |
"def find_minimal_error_and_permutation(real_a_n, estimated_a_n, m):\n", | |
" return sorted([{'permutation':np.array(p), 'error':100 * np.average(real_a_n != np.array(p)[estimated_a_n])} for p in itertools.permutations(range(m))],\n", | |
" key=operator.itemgetter('error'))[0]\n", | |
"\n", | |
"error_and_permutation = find_minimal_error_and_permutation(real_a_n=assignements_n, estimated_a_n=centroids_a_n, m=m)\n", | |
"print 'Minimum error of {error}% with re-ordering {permutation} of clusters'.format(**error_and_permutation)\n", | |
"estimated_probs_m = np.array([np.sum(centroids_a_n==k,dtype='float') for k in range(m)])[error_and_permutation['permutation']] \n", | |
"print 'The estimated probabilities of each cluster are : {}'.format(estimated_probs_m)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Minimum error of 16.0% with re-ordering [0 2 1] of clusters\n", | |
"The estimated probabilities of each cluster are : [ 40. 27. 33.]\n" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The Expectation-Maximization algorithm\n", | |
"--------------------------------------\n", | |
"\n", | |
"EM is an general purpose optimization method for deriving hidden parameters of probabilistic models. It is formulated as to find the maximum likelihood estimator of parameters of a probability distribution which is partially observed. It fits well to model problems with hidden or latent variables. The term was coined by Dempster, Laird and Rubin in [their original paper](http://web.mit.edu/6.435/www/Dempster77.pdf) and it is widely used in the context of [mixture models](http://en.wikipedia.org/wiki/Mixture_model). The paper [\"What is the expectation maximization\n", | |
"algorithm?\"](http://ai.stanford.edu/~chuongdo/papers/em_tutorial.pdf) is a nice introduction to it.\n", | |
"\n", | |
"Our toy problem actually fits well that framework if we write :\n", | |
"\n", | |
"- $p = \\big(\\mu^{1} = (\\mu^{1}_1, \\ldots, \\mu^{1}_d), \\ldots, \\mu^{m}, \\pi_1, \\ldots, \\pi_m, \\Sigma_1, \\ldots, \\Sigma_m \\big)$ the parameters of our mixture of gaussian model;\n", | |
"- $(a_i)_{i=1,\\ldots, n})$ the hidden or latent assignements of points to gaussians;\n", | |
"\n", | |
"The EM algorithm decomposes itself in two steps :\n", | |
"\n", | |
"- The E or Expectation step : estimation of the latent variables behind the observed sample. In our example, those variables are the assignements of the $i=1,\\ldots,n$ points to one of the $k=1,\\ldots,m$ components, whom we estimate the probabilities $P[A = k | x = x_i] = P[a_i = k | x_i]$.\n", | |
"\n", | |
"\n", | |
"- The Maximization step : estimation of parameters $p$ through the maximum likelihod estimator. In the gaussian case, the maximum likelihood estimator of $p$ has the nice property to have two independent closed forms :\n", | |
"<center> $\\pi_k = \\frac{1}{n} \\sum \\limits_{i=1}^n \\ p[a_i = k]$ and $\\mu^{k} = \\frac{\\sum \\limits_{i=1}^n p[a_i = k] x_i}{\\sum \\limits_{i=1}^n p[a_i = k]}$</center>\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here are the details for calculating the assignements probabilities $P[a_i = k|x_i]$ : \n", | |
"\n", | |
" - Knowing that $P[x_i | a_i = k ] = \\frac{P[a_i = k, x_i]}{P[a_i = k]} $ and $P[x_i] = \\sum \\limits_k^m P[x_i | a_i = k ]$\n", | |
" - And that $ P[a_i = k] = \\pi_k$ and $ P[x_i | a_i = k ] = \\text{pdf}(x_i; \\mu_k, \\Sigma_k^{-1}) \\propto \\frac{1}{|\\Sigma^k|} \\text{exp} \\Big( - \\frac{1}{2}(x_i - \\mu^k)^{T} \\Sigma_k^{-1} (x_i - \\mu^k) \\Big)$ \n", | |
"\n", | |
"<center> $P[a_i = k | x_i] = \\frac{P[a_i = k, x_i]}{P[x_i]} = \\frac{P[x_i | a_i = k ] P[a_i = k]}{\\sum \\limits_k^m P[x_i | a_i = k ]} = \\pi_k \\ \\frac{1}{|\\Sigma^k|} \\text{exp} \\Big( - \\frac{1}{2}(x_i - \\mu^k)^{T} \\Sigma_k^{-1} (x_i - \\mu^k) \\Big) \\frac{1}{\\sum \\limits_k^m \\ldots} $ </center>\n", | |
"\n", | |
"It is proven that the EM algorithm converges to a local maxima of the likelihood $L(x_1, \\ldots, x_n; p) = \\Pi_i^n P[x_i]$\n", | |
"\n", | |
"<center> $\\text{ln} L(x_1, \\ldots, x_n; p) = \\sum \\limits_i^n \\sum \\limits_k^m \\frac{\\pi_k}{|\\Sigma_k|} \\text{exp} \\Big( - \\frac{1}{2}(x_i - \\mu^k)^{T} \\Sigma_k^{-1} (x_i - \\mu^k) \\Big)$ </center>\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def em_iteration(x_n, centroids_mu_m, prob_m, cov_m):\n", | |
" # cov_m.shape = (m) => spherical covariances \n", | |
" # E-step\n", | |
" dist_n_m = np.transpose([np.sum(np.abs(x_n - centroids_mu_m[k,:])**2, axis=1) / (2 * cov_m[k]) for k in range(m)])\n", | |
" pa_n_m = np.exp(-dist_n_m) * prob_m / cov_m[k]\n", | |
" pa_n_m = (pa_n_m.T / np.sum(pa_n_m, axis=1)).T\n", | |
" # M-step\n", | |
" prob_m = np.mean(pa_n_m, axis=0)\n", | |
" centroids_mu_m = np.array([np.sum(x_n.T * pa_n_m[:,k], axis=1) / np.sum(pa_n_m[:,k]) for k in range(m)])\n", | |
" return centroids_mu_m, prob_m, pa_n_m, dist_n_m" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Initialisation :" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"step = 0\n", | |
"\n", | |
"centroids_mu_m = x_n[np.random.choice(n, size = m, replace=False),:]\n", | |
"estimated_prob_m = np.repeat(1.0/m, m)\n", | |
"old_pa_n_m = pa_n_m = np.repeat(1.0/3.0, n * m).reshape(n,m)\n", | |
"sigma_m = np.repeat(1, m)\n", | |
"dist_n = np.repeat(np.infty, n)\n", | |
"\n", | |
"plt.scatter(x = x_n[:,0], y = x_n[:,1], s = 10, c = 'k', marker = 'o', faceted=False, alpha=.6)\n", | |
"plt.scatter(x = centroids_mu_m[:,0], y = centroids_mu_m[:,1], s = 50, lw = 3, c = colors_m, marker = 'x', faceted=False);\n", | |
"plt.xlim([0, max(plt.xlim()[1], plt.ylim()[1])]);plt.ylim([0, max(plt.xlim()[1], plt.ylim()[1])]);" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"png": "iVBORw0KGgoAAAANSUhEUgAAAWwAAAD5CAYAAAADQw/9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XdYVGe+B/AvQxEF6TBUFQtKE7DEio6xm+Ba2I1yLZHV\nZHWTVTc9MQlmVcya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FiwQKiqqhK7pBabM2eO4OLiIpiamgru7u7C1q1b\nhdu3bwvjx48X+vXrJ0ycOFG4c+eO2GW22MPP89VXXwl9+/YVevToocmCpUuXtntdWq0lQkRE7U/6\nwwWIiDoJBjYRkUQwsImIJIKBTUQkEQxsIiKJYGATEUnE/wNGpwjUX/H1bwAAAABJRU5ErkJggg==\n" | |
} | |
], | |
"prompt_number": 32 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"plt.scatter(x = x_n[:,0], y = x_n[:,1], c = colors_m[np.argmax(pa_n_m, axis=1)], s = 50 * (np.max(pa_n_m, axis=1)-.333), marker = 'o', faceted=False, alpha=.6)\n", | |
"plt.scatter(x = centroids_mu_m[:,0], y = centroids_mu_m[:,1], s = 50, lw = 3, c = colors_m, marker = 'x', faceted=False)\n", | |
"plt.xlim([0, max(plt.xlim()[1], plt.ylim()[1])]);plt.ylim([0, max(plt.xlim()[1], plt.ylim()[1])]);\n", | |
"plt.title('Step = {}'.format(step))\n", | |
"print 'At step {}, {} assignements changed and total variance equals to {:.2f}'.format(step,\n", | |
" np.sum(np.argmax(pa_n_m, axis=1) != np.argmax(old_pa_n_m, axis=1)),\n", | |
" np.mean(np.sqrt(dist_n)))\n", | |
"\n", | |
"# Performing step\n", | |
"centroids_mu_m, estimated_prob_m, new_pa_n_m, dist_n_m = em_iteration(x_n, centroids_mu_m, estimated_prob_m, sigma_m)\n", | |
"pa_n_m, old_pa_n_m = new_pa_n_m, pa_n_m\n", | |
"step += 1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"At step 10, 0 assignements changed and total variance equals to inf\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"png": "iVBORw0KGgoAAAANSUhEUgAAAWwAAAEHCAYAAACKrHwgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcU2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c3GH1ip+fH4qLiwEAN2/ehK+vr5kj6r1du3Zh//79ZvswNXrCtqYabY7j8OijjyIi\nIgJr1qwxdzi9smnTJuTn5yM7Oxt79+7FlClT8Omnn5o7LIP5+/ujX79+yMzMBAAcOnQIkZGRZo7K\ncMOGDcPJkyfR2NgIjuNw6NAhREREmDusXpk7dy6SkpIAAElJSbwe8ACs+m3btm344YcfIJFIzBME\nZwL79+/nwsLCuMGDB3ObNm0yxUv0iaNHj3ICgYCLiorioqOjuejoaO7AgQPmDqvXZDIZN2fOHHOH\n0Wvnz5/nYmJiuJEjR3L33nsvV11dbe6QemXr1q1cREQEN3z4cG7JkiVcS0uLuUPS2wMPPMAFBARw\nYrGYCw4O5nbu3MlVVFRwU6dO5YYMGcIlJiZyVVVV5g5Tbx3fz8cff8yFhoZy/fv3V+WCv/3tb30e\nl0G9RAghhPQ9/pcLEEKIjaCETQghPEEJmxBCeIISNiGE8AQlbEII4QlK2IQQwhP/Hz/+c87RR8m6\nAAAAAElFTkSuQmCC\n" | |
} | |
], | |
"prompt_number": 43 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"error_and_permutation = find_minimal_error_and_permutation(real_a_n=assignements_n, estimated_a_n=np.argmax(pa_n_m, axis=1), m=m)\n", | |
"print 'Minimum error of {error}% with re-ordering {permutation} of clusters'.format(**error_and_permutation) \n", | |
"print 'The estimated probabilities of each cluster are : {} VS the real ones : {}'\\\n", | |
" .format(estimated_prob_m[error_and_permutation['permutation']], prob_m)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Minimum error of 16.0% with re-ordering [1 2 0] of clusters\n", | |
"The estimated probabilities of each cluster are : [ 0.34255195 0.42465982 0.23278822] VS the real ones : [ 0.27417081 0.41692802 0.30890117]\n" | |
] | |
} | |
], | |
"prompt_number": 44 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"Comparison to SKlearn implementations" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def my_kmeans(m, x_n):\n", | |
" n = int(x_n.shape[0])\n", | |
" step = 0\n", | |
" centroids_mu_m = x_n[np.random.choice(n, size = m, replace=False),:]\n", | |
" old_estimated_a_n = estimated_a_n = np.repeat(m, n)\n", | |
" dist_n = np.repeat(np.infty, n)\n", | |
" \n", | |
" while step == 0 or not np.all(centroids_a_n == centroids_a_old_n):\n", | |
" centroids_mu_m, new_estimated_a_n, dist_n = kmeans_iteration(x_n, centroids_mu_m)\n", | |
" estimated_a_n, old_estimated_a_n = new_estimated_a_n, estimated_a_n\n", | |
" step += 1\n", | |
" return centroids_a_n\n", | |
"\n", | |
"def my_em(m, x_n):\n", | |
" n = int(x_n.shape[0])\n", | |
" step = 0\n", | |
" centroids_mu_m = x_n[np.random.choice(n, size = m, replace=False),:]\n", | |
" estimated_prob_m = np.repeat(1.0/m, m)\n", | |
" old_pa_n_m = pa_n_m = np.repeat(1.0/3.0, n * m).reshape(n,m)\n", | |
" sigma_m = np.repeat(1, m)\n", | |
" dist_n = np.repeat(np.infty, n)\n", | |
" \n", | |
" while step == 0 or not np.all(pa_n_m == old_pa_n_m):\n", | |
" centroids_mu_m, estimated_prob_m, new_pa_n_m, dist_n_m = em_iteration(x_n, centroids_mu_m, estimated_prob_m, sigma_m)\n", | |
" pa_n_m, old_pa_n_m = new_pa_n_m, pa_n_m \n", | |
" step += 1\n", | |
" return centroids_a_n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 45 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit my_kmeans(m, x_n)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1000 loops, best of 3: 275 us per loop\n" | |
] | |
} | |
], | |
"prompt_number": 46 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from sklearn.cluster import KMeans\n", | |
"km = KMeans(n_clusters=m, init='random', precompute_distances=False, n_init=1)#, n_jobs=1)\n", | |
"\n", | |
"%timeit km.fit(X=x_n)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1000 loops, best of 3: 597 us per loop\n" | |
] | |
} | |
], | |
"prompt_number": 48 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit my_em(m, x_n)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "KeyboardInterrupt", | |
"evalue": "", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-49-dc4bb81ea95f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mget_ipython\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mu'timeit my_em(m, x_n)'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;32m/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc\u001b[0m in \u001b[0;36mmagic\u001b[1;34m(self, arg_s)\u001b[0m\n\u001b[0;32m 2134\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0marg_s\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpartition\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' '\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2135\u001b[0m \u001b[0mmagic_name\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlstrip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprefilter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mESC_MAGIC\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2136\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmagic_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2137\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2138\u001b[0m \u001b[1;31m#-------------------------------------------------------------------------\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc\u001b[0m in \u001b[0;36mrun_line_magic\u001b[1;34m(self, magic_name, line)\u001b[0m\n\u001b[0;32m 2060\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getframe\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstack_depth\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mf_locals\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2061\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2062\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2063\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2064\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m/usr/local/lib/python2.7/dist-packages/IPython/core/magics/execution.pyc\u001b[0m in \u001b[0;36mtimeit\u001b[1;34m(self, line, cell)\u001b[0m\n", | |
"\u001b[1;32m/usr/local/lib/python2.7/dist-packages/IPython/core/magic.pyc\u001b[0m in \u001b[0;36m<lambda>\u001b[1;34m(f, *a, **k)\u001b[0m\n\u001b[0;32m 189\u001b[0m \u001b[1;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 190\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 191\u001b[1;33m \u001b[0mcall\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 192\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 193\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m/usr/local/lib/python2.7/dist-packages/IPython/core/magics/execution.pyc\u001b[0m in \u001b[0;36mtimeit\u001b[1;34m(self, line, cell)\u001b[0m\n\u001b[0;32m 804\u001b[0m \u001b[0mnumber\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 805\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 806\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mtimer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>=\u001b[0m \u001b[1;36m0.2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 807\u001b[0m \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 808\u001b[0m \u001b[0mnumber\u001b[0m \u001b[1;33m*=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m/usr/lib/python2.7/timeit.pyc\u001b[0m in \u001b[0;36mtimeit\u001b[1;34m(self, number)\u001b[0m\n\u001b[0;32m 193\u001b[0m \u001b[0mgc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdisable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 194\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 195\u001b[1;33m \u001b[0mtiming\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minner\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mit\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 196\u001b[0m \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 197\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mgcold\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m<magic-timeit>\u001b[0m in \u001b[0;36minner\u001b[1;34m(_it, _timer)\u001b[0m\n", | |
"\u001b[1;32m<ipython-input-45-88ae020493d2>\u001b[0m in \u001b[0;36mmy_em\u001b[1;34m(m, x_n)\u001b[0m\n\u001b[0;32m 21\u001b[0m \u001b[0mdist_n\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrepeat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minfty\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 22\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 23\u001b[1;33m \u001b[1;32mwhile\u001b[0m \u001b[0mstep\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;32mor\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpa_n_m\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mold_pa_n_m\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 24\u001b[0m \u001b[0mcentroids_mu_m\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mestimated_prob_m\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnew_pa_n_m\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdist_n_m\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mem_iteration\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx_n\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcentroids_mu_m\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mestimated_prob_m\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msigma_m\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 25\u001b[0m \u001b[0mpa_n_m\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mold_pa_n_m\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnew_pa_n_m\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpa_n_m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc\u001b[0m in \u001b[0;36mall\u001b[1;34m(a, axis, out, keepdims)\u001b[0m\n\u001b[0;32m 1707\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1708\u001b[0m \"\"\"\n\u001b[1;32m-> 1709\u001b[1;33m \u001b[0marr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0masanyarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1710\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1711\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32m/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.pyc\u001b[0m in \u001b[0;36masanyarray\u001b[1;34m(a, dtype, order)\u001b[0m\n\u001b[0;32m 370\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 371\u001b[0m \"\"\"\n\u001b[1;32m--> 372\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msubok\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 373\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 374\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mascontiguousarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;31mKeyboardInterrupt\u001b[0m: " | |
] | |
} | |
], | |
"prompt_number": 49 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from sklearn.mixture import GMM\n", | |
"gmm = GMM(n_components=m, covariance_type='diag', random_state=None, thresh=0.01, min_covar=0.001)\n", | |
"fitted_model = gmm.fit(X=x_n)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 51 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"fitted_model.means_\n", | |
"fitted_model.weights_" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 54, | |
"text": [ | |
"array([ 0.30678353, 0.34441477, 0.3488017 ])" | |
] | |
} | |
], | |
"prompt_number": 54 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment