Skip to content

Instantly share code, notes, and snippets.

Created June 13, 2017 17:07
Show Gist options
  • Select an option

  • Save anonymous/ff7525fb5feae7b8503e7a308445a894 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/ff7525fb5feae7b8503e7a308445a894 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div class=\"bk-root\">\n",
" <a href=\"http://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
" <span id=\"f9ccf774-94ff-4029-b43b-0d675f4e84cd\">Loading BokehJS ...</span>\n",
" </div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
"(function(global) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
"\n",
" var force = true;\n",
"\n",
" if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
" window._bokeh_onload_callbacks = [];\n",
" window._bokeh_is_loading = undefined;\n",
" }\n",
"\n",
"\n",
" \n",
" if (typeof (window._bokeh_timeout) === \"undefined\" || force === true) {\n",
" window._bokeh_timeout = Date.now() + 5000;\n",
" window._bokeh_failed_load = false;\n",
" }\n",
"\n",
" var NB_LOAD_WARNING = {'data': {'text/html':\n",
" \"<div style='background-color: #fdd'>\\n\"+\n",
" \"<p>\\n\"+\n",
" \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
" \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
" \"</p>\\n\"+\n",
" \"<ul>\\n\"+\n",
" \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
" \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
" \"</ul>\\n\"+\n",
" \"<code>\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"</code>\\n\"+\n",
" \"</div>\"}};\n",
"\n",
" function display_loaded() {\n",
" if (window.Bokeh !== undefined) {\n",
" var el = document.getElementById(\"f9ccf774-94ff-4029-b43b-0d675f4e84cd\");\n",
" el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
" } else if (Date.now() < window._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\n",
" }\n",
"\n",
" function run_callbacks() {\n",
" window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
" delete window._bokeh_onload_callbacks\n",
" console.info(\"Bokeh: all callbacks have finished\");\n",
" }\n",
"\n",
" function load_libs(js_urls, callback) {\n",
" window._bokeh_onload_callbacks.push(callback);\n",
" if (window._bokeh_is_loading > 0) {\n",
" console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
" return null;\n",
" }\n",
" if (js_urls == null || js_urls.length === 0) {\n",
" run_callbacks();\n",
" return null;\n",
" }\n",
" console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" window._bokeh_is_loading = js_urls.length;\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = false;\n",
" s.onreadystatechange = s.onload = function() {\n",
" window._bokeh_is_loading--;\n",
" if (window._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
" run_callbacks()\n",
" }\n",
" };\n",
" s.onerror = function() {\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" }\n",
" };var element = document.getElementById(\"f9ccf774-94ff-4029-b43b-0d675f4e84cd\");\n",
" if (element == null) {\n",
" console.log(\"Bokeh: ERROR: autoload.js configured with elementid 'f9ccf774-94ff-4029-b43b-0d675f4e84cd' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
"\n",
" var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.js\"];\n",
"\n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" Bokeh.set_log_level(\"info\");\n",
" },\n",
" \n",
" function(Bokeh) {\n",
" \n",
" },\n",
" \n",
" function(Bokeh) {\n",
" \n",
" document.getElementById(\"f9ccf774-94ff-4029-b43b-0d675f4e84cd\").textContent = \"BokehJS is loading...\";\n",
" },\n",
" function(Bokeh) {\n",
" console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css\");\n",
" Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css\");\n",
" console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css\");\n",
" Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css\");\n",
" }\n",
" ];\n",
"\n",
" function run_inline_js() {\n",
" \n",
" if ((window.Bokeh !== undefined) || (force === true)) {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i](window.Bokeh);\n",
" }if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < window._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!window._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" window._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"f9ccf774-94ff-4029-b43b-0d675f4e84cd\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\n",
"\n",
" }\n",
"\n",
" if (window._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(js_urls, function() {\n",
" console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
"}(this));"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from bokeh.io import output_notebook, show\n",
"from bokeh.charts import *\n",
"from bokeh.models.ranges import *\n",
"output_notebook()"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.read_csv('./database.csv', low_memory=False, na_values='Unknown')"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
" <div class=\"bk-root\">\n",
" <div class=\"bk-plotdiv\" id=\"583feea0-d1eb-4d64-99ad-8a41b36afd1d\"></div>\n",
" </div>\n",
"<script type=\"text/javascript\">\n",
" \n",
" (function(global) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
" \n",
" var force = false;\n",
" \n",
" if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
" window._bokeh_onload_callbacks = [];\n",
" window._bokeh_is_loading = undefined;\n",
" }\n",
" \n",
" \n",
" \n",
" if (typeof (window._bokeh_timeout) === \"undefined\" || force === true) {\n",
" window._bokeh_timeout = Date.now() + 0;\n",
" window._bokeh_failed_load = false;\n",
" }\n",
" \n",
" var NB_LOAD_WARNING = {'data': {'text/html':\n",
" \"<div style='background-color: #fdd'>\\n\"+\n",
" \"<p>\\n\"+\n",
" \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
" \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
" \"</p>\\n\"+\n",
" \"<ul>\\n\"+\n",
" \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
" \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
" \"</ul>\\n\"+\n",
" \"<code>\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"</code>\\n\"+\n",
" \"</div>\"}};\n",
" \n",
" function display_loaded() {\n",
" if (window.Bokeh !== undefined) {\n",
" var el = document.getElementById(\"583feea0-d1eb-4d64-99ad-8a41b36afd1d\");\n",
" el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
" } else if (Date.now() < window._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\n",
" }\n",
" \n",
" function run_callbacks() {\n",
" window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
" delete window._bokeh_onload_callbacks\n",
" console.info(\"Bokeh: all callbacks have finished\");\n",
" }\n",
" \n",
" function load_libs(js_urls, callback) {\n",
" window._bokeh_onload_callbacks.push(callback);\n",
" if (window._bokeh_is_loading > 0) {\n",
" console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
" return null;\n",
" }\n",
" if (js_urls == null || js_urls.length === 0) {\n",
" run_callbacks();\n",
" return null;\n",
" }\n",
" console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" window._bokeh_is_loading = js_urls.length;\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = false;\n",
" s.onreadystatechange = s.onload = function() {\n",
" window._bokeh_is_loading--;\n",
" if (window._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
" run_callbacks()\n",
" }\n",
" };\n",
" s.onerror = function() {\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" }\n",
" };var element = document.getElementById(\"583feea0-d1eb-4d64-99ad-8a41b36afd1d\");\n",
" if (element == null) {\n",
" console.log(\"Bokeh: ERROR: autoload.js configured with elementid '583feea0-d1eb-4d64-99ad-8a41b36afd1d' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
" \n",
" var js_urls = [];\n",
" \n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" (function() {\n",
" var fn = function() {\n",
" var docs_json = {\"efcee48c-f88d-44c0-9d41-1ffba6aa3196\":{\"roots\":{\"references\":[{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"26d0b928-7665-4516-be17-819e168c7780\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"1c8a047f-9628-40dd-9777-e53d98929a70\",\"type\":\"BasicTicker\"}},\"id\":\"00473497-da10-465e-b8b4-f142ed332fd4\",\"type\":\"Grid\"},{\"attributes\":{\"plot\":null,\"text\":\"Prob vs Year\"},\"id\":\"cd198e48-1eb6-4a31-9db8-a435683556d2\",\"type\":\"Title\"},{\"attributes\":{\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"a0025537-e1c0-42bf-b621-86994c9eea64\",\"type\":\"ResetTool\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"text\",\"y\"],\"data\":{\"text\":[\"-0.555\"],\"x\":[1997.0],\"y\":[0.7018515845666297]}},\"id\":\"f13415cb-b92e-48c5-9d05-b7af07b0ccaa\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":[1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014],\"y\":{\"__ndarray__\":\"mKHifPAU5z9cVHCLkAvnPyQH/pkwAuc/6LmLqND45j+sbBm3cO/mP3Afp8UQ5uY/ONI01LDc5j/8hMLiUNPmP8A3UPHwyeY/iOrd/5DA5j9MnWsOMbfmPxBQ+RzRreY/1AKHK3Gk5j+ctRQ6EZvmP2BookixkeY/JBswV1GI5j/ozb1l8X7mP7CAS3SRdeY/dDPZgjFs5j845maR0WLmP/yY9J9xWeY/xEuCrhFQ5j+I/g+9sUbmP0yxnctRPeY/EGQr2vEz5j/YFrnokSrmP5zJRvcxIeY/YHzUBdIX5j8kL2IUcg7mP+zh7yISBeY/sJR9MbL75T90RwtAUvLlPzz6mE7y6OU/AK0mXZLf5T/EX7RrMtblPw==\",\"dtype\":\"float64\",\"shape\":[35]}}},\"id\":\"fe07497d-cabc-4478-8478-8632182be8b0\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"d05815d0-5b47-4193-a595-d31927753cde\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"ae3692b5-d3e6-4bca-b6ca-54ff4ced15f1\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"7b78ef53-f0de-46d2-a39e-121e03097968\",\"type\":\"BasicTicker\"}},\"id\":\"b6a57010-658c-4d7f-b650-2b2606d1106e\",\"type\":\"Grid\"},{\"attributes\":{\"callback\":null,\"end\":0.7521331459634647,\"start\":0.6597681367632622},\"id\":\"ff52172f-48b1-40a3-9219-8072cf59442a\",\"type\":\"Range1d\"},{\"attributes\":{\"overlay\":{\"id\":\"26d0b928-7665-4516-be17-819e168c7780\",\"type\":\"BoxAnnotation\"},\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"c4b6e52a-84e3-4b1b-83c8-30d11f70ddc7\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"text_color\":{\"value\":\"black\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"664f2289-b453-49ad-bc12-5db08ffba3a7\",\"type\":\"Text\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"d323db97-6277-4662-8340-e649b9e453f4\",\"type\":\"PanTool\"},{\"id\":\"d05815d0-5b47-4193-a595-d31927753cde\",\"type\":\"WheelZoomTool\"},{\"id\":\"c4b6e52a-84e3-4b1b-83c8-30d11f70ddc7\",\"type\":\"BoxZoomTool\"},{\"id\":\"b277cb5a-daea-4773-99e7-2055fa753d82\",\"type\":\"SaveTool\"},{\"id\":\"a0025537-e1c0-42bf-b621-86994c9eea64\",\"type\":\"ResetTool\"},{\"id\":\"9a6ac34e-ba30-4009-91f4-026762d7a562\",\"type\":\"HelpTool\"}]},\"id\":\"cda73b5c-2d8f-4895-8c6c-f2e23dc3316c\",\"type\":\"Toolbar\"},{\"attributes\":{\"axis_label\":\"Year\",\"formatter\":{\"id\":\"5ab30bb5-c26d-49df-9acf-af9b7a60f11d\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"1c8a047f-9628-40dd-9777-e53d98929a70\",\"type\":\"BasicTicker\"}},\"id\":\"a61c720d-4b77-45b2-96a4-ca7948af78e2\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"7b78ef53-f0de-46d2-a39e-121e03097968\",\"type\":\"BasicTicker\"},{\"attributes\":{\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"9a6ac34e-ba30-4009-91f4-026762d7a562\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"1c8a047f-9628-40dd-9777-e53d98929a70\",\"type\":\"BasicTicker\"},{\"attributes\":{\"callback\":null,\"end\":2017.4,\"start\":1976.6},\"id\":\"3f5428b4-81b3-4df9-9907-ab18e7346349\",\"type\":\"Range1d\"},{\"attributes\":{\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"d323db97-6277-4662-8340-e649b9e453f4\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"5ab30bb5-c26d-49df-9acf-af9b7a60f11d\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"location\":\"top_left\",\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"de831926-4d2a-40be-9388-d641b811a759\",\"type\":\"Legend\"},{\"attributes\":{\"data_source\":{\"id\":\"fe07497d-cabc-4478-8478-8632182be8b0\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"4d18337e-3aaf-495a-8768-a4f087a181a2\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"8b47d0ff-0621-4227-b890-fb1702dd53b5\",\"type\":\"Line\"},\"selection_glyph\":null},\"id\":\"2aef0c53-8cd1-408f-9e04-133f8fab2ec6\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"data_source\":{\"id\":\"f13415cb-b92e-48c5-9d05-b7af07b0ccaa\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"664f2289-b453-49ad-bc12-5db08ffba3a7\",\"type\":\"Text\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"9619be89-e2d8-48b6-ac89-d6173026056a\",\"type\":\"Text\"},\"selection_glyph\":null},\"id\":\"f02f13e1-ffd7-4ff1-b460-44132619a0f0\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"line_color\":{\"value\":\"#1f77b4\"},\"line_width\":{\"value\":2},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4d18337e-3aaf-495a-8768-a4f087a181a2\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"32f82969-a22e-4c36-9e54-dc295ad6b5ad\",\"type\":\"ToolEvents\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"y_values\",\"x_values\"],\"data\":{\"chart_index\":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],\"x_values\":[1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014],\"y_values\":{\"__ndarray__\":\"vt4l0rEu5z9OVHWTa9LnP8dHJtA7dec/S57oJlGi5z8Tnkxd9mznP5Gqm5rQtec/2IrBhZGQ5z8nBToBGvTmPy2elwqV0eY/ZsBd003W5j9FtWIAEJjmP4UCoVaNo+U/C2jaBeBb5T8q8/tulpzlPx7L9g2K3OU/HIo6b0q25T/Rx7pwNxTmP4aTOe8Gz+U/eBEYIgl/5j/PB6qFhmHmP1TNJ7ljQ+Y/wf6W1Q1e5j8w9wMLOzLmP3FH3BF3xOU/hlzN0mH95T/MRrQssQ/mPzs7OZ5xF+Y/AsJRJm7N5T8d9HtDk5/lP1Og1q8UqOY/Gw1aqR2o5j8TWr7hk6XmP5GnEJ6uguY/dNLxn1tG5j8ZSPSvsjfmPw==\",\"dtype\":\"float64\",\"shape\":[35]}}},\"id\":\"b6a29819-f3fc-4d69-b09d-05c0bc6a4b90\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"data_source\":{\"id\":\"b6a29819-f3fc-4d69-b09d-05c0bc6a4b90\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"98ca2464-b7bb-450c-93d1-befb41d3f140\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null},\"id\":\"f6e34eab-1721-4d66-b8bb-23eb902d7dd6\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"b277cb5a-daea-4773-99e7-2055fa753d82\",\"type\":\"SaveTool\"},{\"attributes\":{\"text_alpha\":{\"value\":0.1},\"text_color\":{\"value\":\"black\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"9619be89-e2d8-48b6-ac89-d6173026056a\",\"type\":\"Text\"},{\"attributes\":{\"axis_label\":\"Prob\",\"formatter\":{\"id\":\"ae3692b5-d3e6-4bca-b6ca-54ff4ced15f1\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"7b78ef53-f0de-46d2-a39e-121e03097968\",\"type\":\"BasicTicker\"}},\"id\":\"f6088f9b-47ab-4aeb-af27-176713fc48a5\",\"type\":\"LinearAxis\"},{\"attributes\":{\"below\":[{\"id\":\"a61c720d-4b77-45b2-96a4-ca7948af78e2\",\"type\":\"LinearAxis\"}],\"css_classes\":null,\"left\":[{\"id\":\"f6088f9b-47ab-4aeb-af27-176713fc48a5\",\"type\":\"LinearAxis\"}],\"renderers\":[{\"id\":\"26d0b928-7665-4516-be17-819e168c7780\",\"type\":\"BoxAnnotation\"},{\"id\":\"f6e34eab-1721-4d66-b8bb-23eb902d7dd6\",\"type\":\"GlyphRenderer\"},{\"id\":\"de831926-4d2a-40be-9388-d641b811a759\",\"type\":\"Legend\"},{\"id\":\"a61c720d-4b77-45b2-96a4-ca7948af78e2\",\"type\":\"LinearAxis\"},{\"id\":\"f6088f9b-47ab-4aeb-af27-176713fc48a5\",\"type\":\"LinearAxis\"},{\"id\":\"00473497-da10-465e-b8b4-f142ed332fd4\",\"type\":\"Grid\"},{\"id\":\"b6a57010-658c-4d7f-b650-2b2606d1106e\",\"type\":\"Grid\"},{\"id\":\"2aef0c53-8cd1-408f-9e04-133f8fab2ec6\",\"type\":\"GlyphRenderer\"},{\"id\":\"f02f13e1-ffd7-4ff1-b460-44132619a0f0\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"cd198e48-1eb6-4a31-9db8-a435683556d2\",\"type\":\"Title\"},\"tool_events\":{\"id\":\"32f82969-a22e-4c36-9e54-dc295ad6b5ad\",\"type\":\"ToolEvents\"},\"toolbar\":{\"id\":\"cda73b5c-2d8f-4895-8c6c-f2e23dc3316c\",\"type\":\"Toolbar\"},\"x_mapper_type\":\"auto\",\"x_range\":{\"id\":\"3f5428b4-81b3-4df9-9907-ab18e7346349\",\"type\":\"Range1d\"},\"y_mapper_type\":\"auto\",\"y_range\":{\"id\":\"ff52172f-48b1-40a3-9219-8072cf59442a\",\"type\":\"Range1d\"}},\"id\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\",\"subtype\":\"Chart\",\"type\":\"Plot\"},{\"attributes\":{\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"line_width\":{\"value\":2},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"8b47d0ff-0621-4227-b890-fb1702dd53b5\",\"type\":\"Line\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.7},\"fill_color\":{\"value\":\"#f22c40\"},\"line_color\":{\"value\":\"#f22c40\"},\"size\":{\"units\":\"screen\",\"value\":8},\"x\":{\"field\":\"x_values\"},\"y\":{\"field\":\"y_values\"}},\"id\":\"98ca2464-b7bb-450c-93d1-befb41d3f140\",\"type\":\"Circle\"}],\"root_ids\":[\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.5\"}};\n",
" var render_items = [{\"docid\":\"efcee48c-f88d-44c0-9d41-1ffba6aa3196\",\"elementid\":\"583feea0-d1eb-4d64-99ad-8a41b36afd1d\",\"modelid\":\"9a921ea8-53ac-4fa7-8103-d60a1a85e04b\"}];\n",
" \n",
" Bokeh.embed.embed_items(docs_json, render_items);\n",
" };\n",
" if (document.readyState != \"loading\") fn();\n",
" else document.addEventListener(\"DOMContentLoaded\", fn);\n",
" })();\n",
" },\n",
" function(Bokeh) {\n",
" }\n",
" ];\n",
" \n",
" function run_inline_js() {\n",
" \n",
" if ((window.Bokeh !== undefined) || (force === true)) {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i](window.Bokeh);\n",
" }if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < window._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!window._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" window._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"583feea0-d1eb-4d64-99ad-8a41b36afd1d\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\n",
" \n",
" }\n",
" \n",
" if (window._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(js_urls, function() {\n",
" console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
" }(this));\n",
"</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"is_solved = df['Crime Solved'] == 'Yes'\n",
"cnt = df[is_solved]['Year'].value_counts(sort=False)\n",
"ttl = df['Year'].value_counts(sort=False)\n",
"data = cnt / ttl\n",
"\n",
"s = pd.DataFrame()\n",
"s['Prob'] = data\n",
"s['Year'] = data.index\n",
"\n",
"reg = np.poly1d(np.polyfit(s['Year'], s['Prob'], deg=1))\n",
"s['Pred'] = reg(s['Year'])\n",
"\n",
"r = '{:.3f}'.format(s['Year'].corr(s['Prob']))\n",
"xbar = s['Year'].mean()\n",
"ybar = s['Prob'].mean()\n",
"\n",
"# p = Line(s, x='Year', y='Prob')\n",
"p = Scatter(s, x='Year', y='Prob', title='Prob vs Year')\n",
"p.line(x=s['Year'], y=s['Pred'], line_width=2)\n",
"p.text(x=[xbar], y=[ybar], text=[r])\n",
"\n",
"show(p)"
]
},
{
"cell_type": "code",
"execution_count": 202,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-34.068733678129384"
]
},
"execution_count": 202,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from math import sqrt\n",
"\n",
"is_male = df['Victim Sex'] == 'Male'\n",
"is_female = df['Victim Sex'] == 'Female'\n",
"\n",
"dfm = df[is_male]['Victim Age']\n",
"dff = df[is_female]['Victim Age']\n",
"\n",
"bar_m, std_m, n_m = dfm.mean(), dfm.std(), dfm.size\n",
"bar_f, std_f, n_f = dff.mean(), dff.std(), dff.size\n",
"\n",
"nom = (bar_m - bar_f) - 0\n",
"dem = sqrt(std_m**2 / n_m + std_f**2 / n_f)\n",
"z = nom / dem\n",
"\n",
"z"
]
},
{
"cell_type": "code",
"execution_count": 201,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"317422 0.7409253296872933\n",
"299899 0.6628031437250541\n"
]
},
{
"data": {
"text/plain": [
"67.13633023616113"
]
},
"execution_count": 201,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_white = df['Victim Race'] == 'White'\n",
"is_black = df['Victim Race'] == 'Black'\n",
"is_solved = df['Crime Solved'] == 'Yes'\n",
"\n",
"n1 = len(df[is_white])\n",
"X1 = len(df[is_white & is_solved])\n",
"n2 = len(df[is_black])\n",
"X2 = len(df[is_black & is_solved])\n",
"\n",
"hat_p1 = X1 / n1\n",
"hat_p2 = X2 / n2\n",
"bar_p = (X1 + X2) / (n1 + n2)\n",
"bar_q = 1 - bar_p\n",
"\n",
"print(n1, hat_p1)\n",
"print(n2, hat_p2)\n",
"\n",
"nom = (hat_p1 - hat_p2) - 0\n",
"dem = sqrt(bar_p * bar_q * (1 / n1 + 1 / n2))\n",
"z = nom / dem\n",
"\n",
"z"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment