mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
394 lines
122 KiB
Python
394 lines
122 KiB
Python
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"<!--BOOK_INFORMATION-->\n",
|
|||
|
"<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PDSH-cover-small.png\">\n",
|
|||
|
"*This notebook contains an excerpt from the [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/PythonDataScienceHandbook).*\n",
|
|||
|
"\n",
|
|||
|
"*The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please consider supporting the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!*\n",
|
|||
|
"\n",
|
|||
|
"*No changes were made to the contents of this notebook from the original.*"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"<!--NAVIGATION-->\n",
|
|||
|
"< [Density and Contour Plots](04.04-Density-and-Contour-Plots.ipynb) | [Contents](Index.ipynb) | [Customizing Plot Legends](04.06-Customizing-Legends.ipynb) >"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"# Histograms, Binnings, and Density"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"A simple histogram can be a great first step in understanding a dataset.\n",
|
|||
|
"Earlier, we saw a preview of Matplotlib's histogram function (see [Comparisons, Masks, and Boolean Logic](02.06-Boolean-Arrays-and-Masks.ipynb)), which creates a basic histogram in one line, once the normal boiler-plate imports are done:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 1,
|
|||
|
"metadata": {
|
|||
|
"collapsed": true
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"%matplotlib inline\n",
|
|||
|
"import numpy as np\n",
|
|||
|
"import matplotlib.pyplot as plt\n",
|
|||
|
"plt.style.use('seaborn-white')\n",
|
|||
|
"\n",
|
|||
|
"data = np.random.randn(1000)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 2,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD/CAYAAADhYy38AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEhxJREFUeJzt3XtsU/X/x/FXu27AbkLIBM3ManCiuEXjGkMkIsowW9Ao\nILpLC+iizksyrREZIEMUp8ZblPETWTQ6iLIoRP8h6hCCIvFCAjiQRZ2oQ5nMP6StzG7j/P5Aq3zZ\n3e6c8eH5+GttT/t5l23Ptac9xWVZliUAwGnP7fQAAID4IOgAYAiCDgCGIOgAYAiCDgCGIOgAYAhP\nbxd2dnZq8eLFOnTokDo6OlReXq5zzjlHd911l7xerySpuLhYhYWFqq+v14YNG5SYmKjy8nJNmzbN\nhvEBAH9z9fY+9I0bN6qpqUmVlZX6/fffddNNN+nee+9VOBzWggULYtu1tbXptttu06ZNm9Te3q7i\n4mJt3LhRiYmJdtwHAID6eIReWFiogoICSdLx48fl8Xi0b98+NTc3q6GhQV6vV5WVldq7d6/y8vLk\n8XiUmpoqr9erpqYm5eTk2HInAAB9BH3UqFGSpHA4rIqKCt1///2KRqOaO3euJk2apDVr1mjVqlW6\n+OKLlZaWFrtecnKyQqHQ0E4OADhJr0GX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xxx8qKyvTggULtGfPHu3fv18+n0+SNHXqVO3cuTOugwIAejeoR+gjR45UWVmZ5s6dq4MH\nD+qOO+6QZVmxy1NSUhQKheI2JACgb4MKutfrVVZWVuzr0aNHa//+/bHLI5GI0tPT4zMhAKBfBrXL\n5Z133tGTTz4pSWptbVU
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x1023e5a58>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plt.hist(data);"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"The ``hist()`` function has many options to tune both the calculation and the display; \n",
|
|||
|
"here's an example of a more customized histogram:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 3,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD/CAYAAAAKVJb/AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGjxJREFUeJzt3X9wVPW9//HXLptNgF1M5Qb8tkmTYCfMHXFSA/XbqzfT\nH7iOWgoW2TZAgtQMTtt/bImjpVbY2JssdSztOLjTFm3H+GunI6ntzRT5GqGZmvlerfm6Gbd+E5QC\nCl7iokbYzZLNZs/9g2EhJtkTMbsbDs/HjDN79vN5H99L4JVPPjln12YYhiEAgKXY890AAGD6Ee4A\nYEGEOwBYEOEOABZEuAOABRHuAGBBDrMJhmHI5/Opv79fTqdTLS0tKisrGzdv69atKi4u1ubNmyVJ\nq1evlsvlkiSVlpaqtbV1mlsHAEzGNNw7OzuVSCQUDAbV29srv9+vQCAwZk4wGNSBAwd07bXXSpIS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qmpUVdX\nlyQpFAqpqqpqzPj999+vkZERBQKB9PbM7t27tX37dknSwMCAYrGYSkpKprt3AMAkTFfuHo9H3d3d\nqqurkyT5/X51dHQoHo/rqquuUnt7u5YuXaqGhgbZbDZt2LBBXq9X9957r9atWye73a7W1la2ZC5R\nz71ySLHTmbdcDIP9FmC6mYa7zWZTc3PzmOcqKyvTj994440J637xi198ytZgBSlDShHeQM6xnAYA\nCyLcAcCCTLdlAMwsb/73Rzo+OJRxztyiAl39+ctz1BFmIsIduMgcHxzS8cHMcy53FRLulzi2ZQDA\nggh3ALAgwh0ALIhwBwA
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x10bfc0588>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plt.hist(data, bins=30, normed=True, alpha=0.5,\n",
|
|||
|
" histtype='stepfilled', color='steelblue',\n",
|
|||
|
" edgecolor='none');"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"The ``plt.hist`` docstring has more information on other customization options available.\n",
|
|||
|
"I find this combination of ``histtype='stepfilled'`` along with some transparency ``alpha`` to be very useful when comparing histograms of several distributions:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD/CAYAAADhYy38AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3WtwFOedLvCne3qumtH9AgIhCWwZYzaKhS8bx7LxibXG\nl8LxCdoVAeFKpbK7SW1VNrAb116CIVvUxE7Kqdqy2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w0qc097MbT\n0tMT9nIlIlLDQE8TRaVFqvfLMXmOKyGiTMWJRUREOsFAJyLSCQY6EZFOMNCJiHSCgU63TFFkDA0N\nIRgMaF0K0bzGUS50SwwGCcFgMd56qwuKAuTk5GtdEtG8xUCfQ4qiIBod38D5+gzPmxGLxRCJROLH\nphNRFLFo0SqtyyAiMNDnVPvxdhw5dQSiON7TZSxMPFXfmmXFweMHcfD4wfE7HKmscPzDIxAIwO/3\np/aFiCjpGOhzyBf0wbrYioKSgps+pqCkAChJYVGf88knnfjkkyuQJAmiWDh3L0xEt4yBThMEgxHY\n7SuRn79Q61KIaIYSBrqiKNi2bRu6urpgMpmwY8cOlJWVxdvff/99/PznP4coinjqqaewadOmlBZM\nRETqEgZ6S0sLwuEwmpqacPz4cTidTuzcuRPA+CYNL730Evbu3Qur1YonnngCa9euRW5ubsoLp9nx\n+Xxwu93x24WFhTAauewukR4kDPS2tjbU1tYCAKqrq9HZ2RlvE0UR7777LkRRjO+PyXBIbwePHET3\nSDeMRiP8Pj8ev/txLF+
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x10e666358>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"x1 = np.random.normal(0, 0.8, 1000)\n",
|
|||
|
"x2 = np.random.normal(-2, 1, 1000)\n",
|
|||
|
"x3 = np.random.normal(3, 2, 1000)\n",
|
|||
|
"\n",
|
|||
|
"kwargs = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=40)\n",
|
|||
|
"\n",
|
|||
|
"plt.hist(x1, **kwargs)\n",
|
|||
|
"plt.hist(x2, **kwargs)\n",
|
|||
|
"plt.hist(x3, **kwargs);"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"If you would like to simply compute the histogram (that is, count the number of points in a given bin) and not display it, the ``np.histogram()`` function is available:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"[ 12 190 468 301 29]\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"counts, bin_edges = np.histogram(data, bins=5)\n",
|
|||
|
"print(counts)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Two-Dimensional Histograms and Binnings\n",
|
|||
|
"\n",
|
|||
|
"Just as we create histograms in one dimension by dividing the number-line into bins, we can also create histograms in two-dimensions by dividing points among two-dimensional bins.\n",
|
|||
|
"We'll take a brief look at several ways to do this here.\n",
|
|||
|
"We'll start by defining some data—an ``x`` and ``y`` array drawn from a multivariate Gaussian distribution:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 6,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"mean = [0, 0]\n",
|
|||
|
"cov = [[1, 1], [1, 2]]\n",
|
|||
|
"x, y = np.random.multivariate_normal(mean, cov, 10000).T"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### ``plt.hist2d``: Two-dimensional histogram\n",
|
|||
|
"\n",
|
|||
|
"One straightforward way to plot a two-dimensional histogram is to use Matplotlib's ``plt.hist2d`` function:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 12,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWgAAAD6CAYAAACMPN0pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X1UVHX+B/D3vTw7PPmAaxJhseqKkhW0+csy7UDh1p5y\nxZQUSmh73mNhCYaCYjTVaa22xfWprNCF0mjpbNupyMwyN0uT0sLNpzIpQ3yIQXRm4P7+ICZNne+X\nmcvMndv75ZlzhPneO59B+Pjlez/381U0TdNARESGo/o7ACIiOjMmaCIig2KCJiIyKCZoIiKDYoIm\nIjKo4J468fHjx7Glfhv69YtDUHBQT70MkenIFFbJ1F6pqqJDNL7R3t6Og01NuGTkCISHh3t0jiNH\njsBms0mNjYyMRGxsrEev40s9lqC3bduG6blTe+r0RGRCq1atQlpaWrePO3LkCNIuG40gOKXGx8TE\n4K233jJ8ku6xBB0XFwcAWPHiKvxmwICeehki0/k1zqAPfP89pudOdeWN7rLZbAiCEwfCfw+n4n4G\nHqwdB45ugs1m+/Um6KCgzmWN3wwYgPj4c3vqZYhMRyZBd0gk6KAAStBduvKGp5xqONrVXu4HdXj1\nEj7VYwmaiMjnFLXzIRoTIJigicg81KDOh1uBU7TABE1EJqIAimhpJ3CWfpigyZBk1mEV4Q+ifnwZ\nj8x5ggInx/iWokgscQTOF48JmojMQ5GYQTNBExH5AS8SEhEZFGfQREQGpcMMur6+Hk888QQqKyux\nc+dOlJSUAAASExNRXl4OVVVRXl6OLVu2wGKxAAAWLVqEyMhIXd7CyZigicg8VFVcZqedPUEvX74c\ntbW1rsT75JNPYubMmUhNTcXs2bOxdu1apKenY/v27Xj22Wd7/E7EwFmMISISUn+eRZ/t4SbtJSYm\noqKiwvXx3//+d6SmpsJut6OpqQlRUVHQNA1ff/01SkpKkJ2djVdeeaXH3g1n0ERkHqrS+RCNOYuM\njAzs37/f9bGiKGhsbM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/jPy1U3c9IROQnyk9ldm4f3bhKeHLr0tbWVkRHRyMiIgI5OTkICwuDxWLB\nqFGj0NDQ0BNvhwmaiExEZnmjG1UcycnJ+PjjjwEA69evR2pqKnbv3o3s7GxomgaHw4HNmzdj+PDh\nPfJ2PFricDqdeOihh7B//344HA7ceeeduPrqq/WOjYioW1RVgSJY4tBUBeLFn06FhYWYO3cuHA4H\nkpKSkJmZCUVRcOONN2LSpEkICQnBhAkTkJSU5H3wZ+BRgn7ttdfQu3dvPP744zh69ChuvPFGJmgi\n8juZKjvRDDo+Ph7V1dUAgEGDBqGysvK0MXl5ecjLy/MwSnkeJejx48cjMzMTANDR0YHgYF5rJCL/\nk9mT0PR3EkZERAAAbDYbZsyYgfvvv1/XoIiIPCG7xBEoPJ76fvfdd7j33nsxbdo0/OEPf9AzJjqJ\nTDtIme83u0R51wmJMTItNQ+3um/xKVPO13DoR+EYGfuOinf+TogJFY7pHS4e088SJhwj02q1T6T4\ntWRakoZI7Ootwyg7dssRz6C1ALrX26MEffDgQeTn56OkpASjRo3SOyYiIo+YrFeSZ2V2S5YswY8/\n/ohFixYhJycHubm5sNvFMxUiop4krIGWWaM2EI9m0MXFxSguLtY7FiIir5htBs3yCyIyjc4EzV29\niYgMhzNoIiKDUlWls2GS20GBk6GZoInIRLgnIRmMo11cpyrTvlOmPeXRY+5rnAFgW9MRt883HxPX\nUjcJaqkB4KOdzcIxw84VN1SXieeapHDhGJka5zCJ2mSZlqQyNc4yE0WZOnuZ9qdGwSUOIiKDkimj\nM32ZHRGREXEGTURkUJx
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x1106647f0>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plt.hist2d(x, y, bins=30, cmap='Blues')\n",
|
|||
|
"cb = plt.colorbar()\n",
|
|||
|
"cb.set_label('counts in bin')"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"Just as with ``plt.hist``, ``plt.hist2d`` has a number of extra options to fine-tune the plot and the binning, which are nicely outlined in the function docstring.\n",
|
|||
|
"Further, just as ``plt.hist`` has a counterpart in ``np.histogram``, ``plt.hist2d`` has a counterpart in ``np.histogram2d``, which can be used as follows:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 8,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"counts, xedges, yedges = np.histogram2d(x, y, bins=30)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"For the generalization of this histogram binning in dimensions higher than two, see the ``np.histogramdd`` function."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### ``plt.hexbin``: Hexagonal binnings\n",
|
|||
|
"\n",
|
|||
|
"The two-dimensional histogram creates a tesselation of squares across the axes.\n",
|
|||
|
"Another natural shape for such a tesselation is the regular hexagon.\n",
|
|||
|
"For this purpose, Matplotlib provides the ``plt.hexbin`` routine, which will represents a two-dimensional dataset binned within a grid of hexagons:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 9,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWgAAAD6CAYAAACMPN0pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsvX2QJVV9Pv6c02/3dd5nWfaFXViBC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nlWylpvi+NrXvoYH\nH3wwJN5du3bh+uuvx86dO3HrrbfioYcewkUXXYQnnngCf/d3f4exsbGB9r8Xa8fWSWHNIDTyCdQU\nXPQtvRZCgDE5qSXg+0z0sAwXAi7jcJiAw+TvvCfI4wL1toemw9CwGVoO6/Pg8BhHre1hoeVhtumi\n3va6ctZCCLRdhkPzbczUHRxesDHfdPtiXMZRtxlsJmB7Aq7Hu82G/PN2mYDtcTQdBsdjfTEu42jY\nDE2HYaHN0Hb7YxyPo+lwOB5H2+V9xwpACWBoBKbWX4UFkNuKprZIA6Xlzw0Moo2lIFotJu0nCdu2\nbcOdd94Z/v3EE09g586dAIALL7wQjz76KIQQeOmll3DLLbfgyiuvxLe//e0VOx81glYYGKKWnv3b\nup3l4sJEJI73OLoBksS5b2BECNB2ed/koMcFPIdBowQ6JbC9/hjb47A9DlMn0AhBre11KTgAoOlI\nAi2aGoqGBofxvv4wATBPgBIBjcT7eri+mkLzR8Eu65/Qsz0B22MwNQKNAG7MDKTnu+3plMDQAqIB\not/nddqJXYzMLk6C6IdGtnePivOS72orO5Y7gr744ouxb9++8O/oQ7FcLqNWq6HVauGqq67C+973\nPnieh6uvvhrnnHMOzjj
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x10e666550>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plt.hexbin(x, y, gridsize=30, cmap='Blues')\n",
|
|||
|
"cb = plt.colorbar(label='count in bin')"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"``plt.hexbin`` has a number of interesting options, including the ability to specify weights for each point, and to change the output in each bin to any NumPy aggregate (mean of weights, standard deviation of weights, etc.)."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"### Kernel density estimation\n",
|
|||
|
"\n",
|
|||
|
"Another common method of evaluating densities in multiple dimensions is *kernel density estimation* (KDE).\n",
|
|||
|
"This will be discussed more fully in [In-Depth: Kernel Density Estimation](05.13-Kernel-Density-Estimation.ipynb), but for now we'll simply mention that KDE can be thought of as a way to \"smear out\" the points in space and add up the result to obtain a smooth function.\n",
|
|||
|
"One extremely quick and simple KDE implementation exists in the ``scipy.stats`` package.\n",
|
|||
|
"Here is a quick example of using the KDE on this data:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 10,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD/CAYAAADoiI2GAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnX2wXVV5/79r7XPuvUkuEUSw01/TkEbjANZAEltbCxMg\nQbB1LJNcmwimNKnjy1ipgAkhYkg0E2SspWW8AtqqBEuYsTgysVjnThBsZIZADcONwpRMeKkiCojk\n3obcs89evz/W27Ne9j7nviQ592Q9mX3321r77d58zvd817PWZkIIgRQpUqRIMe2CH+8LSJEiRYoU\nE4sE8BQpUqSYppEAniJFihTTNBLAU6RIkWKaRgJ4ihQpUkzTSABPkSJFimkatYlWvOOOO7B79240\nGg188IMfxIoVK6byulKkSJEiRYuYEMAfeeQR/OQnP8HOnTvxf//3f/jXf/3Xqb6uFClSpEjRIthE\nOvJ86UtfAmMM//M//4PR0VGsX78eZ5999tG4vhQpUqRIURITUuC/+c1v8Itf/AK33347nn/+eXzs\nYx/D97///am+thQpUqRIURETAvjJJ5+M+fPno1arYd68eejt7cUrr7yCN77xjabM66+/juHhYZx2\n2mnIsmzKLjhFihTdF81mE7/+9a/x9re/HX19fRM6xquvvoqRkZG2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JYq2luItU22pcRJxTJPvepY\n7US0rq+oq04QqHUGFoM41O+ElVRT9aZdJA88RYrjF5VwbQHceLmQruXZJmXHF6Xlqo4x6aDE9a7G\nz0yhZQObRO2PQZzus9V
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x10e36a198>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"from scipy.stats import gaussian_kde\n",
|
|||
|
"\n",
|
|||
|
"# fit an array of size [Ndim, Nsamples]\n",
|
|||
|
"data = np.vstack([x, y])\n",
|
|||
|
"kde = gaussian_kde(data)\n",
|
|||
|
"\n",
|
|||
|
"# evaluate on a regular grid\n",
|
|||
|
"xgrid = np.linspace(-3.5, 3.5, 40)\n",
|
|||
|
"ygrid = np.linspace(-6, 6, 40)\n",
|
|||
|
"Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)\n",
|
|||
|
"Z = kde.evaluate(np.vstack([Xgrid.ravel(), Ygrid.ravel()]))\n",
|
|||
|
"\n",
|
|||
|
"# Plot the result as an image\n",
|
|||
|
"plt.imshow(Z.reshape(Xgrid.shape),\n",
|
|||
|
" origin='lower', aspect='auto',\n",
|
|||
|
" extent=[-3.5, 3.5, -6, 6],\n",
|
|||
|
" cmap='Blues')\n",
|
|||
|
"cb = plt.colorbar()\n",
|
|||
|
"cb.set_label(\"density\")"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"KDE has a smoothing length that effectively slides the knob between detail and smoothness (one example of the ubiquitous bias–variance trade-off).\n",
|
|||
|
"The literature on choosing an appropriate smoothing length is vast: ``gaussian_kde`` uses a rule-of-thumb to attempt to find a nearly optimal smoothing length for the input data.\n",
|
|||
|
"\n",
|
|||
|
"Other KDE implementations are available within the SciPy ecosystem, each with its own strengths and weaknesses; see, for example, ``sklearn.neighbors.KernelDensity`` and ``statsmodels.nonparametric.kernel_density.KDEMultivariate``.\n",
|
|||
|
"For visualizations based on KDE, using Matplotlib tends to be overly verbose.\n",
|
|||
|
"The Seaborn library, discussed in [Visualization With Seaborn](04.14-Visualization-With-Seaborn.ipynb), provides a much more terse API for creating KDE-based visualizations."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"<!--NAVIGATION-->\n",
|
|||
|
"< [Density and Contour Plots](04.04-Density-and-Contour-Plots.ipynb) | [Contents](Index.ipynb) | [Customizing Plot Legends](04.06-Customizing-Legends.ipynb) >"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"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.4.3"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 0
|
|||
|
}
|