mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
556 lines
158 KiB
Plaintext
556 lines
158 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Density Estimation: Gaussian Mixture Models\n",
|
||
|
"\n",
|
||
|
"Credits: Forked from [PyCon 2015 Scikit-learn Tutorial](https://github.com/jakevdp/sklearn_pycon2015) by Jake VanderPlas"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Here we'll explore **Gaussian Mixture Models**, which is an unsupervised clustering & density estimation technique.\n",
|
||
|
"\n",
|
||
|
"We'll start with our standard set of initial imports"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%matplotlib inline\n",
|
||
|
"import numpy as np\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"from scipy import stats\n",
|
||
|
"\n",
|
||
|
"# use seaborn plotting defaults\n",
|
||
|
"import seaborn as sns; sns.set()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Introducing Gaussian Mixture Models\n",
|
||
|
"\n",
|
||
|
"We previously saw an example of K-Means, which is a clustering algorithm which is most often fit using an expectation-maximization approach.\n",
|
||
|
"\n",
|
||
|
"Here we'll consider an extension to this which is suitable for both **clustering** and **density estimation**.\n",
|
||
|
"\n",
|
||
|
"For example, imagine we have some one-dimensional data in a particular distribution:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"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TOMqy1KlXNRPYX/3aATZte14XKJKn9DGutWs5EJqlXeM5akqTCGdaSJBXOsJYkqXCG\ntSRJhTOsJUkqnGEtSVLhDGtJkgpnWEuSVDjDWpKkwjmDmVSY8+fOcuLE4vOeb916Gf39/StYkaRu\nM6ylwjw/dZIDXx9nw9BT89rOTD7D7Tddx7Ztl3ehMkndYlhLBXLec0mNPGctSVLhDGtJkgrnYXCt\nOefPneXnP/854+NT89qaXbglSaUyrLXmPD91klv/4w/YMLRlXtvJJx/l4ku3d6EqSVo+w1pr0mIX\naJ2ZfLoL1UjShWka1hHRB9wBXAG8ANyQmU/Mec0G4LvARzMzW+kjSZJaV3WB2fVAf2ZeBdwMHGhs\njIgdwAPAG4GZVvpIkqSlqQrrq4GjAJn5MLBjTns/tXDOJfSRJElLUHXOeiNwqmH5XET0ZeZ5gMx8\nCCAiWu6zmOHhwZaL7mWOU83ExEBH3nfz5oG2jfFqqBHcppbCsWqN49R+VWF9Cmgc9crQXWYfxsZO\nV72k5w0PD/bUOE1PTzM6uvCtVp26BWt8fKptY7zQrWPtet921dhr29SFcKxa4zi1ZqlfaKrCegTY\nDRyKiJ3AIy2853L6SPOMjh5n3/4jK3YLlg/QkFSqqrA+DOyKiJH68t6I2AMMZObBVvu0oU71qJW8\nBcsHaEgqVdOwzswZ4MY5q48t8LprK/pIq4IP0JBUIidFkS5AN86rS+o9hrV0AVb6vLqk3mRYSxfI\nqU0ldZqPyJQkqXCGtSRJhTOsJUkqnGEtSVLhDGtJkgpnWEuSVDhv3ZJasNi84U58ImklGNZSCxab\nN9yJTyStBMNaatFCk5848YmkleA5a0mSCueetbrKB2FIUjXDWl3lgzAkqZphra7zQRiS1JznrCVJ\nKpxhLUlS4QxrSZIKZ1h
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x109842cf8>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"np.random.seed(2)\n",
|
||
|
"x = np.concatenate([np.random.normal(0, 2, 2000),\n",
|
||
|
" np.random.normal(5, 5, 2000),\n",
|
||
|
" np.random.normal(3, 0.5, 600)])\n",
|
||
|
"plt.hist(x, 80, normed=True)\n",
|
||
|
"plt.xlim(-10, 20);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Gaussian mixture models will allow us to approximate this density:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAesAAAFVCAYAAADPM8ekAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xl8VPW9//HXLJlJZklCFnbZBI4oiwoqgoILuOPSWq21\nttVrXdre2l9Xe1tt771tb3ut3a7XLlZ77eJSUVwAEXEBjIKCC8hyQPadJDPZk5lkZn5/zCQEApkE\nJnMmmffz8eBR5nznTD45jbzzPd/v+X5tsVgMERERyVx2qwsQERGRzimsRUREMpzCWkREJMMprEVE\nRDKcwlpERCTDKaxFREQynLOzRsMw7MDDwEQgBNxumuaWI97jAV4FbjNN00yc82dgLBAFvmyaptkT\nxYuIiGSDZD3rawGXaZrTgHuBB9s3GoYxBVgGjARaH9i+BPCapnke8B/AT1NasYiISJZJFtbTgUUA\npmmuBKYc0e4iHujte86NQIFhGDagAAinplQREZHs1OltcCAfqGn3OmIYht00zSiAaZpvAxiG0f6c\nMiAX2AgUA3NSVq2IiEgWShbWNYC/3eu2oO7Ed4Ey0zR/YBjGUOB1wzDGm6Z5zB52LBaL2Wy2rlUs\nIiLS+3Ur9JKFdRnxnvEzhmFMBdZ04TO9HOqNB4EcwNHZCTabjfLy2i58dHYrLfXrOnWRrl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PFmvwFXTshJ4eYx6MxaxEBFNYiaeGuDQLQlJ/8GetW\nUX++lhsVEUBhLZIWrT3rUFd71kAsPx+7boOLCAprkbTo7pg1QMzv1wQzEQEU1iJp4a47nrDOxxYO\nQyjUU2WJSC+hsBZJg7a
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x109a3cc88>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"from sklearn.mixture import GMM\n",
|
||
|
"clf = GMM(4, n_iter=500, random_state=3).fit(x)\n",
|
||
|
"xpdf = np.linspace(-10, 20, 1000)\n",
|
||
|
"density = np.exp(clf.score(xpdf))\n",
|
||
|
"\n",
|
||
|
"plt.hist(x, 80, normed=True, alpha=0.5)\n",
|
||
|
"plt.plot(xpdf, density, '-r')\n",
|
||
|
"plt.xlim(-10, 20);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Note that this density is fit using a **mixture of Gaussians**, which we can examine by looking at the ``means_``, ``covars_``, and ``weights_`` attributes:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([[ 4.5601338 ],\n",
|
||
|
" [ 0.0861325 ],\n",
|
||
|
" [ 3.01890623],\n",
|
||
|
" [ 6.87627234]])"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"clf.means_"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([[ 30.34748627],\n",
|
||
|
" [ 4.30521863],\n",
|
||
|
" [ 0.19750802],\n",
|
||
|
" [ 14.78230876]])"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"clf.covars_"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([ 0.27613209, 0.48308463, 0.11612442, 0.12465886])"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"clf.weights_"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAesAAAFVCAYAAADPM8ekAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XmcXHWd7//XOVVd1VW9JZ10QthXD8iiSNAYIIAKKsjm\nOCjj5aEwuDDOVX86Kg7KHWeGe/UiLiPCddw3RBFRFoGwKZDIqhhFcoAECAESOr1Vd62nzjm/P6q6\nU91VXVtXd1d3vZ+PRz+SOt9zTn37pNOf+nxXw/d9REREpHmZ810BERERKU/BWkREpMkpWIuIiDQ5\nBWsREZEmp2AtIiLS5BSsRUREmlywXKFlWSZwNXAUkAYusm17y5RzosCdwIW2bdv5a74DvArwgA/Y\ntm3PRuVF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Z+1qT5rEUHB\nWmTOjG+P6S+dfnvMceqzFpFCCtYic8QcHMztthWstLxBwWhwBWsRQcFaZM4YgwP4VcyxBvA6O3PX\njGqAmYhUXsFMROrkeR7JZDL3wvdZPjRIZo/DicfjJBIJfH/6Zfb9bs2zFpHdFKxFZkkymeSRJ7YT\nbo8QSMTZP5NhONzJpi0DjAwP0h6JEo2WvnZigJmawUUENYOLzKpwe4RIJEp3Jg2A27ucSCRKONxe\n9joNMBORQgrWInMgNJybtuX0VB4JDuB1jPdZK1iLiIK1yJxoiw0D1Qdr2trwo1H1WYsIoGAtMifa\n8pl1ptpgTX7nLS03KiIoWIvMid2Z9ZKqr/G7uzHVDC4iKFiLzInQ8BAATk9186wB/K4uDTATEUDB\nWmROtMXGg3UNmXVXN0Y
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10aab3390>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.hist(x, 80, normed=True, alpha=0.3)\n",
|
||
|
"plt.plot(xpdf, density, '-r')\n",
|
||
|
"\n",
|
||
|
"for i in range(clf.n_components):\n",
|
||
|
" pdf = clf.weights_[i] * stats.norm(clf.means_[i, 0],\n",
|
||
|
" np.sqrt(clf.covars_[i, 0])).pdf(xpdf)\n",
|
||
|
" plt.fill(xpdf, pdf, facecolor='gray',\n",
|
||
|
" edgecolor='none', alpha=0.3)\n",
|
||
|
"plt.xlim(-10, 20);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"These individual Gaussian distributions are fit using an expectation-maximization method, much as in K means, except that rather than explicit cluster assignment, the **posterior probability** is used to compute the weighted mean and covariance.\n",
|
||
|
"Somewhat surprisingly, this algorithm **provably** converges to the optimum (though the optimum is not necessarily global)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## How many Gaussians?\n",
|
||
|
"\n",
|
||
|
"Given a model, we can use one of several means to evaluate how well it fits the data.\n",
|
||
|
"For example, there is the Aikaki Information Criterion (AIC) and the Bayesian Information Criterion (BIC)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"25696.4735953\n",
|
||
|
"25625.7016679\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(clf.bic(x))\n",
|
||
|
"print(clf.aic(x))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Let's take a look at these as a function of the number of gaussians:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAfEAAAFVCAYAAAD/v9aFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl8XXWd//HXuTf3Zr3JTZN0TZuu+bK0tIAKCII6yIDy\nU1FAUQdxZ1HUwdEZRhn1oTM/FxCVAg6L4jIsRVBAB6riT4QZla1lafmWQveWNm1zsy93Ob8/zkma\ntGm23uTk3vt+Ph595Obcc04+36bNO9/v+Z7vcVzXRURERHJPKOgCREREZHwU4iIiIjlKIS4iIpKj\nFOIiIiI5SiEuIiKSoxTiIiIiOapouDeNMRHgdqABKAa+AfwVuAWIAw5wsbV2szHmHOAa/9AnrbVX\nGmNKgZ8DdUAb8GFr7V5j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BLxphVwEXA+cC/4Q2zA1QCLf7rNqDK39Y64Mu0AQtHKrSu\nLjZiY5bNPIr/3fY0TnEnm5s6OOX4+hGPmSpG075cpvbltnxuXz63DdS+fDdsiAMYY+YC9wErrbV3\nGmOuAx7w334Q+CZQAszB65XPB3qMMZvxwrrS3zcGJPxtA//WK/3tw2pqGrln3VA2j//lacKV+3hq\n3WucefzsEY+ZCurqYqNqX65S+3JbPrcvn9sGal+uG80vKMMOpxtjZgCrgS9aa3/ib34ceIf/+gzg\nBWvtl6y1J1tr3wL8BLjOWvsI8ATwdn/fc4DHrLVtQK8xZqExxgHOAh4bS8MOp28d9Yrprbyyo4We\n3nQ2TisiIjIljdQTvxp
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10ac82b38>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"n_estimators = np.arange(1, 10)\n",
|
||
|
"clfs = [GMM(n, n_iter=1000).fit(x) for n in n_estimators]\n",
|
||
|
"bics = [clf.bic(x) for clf in clfs]\n",
|
||
|
"aics = [clf.aic(x) for clf in clfs]\n",
|
||
|
"\n",
|
||
|
"plt.plot(n_estimators, bics, label='BIC')\n",
|
||
|
"plt.plot(n_estimators, aics, label='AIC')\n",
|
||
|
"plt.legend();"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"It appears that for both the AIC and BIC, 4 components is preferred."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Example: GMM For Outlier Detection\n",
|
||
|
"\n",
|
||
|
"GMM is what's known as a **Generative Model**: it's a probabilistic model from which a dataset can be generated.\n",
|
||
|
"One thing that generative models can be useful for is **outlier detection**: we can simply evaluate the likelihood of each point under the generative model; the points with a suitably low likelihood (where \"suitable\" is up to your own bias/variance preference) can be labeld outliers.\n",
|
||
|
"\n",
|
||
|
"Let's take a look at this by defining a new dataset with some outliers:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"np.random.seed(0)\n",
|
||
|
"\n",
|
||
|
"# Add 20 outliers\n",
|
||
|
"true_outliers = np.sort(np.random.randint(0, len(x), 20))\n",
|
||
|
"y = x.copy()\n",
|
||
|
"y[true_outliers] += 50 * np.random.randn(20)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAe4AAAFVCAYAAAApGgzgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XmcZGV97/HPqbW7qpfZemYCgywCjwRFVCQsOkB0cJ0r\nmpv4IttLFIloEhOzIUazXE3uDRfMzUsxhkW93kTvBUQQlE1QoBEMuDCCPOMgiyMD9PRaXVVd1V1V\n949zqqdoerp6qapzTp3v+/XixdQ5Vaee31T3fOt5znOe49RqNURERCQcYn43QERERJZPwS0iIhIi\nCm4REZEQUXCLiIiEiIJbREQkRBTcIiIiIZJYaqcxJgZcDpwAlIDzrbWPL3hOBrgdeK+11jZs3ww8\nBLzBWru71Q0XERGJomY97nOAlLX2NOAi4NLGncaYk4C7gSOBWsP2JPB5IN/S1oqIiERcs+A+HbgF\nwFr7AHDSgv0p3HC3C7ZfAnwO2NeCNoqIiIinWXAPAFMNjyve8DkA1tr7rLV7G19gjHkPMGKtvc3b\n5LSioSIiItLkHDduaPc3PI5Za6tNXnMeUDPGvBE4EfiSMeYd1trnDvaCWq1Wcx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3rczYfKU6UZqDa98Z6I\ndBkFt0iA1HvcS13HDTDnLcJSf76IRIeCWyRIljOrHCj3uIuwxBTcIpGj4BYJkOWsVQ4w1+sOpce8\noXURiQ4Ft0iALGetcjiwXnksr+AWiRoFt0iAOIU81XQaYkv/as4Hty4JE4kcBbdIgDiFAlXvfttL\nqU9O0zlukehRcIsEiFM
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10ac70828>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"clf = GMM(4, n_iter=500, random_state=0).fit(y)\n",
|
||
|
"xpdf = np.linspace(-10, 20, 1000)\n",
|
||
|
"density_noise = np.exp(clf.score(xpdf))\n",
|
||
|
"\n",
|
||
|
"plt.hist(y, 80, normed=True, alpha=0.5)\n",
|
||
|
"plt.plot(xpdf, density_noise, '-r')\n",
|
||
|
"#plt.xlim(-10, 20);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Now let's evaluate the log-likelihood of each point under the model, and plot these as a function of ``y``:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAesAAAFVCAYAAADPM8ekAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAF+JJREFUeJzt3X+sZGddx/H3vfSH7b13u4bselkluo36bZvwI2UttGJb\n4rXKBMKP6B+VHylZ0CIQCgQCxVSDpBoRFCLyoyI1QKqgVURWCjWkQCvdXtAgsj6CuTbRpfQaqXt3\nXSh0xj/OzHZ29v6cOTPnOWfer6TpzNk7Z57vnbnzmec5z3nOTKfTQZIk5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Dzl4W5LGyWPW0g6cd975p07b6r9U5DTymLU0OYa1pKEZ0tJkOI4n\n7cDKylHm5uaZm5tnZeVo1c2RNCXsWUs7ZEhLmjR71tI2tVpLznregL8babwMa2kbPE1pY/5upPEz\nrCWNxMuFSuNnWEvbNDc3z4EDlzkDekD/Km6u6CaNhxPMpC30hnm1Ps+3lsbPsJY0MkNaGi/DWtqC\nPUdJVTOspW0wpLfmFxppfJxgJmlknr4ljZdhLUlS5hwGlzQyj+tL42VYSyqFIS2Nj8PgkiRlzrCW\nNuDFKSTlwrCW1uHsZkk5MawlScqcYS1JUuZKmw0eEecDtwG7gYeBF6aUvlXW/qVJ8rKPknJSZs/6\nxcCRlNJVwJ8Dry9x39JEedlHSTkp8zzrk8Bju7cvoOhdS7XkIh+jWVhYoNOBlZWjVTdFaoSZTqez\n4wdFxEHghr5NHeCVwPuANvCDwJUppW9sspudP7Gk7C0sLHD8+HEA5ufnWVtbq7hFUlZmhnrQMGG9\nnoh4P3BfSumWiHgC8OG
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x10271c470>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"log_likelihood = clf.score_samples(y)[0]\n",
|
||
|
"plt.plot(y, log_likelihood, '.k');"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"true outliers:\n",
|
||
|
"[ 99 537 705 1033 1653 1701 1871 2046 2135 2163 2222 2496 2599 2607 2732\n",
|
||
|
" 2893 2897 3264 3468 4373]\n",
|
||
|
"\n",
|
||
|
"detected outliers:\n",
|
||
|
"[ 537 705 1653 2046 2135 2163 2496 2732 2893 2897 3067 3468 4373]\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"detected_outliers = np.where(log_likelihood < -9)[0]\n",
|
||
|
"\n",
|
||
|
"print(\"true outliers:\")\n",
|
||
|
"print(true_outliers)\n",
|
||
|
"print(\"\\ndetected outliers:\")\n",
|
||
|
"print(detected_outliers)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"The algorithm misses a few of these points, which is to be expected (some of the \"outliers\" actually land in the middle of the distribution!)\n",
|
||
|
"\n",
|
||
|
"Here are the outliers that were missed:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{99, 1033, 1701, 1871, 2222, 2599, 2607, 3264}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 14,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"set(true_outliers) - set(detected_outliers)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"And here are the non-outliers which were spuriously labeled outliers:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{3067}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 15,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"set(detected_outliers) - set(true_outliers)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Finally, we should note that although all of the above is done in one dimension, GMM does generalize to multiple dimensions, as we'll see in the breakout session."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Other Density Estimators\n",
|
||
|
"\n",
|
||
|
"The other main density estimator that you might find useful is *Kernel Density Estimation*, which is available via ``sklearn.neighbors.KernelDensity``. In some ways, this can be thought of as a generalization of GMM where there is a gaussian placed at the location of *every* training point!"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAesAAAFVCAYAAADPM8ekAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XmcFPWd//FX3zPT3XMfwAy3UCAKIqgIiifGqESziUlM\nslk1hzG7McZsou4mWc3uJptfYjZrspqsMfehkXjjgXgLAiJyQyE3A3MfPdM9R08fvz9qGIdrDuie\n7pl+Px8PHtL9rar+TDnDe77f+ta3bPF4HBEREUlf9lQXICIiIn1TWIuIiKQ5hbWIiEiaU1iLiIik\nOYW1iIhImlNYi4iIpDlnX42GYdiBB4CZQCfwBdM0dx21TQ7wEnCzaZpm9z6/AqYCMeCLpmmayShe\nREQkE/TXs74OcJumOR+4C7ivd6NhGHOBN4CJwOEbtq8AvKZpXgB8D/jPhFYsIiKSYfoL6wXACwCm\naa4G5h7V7sYK9N4953YgzzAMG5AHhBNTqoiISGbqcxgcyAVaer2OGoZhN00zBmCa5koAwzB677MC\nyAK2A0XA4oRVKyIikoH6C+sWwN/rdU9Q9+FbwArTNP/VMIwK4BXDMM4wTfOEPex4PB632WwDq1hE\nRGT4G1To9RfWK7B6xo8ZhjEP2DiAY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m5LHC2tbcPJSliUgaU1iL\nDKFAAIrjdXjinbQUjTruNu1ZfgDizepZi4hFYS0yhHrfttVSfPyedUe2D4DOGoW1iFgU1iJDqHdY\nn2gYPOz1Wv9VWItIN4W
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x109ad97b8>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"from sklearn.neighbors import KernelDensity\n",
|
||
|
"kde = KernelDensity(0.15).fit(x[:, None])\n",
|
||
|
"density_kde = np.exp(kde.score_samples(xpdf[:, None]))\n",
|
||
|
"\n",
|
||
|
"plt.hist(x, 80, normed=True, alpha=0.5)\n",
|
||
|
"plt.plot(xpdf, density, '-b', label='GMM')\n",
|
||
|
"plt.plot(xpdf, density_kde, '-r', label='KDE')\n",
|
||
|
"plt.xlim(-10, 20)\n",
|
||
|
"plt.legend();"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"All of these density estimators can be viewed as **Generative models** of the data: that is, that is, the model tells us how more data can be created which fits the model."
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 2",
|
||
|
"language": "python",
|
||
|
"name": "python2"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 2
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython2",
|
||
|
"version": "2.7.10"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 0
|
||
|
}
|