Skip to content

Instantly share code, notes, and snippets.

@Shinichi-Nakagawa
Created November 17, 2019 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shinichi-Nakagawa/4c7a3673256647edb3a112691287de73 to your computer and use it in GitHub Desktop.
Save Shinichi-Nakagawa/4c7a3673256647edb3a112691287de73 to your computer and use it in GitHub Desktop.
ミッキー・マントル(MLB)のOPS推移とパフォーマンス軌跡 - Python版
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"import pandas as pd\n",
"pd.options.display.max_columns = 100"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"players = {}\n",
"with open(\"/Users/hoge/github/baseballdatabank/core/People.csv\", \"r\") as f:\n",
" reader = csv.DictReader(f)\n",
" players = {r['playerID'] : r for r in reader}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"df_batting = pd.read_csv(\"/Users/hoge/github/baseballdatabank/core/Batting.csv\")\n",
"df_batting.fillna(0.0, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def player_name(playerID: str) -> str :\n",
" if playerID in players:\n",
" return str(f\"{players[playerID]['nameFirst']} {players[playerID]['nameLast']}\")\n",
" return 'Unknown'\n",
"\n",
"def age(row: pd.Series) -> int :\n",
" if row.playerID in players:\n",
" player = players[row.playerID]\n",
" birthYear = player['birthYear']\n",
" if birthYear:\n",
" birthYear = int(birthYear)\n",
" else:\n",
" return None\n",
" return row.yearID - birthYear - 1\n",
" return None\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"df_batting['playerName'] = df_batting['playerID'].map(player_name)\n",
"df_batting['age'] = df_batting.apply(age, axis=1)\n",
"df_batting['SINGLE'] = df_batting['H'] - (df_batting['HR'] + df_batting['3B'] + df_batting['2B'])\n",
"df_batting['TB'] = df_batting['HR'] * 4 + df_batting['3B'] * 3 + df_batting['2B'] * 2 + df_batting['SINGLE'] * 1"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# 打率, 出塁率, 長打率. 指標値計算にSABRを使う\n",
"\n",
"import sabr\n",
"\n",
"from sabr.stats import Stats\n",
"def ba(row: pd.Series) -> float:\n",
" try:\n",
" return Stats.avg(ab=row.AB, h=row.H)\n",
" except ZeroDivisionError as e:\n",
" return 0.0\n",
"\n",
"def obp(row: pd.Series) -> float:\n",
" try:\n",
" return Stats.obp(ab=row.AB, h=row.H, bb=row.BB, sf=row.SF, hbp=row.HBP)\n",
" except ZeroDivisionError as e:\n",
" return 0.0\n",
"\n",
"def slg(row: pd.Series) -> float:\n",
" try:\n",
" return Stats.slg(ab=row.AB, tb=row.TB)\n",
" except ZeroDivisionError as e:\n",
" return 0.0\n",
"\n",
"df_batting['BA'] = df_batting.apply(ba, axis=1)\n",
"df_batting['OBP'] = df_batting.apply(obp, axis=1)\n",
"df_batting['SLG'] = df_batting.apply(slg, axis=1)\n",
"df_batting['OPS'] = df_batting['OBP'] + df_batting['SLG']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# check\n",
"df_mantle = df_batting.query(\"playerName == 'Mickey Mantle'\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>age</th>\n",
" <th>SLG</th>\n",
" <th>OBP</th>\n",
" <th>OPS</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>35380</th>\n",
" <td>19.0</td>\n",
" <td>0.443</td>\n",
" <td>0.349</td>\n",
" <td>0.792</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36008</th>\n",
" <td>20.0</td>\n",
" <td>0.530</td>\n",
" <td>0.394</td>\n",
" <td>0.924</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36620</th>\n",
" <td>21.0</td>\n",
" <td>0.497</td>\n",
" <td>0.398</td>\n",
" <td>0.895</td>\n",
" </tr>\n",
" <tr>\n",
" <th>37199</th>\n",
" <td>22.0</td>\n",
" <td>0.525</td>\n",
" <td>0.408</td>\n",
" <td>0.933</td>\n",
" </tr>\n",
" <tr>\n",
" <th>37823</th>\n",
" <td>23.0</td>\n",
" <td>0.611</td>\n",
" <td>0.431</td>\n",
" <td>1.042</td>\n",
" </tr>\n",
" <tr>\n",
" <th>38467</th>\n",
" <td>24.0</td>\n",
" <td>0.705</td>\n",
" <td>0.464</td>\n",
" <td>1.169</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39074</th>\n",
" <td>25.0</td>\n",
" <td>0.665</td>\n",
" <td>0.512</td>\n",
" <td>1.177</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39706</th>\n",
" <td>26.0</td>\n",
" <td>0.592</td>\n",
" <td>0.443</td>\n",
" <td>1.035</td>\n",
" </tr>\n",
" <tr>\n",
" <th>40347</th>\n",
" <td>27.0</td>\n",
" <td>0.514</td>\n",
" <td>0.390</td>\n",
" <td>0.904</td>\n",
" </tr>\n",
" <tr>\n",
" <th>40992</th>\n",
" <td>28.0</td>\n",
" <td>0.558</td>\n",
" <td>0.399</td>\n",
" <td>0.957</td>\n",
" </tr>\n",
" <tr>\n",
" <th>41674</th>\n",
" <td>29.0</td>\n",
" <td>0.687</td>\n",
" <td>0.448</td>\n",
" <td>1.135</td>\n",
" </tr>\n",
" <tr>\n",
" <th>42389</th>\n",
" <td>30.0</td>\n",
" <td>0.605</td>\n",
" <td>0.486</td>\n",
" <td>1.091</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43145</th>\n",
" <td>31.0</td>\n",
" <td>0.622</td>\n",
" <td>0.441</td>\n",
" <td>1.063</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43883</th>\n",
" <td>32.0</td>\n",
" <td>0.591</td>\n",
" <td>0.423</td>\n",
" <td>1.014</td>\n",
" </tr>\n",
" <tr>\n",
" <th>44643</th>\n",
" <td>33.0</td>\n",
" <td>0.452</td>\n",
" <td>0.379</td>\n",
" <td>0.831</td>\n",
" </tr>\n",
" <tr>\n",
" <th>45412</th>\n",
" <td>34.0</td>\n",
" <td>0.538</td>\n",
" <td>0.389</td>\n",
" <td>0.927</td>\n",
" </tr>\n",
" <tr>\n",
" <th>46188</th>\n",
" <td>35.0</td>\n",
" <td>0.434</td>\n",
" <td>0.391</td>\n",
" <td>0.825</td>\n",
" </tr>\n",
" <tr>\n",
" <th>46924</th>\n",
" <td>36.0</td>\n",
" <td>0.398</td>\n",
" <td>0.385</td>\n",
" <td>0.783</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" age SLG OBP OPS\n",
"35380 19.0 0.443 0.349 0.792\n",
"36008 20.0 0.530 0.394 0.924\n",
"36620 21.0 0.497 0.398 0.895\n",
"37199 22.0 0.525 0.408 0.933\n",
"37823 23.0 0.611 0.431 1.042\n",
"38467 24.0 0.705 0.464 1.169\n",
"39074 25.0 0.665 0.512 1.177\n",
"39706 26.0 0.592 0.443 1.035\n",
"40347 27.0 0.514 0.390 0.904\n",
"40992 28.0 0.558 0.399 0.957\n",
"41674 29.0 0.687 0.448 1.135\n",
"42389 30.0 0.605 0.486 1.091\n",
"43145 31.0 0.622 0.441 1.063\n",
"43883 32.0 0.591 0.423 1.014\n",
"44643 33.0 0.452 0.379 0.831\n",
"45412 34.0 0.538 0.389 0.927\n",
"46188 35.0 0.434 0.391 0.825\n",
"46924 36.0 0.398 0.385 0.783"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_mantle[['age', 'SLG', 'OBP', 'OPS']]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div class=\"bk-root\">\n",
" <a href=\"https://bokeh.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
" <span id=\"1001\">Loading BokehJS ...</span>\n",
" </div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
"(function(root) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
"\n",
" var force = true;\n",
"\n",
" if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n",
" root._bokeh_onload_callbacks = [];\n",
" root._bokeh_is_loading = undefined;\n",
" }\n",
"\n",
" var JS_MIME_TYPE = 'application/javascript';\n",
" var HTML_MIME_TYPE = 'text/html';\n",
" var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n",
" var CLASS_NAME = 'output_bokeh rendered_html';\n",
"\n",
" /**\n",
" * Render data to the DOM node\n",
" */\n",
" function render(props, node) {\n",
" var script = document.createElement(\"script\");\n",
" node.appendChild(script);\n",
" }\n",
"\n",
" /**\n",
" * Handle when an output is cleared or removed\n",
" */\n",
" function handleClearOutput(event, handle) {\n",
" var cell = handle.cell;\n",
"\n",
" var id = cell.output_area._bokeh_element_id;\n",
" var server_id = cell.output_area._bokeh_server_id;\n",
" // Clean up Bokeh references\n",
" if (id != null && id in Bokeh.index) {\n",
" Bokeh.index[id].model.document.clear();\n",
" delete Bokeh.index[id];\n",
" }\n",
"\n",
" if (server_id !== undefined) {\n",
" // Clean up Bokeh references\n",
" var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n",
" cell.notebook.kernel.execute(cmd, {\n",
" iopub: {\n",
" output: function(msg) {\n",
" var id = msg.content.text.trim();\n",
" if (id in Bokeh.index) {\n",
" Bokeh.index[id].model.document.clear();\n",
" delete Bokeh.index[id];\n",
" }\n",
" }\n",
" }\n",
" });\n",
" // Destroy server and session\n",
" var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n",
" cell.notebook.kernel.execute(cmd);\n",
" }\n",
" }\n",
"\n",
" /**\n",
" * Handle when a new output is added\n",
" */\n",
" function handleAddOutput(event, handle) {\n",
" var output_area = handle.output_area;\n",
" var output = handle.output;\n",
"\n",
" // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n",
" if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n",
" return\n",
" }\n",
"\n",
" var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n",
"\n",
" if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n",
" toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n",
" // store reference to embed id on output_area\n",
" output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n",
" }\n",
" if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n",
" var bk_div = document.createElement(\"div\");\n",
" bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n",
" var script_attrs = bk_div.children[0].attributes;\n",
" for (var i = 0; i < script_attrs.length; i++) {\n",
" toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n",
" }\n",
" // store reference to server id on output_area\n",
" output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n",
" }\n",
" }\n",
"\n",
" function register_renderer(events, OutputArea) {\n",
"\n",
" function append_mime(data, metadata, element) {\n",
" // create a DOM node to render to\n",
" var toinsert = this.create_output_subarea(\n",
" metadata,\n",
" CLASS_NAME,\n",
" EXEC_MIME_TYPE\n",
" );\n",
" this.keyboard_manager.register_events(toinsert);\n",
" // Render to node\n",
" var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n",
" render(props, toinsert[toinsert.length - 1]);\n",
" element.append(toinsert);\n",
" return toinsert\n",
" }\n",
"\n",
" /* Handle when an output is cleared or removed */\n",
" events.on('clear_output.CodeCell', handleClearOutput);\n",
" events.on('delete.Cell', handleClearOutput);\n",
"\n",
" /* Handle when a new output is added */\n",
" events.on('output_added.OutputArea', handleAddOutput);\n",
"\n",
" /**\n",
" * Register the mime type and append_mime function with output_area\n",
" */\n",
" OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n",
" /* Is output safe? */\n",
" safe: true,\n",
" /* Index of renderer in `output_area.display_order` */\n",
" index: 0\n",
" });\n",
" }\n",
"\n",
" // register the mime type if in Jupyter Notebook environment and previously unregistered\n",
" if (root.Jupyter !== undefined) {\n",
" var events = require('base/js/events');\n",
" var OutputArea = require('notebook/js/outputarea').OutputArea;\n",
"\n",
" if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n",
" register_renderer(events, OutputArea);\n",
" }\n",
" }\n",
"\n",
" \n",
" if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
" root._bokeh_timeout = Date.now() + 5000;\n",
" root._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",
" var el = document.getElementById(\"1001\");\n",
" if (el != null) {\n",
" el.textContent = \"BokehJS is loading...\";\n",
" }\n",
" if (root.Bokeh !== undefined) {\n",
" if (el != null) {\n",
" el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n",
" }\n",
" } else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\n",
" }\n",
"\n",
"\n",
" function run_callbacks() {\n",
" try {\n",
" root._bokeh_onload_callbacks.forEach(function(callback) {\n",
" if (callback != null)\n",
" callback();\n",
" });\n",
" } finally {\n",
" delete root._bokeh_onload_callbacks\n",
" }\n",
" console.debug(\"Bokeh: all callbacks have finished\");\n",
" }\n",
"\n",
" function load_libs(css_urls, js_urls, callback) {\n",
" if (css_urls == null) css_urls = [];\n",
" if (js_urls == null) js_urls = [];\n",
"\n",
" root._bokeh_onload_callbacks.push(callback);\n",
" if (root._bokeh_is_loading > 0) {\n",
" console.debug(\"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.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" root._bokeh_is_loading = css_urls.length + js_urls.length;\n",
"\n",
" function on_load() {\n",
" root._bokeh_is_loading--;\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n",
" run_callbacks()\n",
" }\n",
" }\n",
"\n",
" function on_error() {\n",
" console.error(\"failed to load \" + url);\n",
" }\n",
"\n",
" for (var i = 0; i < css_urls.length; i++) {\n",
" var url = css_urls[i];\n",
" const element = document.createElement(\"link\");\n",
" element.onload = on_load;\n",
" element.onerror = on_error;\n",
" element.rel = \"stylesheet\";\n",
" element.type = \"text/css\";\n",
" element.href = url;\n",
" console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n",
" document.body.appendChild(element);\n",
" }\n",
"\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var element = document.createElement('script');\n",
" element.onload = on_load;\n",
" element.onerror = on_error;\n",
" element.async = false;\n",
" element.src = url;\n",
" console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.head.appendChild(element);\n",
" }\n",
" };var element = document.getElementById(\"1001\");\n",
" if (element == null) {\n",
" console.error(\"Bokeh: ERROR: autoload.js configured with elementid '1001' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
"\n",
" function inject_raw_css(css) {\n",
" const element = document.createElement(\"style\");\n",
" element.appendChild(document.createTextNode(css));\n",
" document.body.appendChild(element);\n",
" }\n",
"\n",
" \n",
" var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.4.0.min.js\"];\n",
" var css_urls = [];\n",
" \n",
"\n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" Bokeh.set_log_level(\"info\");\n",
" },\n",
" function(Bokeh) {\n",
" \n",
" \n",
" }\n",
" ];\n",
"\n",
" function run_inline_js() {\n",
" \n",
" if (root.Bokeh !== undefined || force === true) {\n",
" \n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i].call(root, root.Bokeh);\n",
" }\n",
" if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!root._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" root._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\n",
"\n",
" }\n",
"\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(css_urls, js_urls, function() {\n",
" console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
"}(window));"
],
"application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n\n if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._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 var el = document.getElementById(\"1001\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) {\n if (callback != null)\n callback();\n });\n } finally {\n delete root._bokeh_onload_callbacks\n }\n console.debug(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(css_urls, js_urls, callback) {\n if (css_urls == null) css_urls = [];\n if (js_urls == null) js_urls = [];\n\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.debug(\"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.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n function on_load() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n run_callbacks()\n }\n }\n\n function on_error() {\n console.error(\"failed to load \" + url);\n }\n\n for (var i = 0; i < css_urls.length; i++) {\n var url = css_urls[i];\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error;\n element.rel = \"stylesheet\";\n element.type = \"text/css\";\n element.href = url;\n console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n document.body.appendChild(element);\n }\n\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n var element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error;\n element.async = false;\n element.src = url;\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n };var element = document.getElementById(\"1001\");\n if (element == null) {\n console.error(\"Bokeh: ERROR: autoload.js configured with elementid '1001' but no matching script tag was found. \")\n return false;\n }\n\n function inject_raw_css(css) {\n const element = document.createElement(\"style\");\n element.appendChild(document.createTextNode(css));\n document.body.appendChild(element);\n }\n\n \n var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.4.0.min.js\"];\n var css_urls = [];\n \n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n function(Bokeh) {\n \n \n }\n ];\n\n function run_inline_js() {\n \n if (root.Bokeh !== undefined || force === true) {\n \n for (var i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }\n if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(css_urls, js_urls, function() {\n console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
" <div class=\"bk-root\" id=\"4a29400c-82b6-444b-ab46-eff388866e44\" data-root-id=\"1002\"></div>\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"(function(root) {\n",
" function embed_document(root) {\n",
" \n",
" var docs_json = {\"e9d94de0-d496-4269-8a0d-dce12cb64cfe\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1013\",\"type\":\"LinearAxis\"}],\"center\":[{\"id\":\"1017\",\"type\":\"Grid\"},{\"id\":\"1022\",\"type\":\"Grid\"}],\"left\":[{\"id\":\"1018\",\"type\":\"LinearAxis\"}],\"plot_height\":840,\"plot_width\":840,\"renderers\":[{\"id\":\"1040\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"1003\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1029\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1005\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"1009\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1007\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"1011\",\"type\":\"LinearScale\"}},\"id\":\"1002\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1023\",\"type\":\"PanTool\"},{\"id\":\"1024\",\"type\":\"WheelZoomTool\"},{\"id\":\"1025\",\"type\":\"BoxZoomTool\"},{\"id\":\"1026\",\"type\":\"SaveTool\"},{\"id\":\"1027\",\"type\":\"ResetTool\"},{\"id\":\"1028\",\"type\":\"HelpTool\"}]},\"id\":\"1029\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1023\",\"type\":\"PanTool\"},{\"attributes\":{\"callback\":null},\"id\":\"1007\",\"type\":\"DataRange1d\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"age\"},\"y\":{\"field\":\"OPS\"}},\"id\":\"1039\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1046\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1024\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1047\",\"type\":\"Selection\"},{\"attributes\":{\"overlay\":{\"id\":\"1049\",\"type\":\"BoxAnnotation\"}},\"id\":\"1025\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"callback\":null},\"id\":\"1005\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1011\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1026\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1027\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1044\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1028\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"1009\",\"type\":\"LinearScale\"},{\"attributes\":{\"axis_label\":\"AGE\",\"formatter\":{\"id\":\"1044\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1014\",\"type\":\"BasicTicker\"}},\"id\":\"1013\",\"type\":\"LinearAxis\"},{\"attributes\":{\"data_source\":{\"id\":\"1036\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1038\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1039\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"1041\",\"type\":\"CDSView\"}},\"id\":\"1040\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"callback\":null,\"data\":{\"2B\":[11,37,24,17,25,22,28,21,23,17,16,15,8,25,12,12,17,14],\"3B\":[5,7,3,12,11,5,6,1,4,6,6,1,0,2,1,1,0,1],\"AB\":[341,549,461,543,517,533,474,519,541,527,514,377,172,465,361,333,440,435],\"BA\":{\"__ndarray__\":\"SgwCK4cW0T+BlUOLbOfTP+F6FK5H4dI/MzMzMzMz0z8v3SQGgZXTP2Q730+Nl9Y/XI/C9Shc1z/b+X5qvHTTPz0K16NwPdI/mpmZmZmZ0T99PzVeuknUPyUGgZVDi9Q/f2q8dJMY1D8xCKwcWmTTP1K4HoXrUdA/O99PjZdu0j9cj8L1KFzPP7x0kxgEVs4/\",\"dtype\":\"float64\",\"shape\":[18]},\"BB\":[43,75,79,102,113,112,146,129,93,111,126,122,40,99,73,57,107,106],\"CS\":{\"__ndarray__\":\"AAAAAAAAHEAAAAAAAADwPwAAAAAAABBAAAAAAAAAAEAAAAAAAADwPwAAAAAAAPA/AAAAAAAACEAAAAAAAAAIQAAAAAAAAAhAAAAAAAAACEAAAAAAAADwPwAAAAAAAAAAAAAAAAAA8D8AAAAAAAAIQAAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAABA\",\"dtype\":\"float64\",\"shape\":[18]},\"G\":[96,142,127,146,147,150,144,150,144,153,153,123,65,143,122,108,144,144],\"GIDP\":{\"__ndarray__\":\"AAAAAAAACEAAAAAAAAAUQAAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABBAAAAAAAAAFEAAAAAAAAAmQAAAAAAAABxAAAAAAAAAJkAAAAAAAAAAQAAAAAAAABBAAAAAAAAAFEAAAAAAAAAiQAAAAAAAACZAAAAAAAAAIkAAAAAAAAAiQAAAAAAAACJA\",\"dtype\":\"float64\",\"shape\":[18]},\"H\":[91,171,136,163,158,188,173,158,154,145,163,121,54,141,92,96,108,103],\"HBP\":{\"__ndarray__\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIQAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAABAAAAAAAAA8D8AAAAAAAAAAAAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwPwAAAAAAAPA/\",\"dtype\":\"float64\",\"shape\":[18]},\"HR\":[13,23,21,27,37,52,34,42,31,40,54,30,15,35,19,23,22,18],\"IBB\":{\"__ndarray__\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYQAAAAAAAABhAAAAAAAAAN0AAAAAAAAAqQAAAAAAAABhAAAAAAAAAGEAAAAAAAAAiQAAAAAAAACJAAAAAAAAAEEAAAAAAAAAyQAAAAAAAABxAAAAAAAAAFEAAAAAAAAAcQAAAAAAAABxA\",\"dtype\":\"float64\",\"shape\":[18]},\"OBP\":{\"__ndarray__\":\"vHSTGARW1j+e76fGSzfZP0a28/3UeNk/6SYxCKwc2j8v3SQGgZXbPxkEVg4tst0//Knx0k1i4D8nMQisHFrcP/YoXI/C9dg/8KfGSzeJ2T956SYxCKzcP7TIdr6fGt8/001iEFg53D/fT42XbhLbP6jGSzeJQdg/TDeJQWDl2D+gGi/dJAbZP6RwPQrXo9g/\",\"dtype\":\"float64\",\"shape\":[18]},\"OPS\":{\"__ndarray__\":\"8tJNYhBY6T/FILByaJHtP6RwPQrXo+w/QmDl0CLb7T956SYxCKzwP05iEFg5tPI/okW28/3U8j+PwvUoXI/wPyGwcmiR7ew/OrTIdr6f7j8pXI/C9SjyP9v5fmq8dPE/NV66SQwC8T/TTWIQWDnwP2Q730+Nl+o/RIts5/up7T9mZmZmZmbqP3WTGARWDuk/\",\"dtype\":\"float64\",\"shape\":[18]},\"R\":[61,94,105,129,121,132,121,127,104,119,132,96,40,92,44,40,63,57],\"RBI\":{\"__ndarray__\":\"AAAAAABAUEAAAAAAAMBVQAAAAAAAAFdAAAAAAACAWUAAAAAAAMBYQAAAAAAAQGBAAAAAAACAV0AAAAAAAEBYQAAAAAAAwFJAAAAAAACAV0AAAAAAAABgQAAAAAAAQFZAAAAAAACAQUAAAAAAAMBbQAAAAAAAAEdAAAAAAAAATEAAAAAAAIBLQAAAAAAAAEtA\",\"dtype\":\"float64\",\"shape\":[18]},\"SB\":{\"__ndarray__\":\"AAAAAAAAIEAAAAAAAAAQQAAAAAAAACBAAAAAAAAAFEAAAAAAAAAgQAAAAAAAACRAAAAAAAAAMEAAAAAAAAAyQAAAAAAAADVAAAAAAAAALEAAAAAAAAAoQAAAAAAAACJAAAAAAAAAAEAAAAAAAAAYQAAAAAAAABBAAAAAAAAA8D8AAAAAAADwPwAAAAAAABhA\",\"dtype\":\"float64\",\"shape\":[18]},\"SF\":{\"__ndarray__\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEEAAAAAAAAAIQAAAAAAAABBAAAAAAAAACEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAFEAAAAAAAAAUQAAAAAAAAABAAAAAAAAA8D8AAAAAAAAIQAAAAAAAAPA/AAAAAAAACEAAAAAAAAAUQAAAAAAAABBA\",\"dtype\":\"float64\",\"shape\":[18]},\"SH\":{\"__ndarray__\":\"AAAAAAAAAEAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAQAAAAAAAAPA/AAAAAAAAAAAAAAAAAADwPwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA/\",\"dtype\":\"float64\",\"shape\":[18]},\"SINGLE\":[62,104,88,107,85,109,105,94,96,82,87,75,31,79,60,60,69,70],\"SLG\":{\"__ndarray__\":\"JzEIrBxa3D/2KFyPwvXgPwIrhxbZzt8/zczMzMzM4D9aZDvfT43jP4/C9Shcj+Y/SOF6FK5H5T+LbOf7qfHiP6abxCCwcuA/QmDl0CLb4T+WQ4ts5/vlP1yPwvUoXOM/gZVDi2zn4z+28/3UeOniPyGwcmiR7dw/nu+nxks34T8tsp3vp8bbP0a28/3UeNk/\",\"dtype\":\"float64\",\"shape\":[18]},\"SO\":{\"__ndarray__\":\"AAAAAACAUkAAAAAAAMBbQAAAAAAAgFZAAAAAAADAWkAAAAAAAEBYQAAAAAAAwFhAAAAAAADAUkAAAAAAAABeQAAAAAAAgF9AAAAAAABAX0AAAAAAAABcQAAAAAAAgFNAAAAAAAAAQEAAAAAAAIBZQAAAAAAAAFNAAAAAAAAAU0AAAAAAAEBcQAAAAAAAQFhA\",\"dtype\":\"float64\",\"shape\":[18]},\"TB\":[151,291,229,285,316,376,315,307,278,294,353,228,107,275,163,179,191,173],\"age\":{\"__ndarray__\":\"AAAAAAAAM0AAAAAAAAA0QAAAAAAAADVAAAAAAAAANkAAAAAAAAA3QAAAAAAAADhAAAAAAAAAOUAAAAAAAAA6QAAAAAAAADtAAAAAAAAAPEAAAAAAAAA9QAAAAAAAAD5AAAAAAAAAP0AAAAAAAABAQAAAAAAAgEBAAAAAAAAAQUAAAAAAAIBBQAAAAAAAAEJA\",\"dtype\":\"float64\",\"shape\":[18]},\"index\":[35380,36008,36620,37199,37823,38467,39074,39706,40347,40992,41674,42389,43145,43883,44643,45412,46188,46924],\"lgID\":[\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\"],\"playerID\":[\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\"],\"playerName\":[\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\"],\"stint\":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],\"teamID\":[\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\"],\"yearID\":[1951,1952,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,1967,1968]},\"selected\":{\"id\":\"1047\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1048\",\"type\":\"UnionRenderers\"}},\"id\":\"1036\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1014\",\"type\":\"BasicTicker\"},{\"attributes\":{\"text\":\"Mickey Mantle OPS\"},\"id\":\"1003\",\"type\":\"Title\"},{\"attributes\":{\"fill_color\":{\"value\":\"black\"},\"x\":{\"field\":\"age\"},\"y\":{\"field\":\"OPS\"}},\"id\":\"1038\",\"type\":\"Circle\"},{\"attributes\":{\"source\":{\"id\":\"1036\",\"type\":\"ColumnDataSource\"}},\"id\":\"1041\",\"type\":\"CDSView\"},{\"attributes\":{\"ticker\":{\"id\":\"1014\",\"type\":\"BasicTicker\"}},\"id\":\"1017\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1019\",\"type\":\"BasicTicker\"},{\"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},\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1049\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"dimension\":1,\"ticker\":{\"id\":\"1019\",\"type\":\"BasicTicker\"}},\"id\":\"1022\",\"type\":\"Grid\"},{\"attributes\":{\"axis_label\":\"OPS\",\"formatter\":{\"id\":\"1046\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1019\",\"type\":\"BasicTicker\"}},\"id\":\"1018\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1048\",\"type\":\"UnionRenderers\"}],\"root_ids\":[\"1002\"]},\"title\":\"Bokeh Application\",\"version\":\"1.4.0\"}};\n",
" var render_items = [{\"docid\":\"e9d94de0-d496-4269-8a0d-dce12cb64cfe\",\"roots\":{\"1002\":\"4a29400c-82b6-444b-ab46-eff388866e44\"}}];\n",
" root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
"\n",
" }\n",
" if (root.Bokeh !== undefined) {\n",
" embed_document(root);\n",
" } else {\n",
" var attempts = 0;\n",
" var timer = setInterval(function(root) {\n",
" if (root.Bokeh !== undefined) {\n",
" clearInterval(timer);\n",
" embed_document(root);\n",
" } else {\n",
" attempts++;\n",
" if (attempts > 100) {\n",
" clearInterval(timer);\n",
" console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
" }\n",
" }\n",
" }, 10, root)\n",
" }\n",
"})(window);"
],
"application/vnd.bokehjs_exec.v0+json": ""
},
"metadata": {
"application/vnd.bokehjs_exec.v0+json": {
"id": "1002"
}
},
"output_type": "display_data"
}
],
"source": [
"# OPSを可視化\n",
"from bokeh.plotting import figure, show, output_notebook\n",
"\n",
"output_notebook()\n",
"\n",
"p = figure(\n",
" plot_width=840, \n",
" plot_height=840, \n",
" title=\"Mickey Mantle OPS\",\n",
" x_axis_label='AGE',\n",
" y_axis_label='OPS',\n",
")\n",
"p.circle(x='age', y='OPS', size=4, source=df_mantle, fill_color='black', line_color='black')\n",
"\n",
"show(p)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# こっから線形回帰させる\n",
"from sklearn.linear_model import LinearRegression"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# 目的変数はOPS\n",
"Y = df_mantle['OPS'].values"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# 説明変数は年齢を元にアレンジ(この辺は書籍を参照)\n",
"_df_x = pd.DataFrame()\n",
"_df_x['I(Age-30)'] = df_mantle['age'] - 30\n",
"_df_x['I((Age-30)^2)'] = (df_mantle['age'] - 30) ** 2\n",
"X = _df_x[['I(Age-30)', 'I((Age-30)^2)']].values"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ml = LinearRegression()\n",
"ml.fit(X, Y)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0431176470588235"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ml.intercept_"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(27.044058744993325, 1.0768867592290237)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"age_max = 30 - (ml.coef_[0] / ml.coef_[1] / 2)\n",
"_max = ml.intercept_ - ml.coef_[0] ** 2 / ml.coef_[1] / 4\n",
"age_max, _max"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div class=\"bk-root\">\n",
" <a href=\"https://bokeh.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
" <span id=\"1098\">Loading BokehJS ...</span>\n",
" </div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
"(function(root) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
"\n",
" var force = true;\n",
"\n",
" if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n",
" root._bokeh_onload_callbacks = [];\n",
" root._bokeh_is_loading = undefined;\n",
" }\n",
"\n",
" var JS_MIME_TYPE = 'application/javascript';\n",
" var HTML_MIME_TYPE = 'text/html';\n",
" var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n",
" var CLASS_NAME = 'output_bokeh rendered_html';\n",
"\n",
" /**\n",
" * Render data to the DOM node\n",
" */\n",
" function render(props, node) {\n",
" var script = document.createElement(\"script\");\n",
" node.appendChild(script);\n",
" }\n",
"\n",
" /**\n",
" * Handle when an output is cleared or removed\n",
" */\n",
" function handleClearOutput(event, handle) {\n",
" var cell = handle.cell;\n",
"\n",
" var id = cell.output_area._bokeh_element_id;\n",
" var server_id = cell.output_area._bokeh_server_id;\n",
" // Clean up Bokeh references\n",
" if (id != null && id in Bokeh.index) {\n",
" Bokeh.index[id].model.document.clear();\n",
" delete Bokeh.index[id];\n",
" }\n",
"\n",
" if (server_id !== undefined) {\n",
" // Clean up Bokeh references\n",
" var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n",
" cell.notebook.kernel.execute(cmd, {\n",
" iopub: {\n",
" output: function(msg) {\n",
" var id = msg.content.text.trim();\n",
" if (id in Bokeh.index) {\n",
" Bokeh.index[id].model.document.clear();\n",
" delete Bokeh.index[id];\n",
" }\n",
" }\n",
" }\n",
" });\n",
" // Destroy server and session\n",
" var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n",
" cell.notebook.kernel.execute(cmd);\n",
" }\n",
" }\n",
"\n",
" /**\n",
" * Handle when a new output is added\n",
" */\n",
" function handleAddOutput(event, handle) {\n",
" var output_area = handle.output_area;\n",
" var output = handle.output;\n",
"\n",
" // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n",
" if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n",
" return\n",
" }\n",
"\n",
" var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n",
"\n",
" if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n",
" toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n",
" // store reference to embed id on output_area\n",
" output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n",
" }\n",
" if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n",
" var bk_div = document.createElement(\"div\");\n",
" bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n",
" var script_attrs = bk_div.children[0].attributes;\n",
" for (var i = 0; i < script_attrs.length; i++) {\n",
" toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n",
" }\n",
" // store reference to server id on output_area\n",
" output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n",
" }\n",
" }\n",
"\n",
" function register_renderer(events, OutputArea) {\n",
"\n",
" function append_mime(data, metadata, element) {\n",
" // create a DOM node to render to\n",
" var toinsert = this.create_output_subarea(\n",
" metadata,\n",
" CLASS_NAME,\n",
" EXEC_MIME_TYPE\n",
" );\n",
" this.keyboard_manager.register_events(toinsert);\n",
" // Render to node\n",
" var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n",
" render(props, toinsert[toinsert.length - 1]);\n",
" element.append(toinsert);\n",
" return toinsert\n",
" }\n",
"\n",
" /* Handle when an output is cleared or removed */\n",
" events.on('clear_output.CodeCell', handleClearOutput);\n",
" events.on('delete.Cell', handleClearOutput);\n",
"\n",
" /* Handle when a new output is added */\n",
" events.on('output_added.OutputArea', handleAddOutput);\n",
"\n",
" /**\n",
" * Register the mime type and append_mime function with output_area\n",
" */\n",
" OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n",
" /* Is output safe? */\n",
" safe: true,\n",
" /* Index of renderer in `output_area.display_order` */\n",
" index: 0\n",
" });\n",
" }\n",
"\n",
" // register the mime type if in Jupyter Notebook environment and previously unregistered\n",
" if (root.Jupyter !== undefined) {\n",
" var events = require('base/js/events');\n",
" var OutputArea = require('notebook/js/outputarea').OutputArea;\n",
"\n",
" if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n",
" register_renderer(events, OutputArea);\n",
" }\n",
" }\n",
"\n",
" \n",
" if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
" root._bokeh_timeout = Date.now() + 5000;\n",
" root._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",
" var el = document.getElementById(\"1098\");\n",
" if (el != null) {\n",
" el.textContent = \"BokehJS is loading...\";\n",
" }\n",
" if (root.Bokeh !== undefined) {\n",
" if (el != null) {\n",
" el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n",
" }\n",
" } else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\n",
" }\n",
"\n",
"\n",
" function run_callbacks() {\n",
" try {\n",
" root._bokeh_onload_callbacks.forEach(function(callback) {\n",
" if (callback != null)\n",
" callback();\n",
" });\n",
" } finally {\n",
" delete root._bokeh_onload_callbacks\n",
" }\n",
" console.debug(\"Bokeh: all callbacks have finished\");\n",
" }\n",
"\n",
" function load_libs(css_urls, js_urls, callback) {\n",
" if (css_urls == null) css_urls = [];\n",
" if (js_urls == null) js_urls = [];\n",
"\n",
" root._bokeh_onload_callbacks.push(callback);\n",
" if (root._bokeh_is_loading > 0) {\n",
" console.debug(\"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.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" root._bokeh_is_loading = css_urls.length + js_urls.length;\n",
"\n",
" function on_load() {\n",
" root._bokeh_is_loading--;\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n",
" run_callbacks()\n",
" }\n",
" }\n",
"\n",
" function on_error() {\n",
" console.error(\"failed to load \" + url);\n",
" }\n",
"\n",
" for (var i = 0; i < css_urls.length; i++) {\n",
" var url = css_urls[i];\n",
" const element = document.createElement(\"link\");\n",
" element.onload = on_load;\n",
" element.onerror = on_error;\n",
" element.rel = \"stylesheet\";\n",
" element.type = \"text/css\";\n",
" element.href = url;\n",
" console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n",
" document.body.appendChild(element);\n",
" }\n",
"\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var element = document.createElement('script');\n",
" element.onload = on_load;\n",
" element.onerror = on_error;\n",
" element.async = false;\n",
" element.src = url;\n",
" console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.head.appendChild(element);\n",
" }\n",
" };var element = document.getElementById(\"1098\");\n",
" if (element == null) {\n",
" console.error(\"Bokeh: ERROR: autoload.js configured with elementid '1098' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
"\n",
" function inject_raw_css(css) {\n",
" const element = document.createElement(\"style\");\n",
" element.appendChild(document.createTextNode(css));\n",
" document.body.appendChild(element);\n",
" }\n",
"\n",
" \n",
" var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.4.0.min.js\"];\n",
" var css_urls = [];\n",
" \n",
"\n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" Bokeh.set_log_level(\"info\");\n",
" },\n",
" function(Bokeh) {\n",
" \n",
" \n",
" }\n",
" ];\n",
"\n",
" function run_inline_js() {\n",
" \n",
" if (root.Bokeh !== undefined || force === true) {\n",
" \n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i].call(root, root.Bokeh);\n",
" }\n",
" if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!root._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" root._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"1098\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\n",
"\n",
" }\n",
"\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(css_urls, js_urls, function() {\n",
" console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
"}(window));"
],
"application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n\n if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._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 var el = document.getElementById(\"1098\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) {\n if (callback != null)\n callback();\n });\n } finally {\n delete root._bokeh_onload_callbacks\n }\n console.debug(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(css_urls, js_urls, callback) {\n if (css_urls == null) css_urls = [];\n if (js_urls == null) js_urls = [];\n\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.debug(\"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.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n function on_load() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n run_callbacks()\n }\n }\n\n function on_error() {\n console.error(\"failed to load \" + url);\n }\n\n for (var i = 0; i < css_urls.length; i++) {\n var url = css_urls[i];\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error;\n element.rel = \"stylesheet\";\n element.type = \"text/css\";\n element.href = url;\n console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n document.body.appendChild(element);\n }\n\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n var element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error;\n element.async = false;\n element.src = url;\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n };var element = document.getElementById(\"1098\");\n if (element == null) {\n console.error(\"Bokeh: ERROR: autoload.js configured with elementid '1098' but no matching script tag was found. \")\n return false;\n }\n\n function inject_raw_css(css) {\n const element = document.createElement(\"style\");\n element.appendChild(document.createTextNode(css));\n document.body.appendChild(element);\n }\n\n \n var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.4.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.4.0.min.js\"];\n var css_urls = [];\n \n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n function(Bokeh) {\n \n \n }\n ];\n\n function run_inline_js() {\n \n if (root.Bokeh !== undefined || force === true) {\n \n for (var i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }\n if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n var cell = $(document.getElementById(\"1098\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(css_urls, js_urls, function() {\n console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
" <div class=\"bk-root\" id=\"7fa07d9e-f76a-4b02-b1fe-f10e652ad3ec\" data-root-id=\"1104\"></div>\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"(function(root) {\n",
" function embed_document(root) {\n",
" \n",
" var docs_json = {\"21bd1608-ec48-4ff2-8690-6afb4b1d7856\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1115\",\"type\":\"LinearAxis\"}],\"center\":[{\"id\":\"1119\",\"type\":\"Grid\"},{\"id\":\"1124\",\"type\":\"Grid\"},{\"id\":\"1099\",\"type\":\"Span\"},{\"id\":\"1100\",\"type\":\"Span\"},{\"id\":\"1102\",\"type\":\"LabelSet\"}],\"left\":[{\"id\":\"1120\",\"type\":\"LinearAxis\"}],\"plot_height\":840,\"plot_width\":840,\"renderers\":[{\"id\":\"1142\",\"type\":\"GlyphRenderer\"},{\"id\":\"1147\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"1105\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1131\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1107\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"1111\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1109\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"1113\",\"type\":\"LinearScale\"}},\"id\":\"1104\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1125\",\"type\":\"PanTool\"},{\"id\":\"1126\",\"type\":\"WheelZoomTool\"},{\"id\":\"1127\",\"type\":\"BoxZoomTool\"},{\"id\":\"1128\",\"type\":\"SaveTool\"},{\"id\":\"1129\",\"type\":\"ResetTool\"},{\"id\":\"1130\",\"type\":\"HelpTool\"}]},\"id\":\"1131\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1162\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1125\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1166\",\"type\":\"Selection\"},{\"attributes\":{\"callback\":null},\"id\":\"1107\",\"type\":\"DataRange1d\"},{\"attributes\":{\"line_color\":\"blue\",\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1145\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1126\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"line_dash\":[6],\"location\":1.0768867592290237},\"id\":\"1100\",\"type\":\"Span\"},{\"attributes\":{},\"id\":\"1159\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1167\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"text\":\"Mickey Mantle OPS\"},\"id\":\"1105\",\"type\":\"Title\"},{\"attributes\":{\"overlay\":{\"id\":\"1168\",\"type\":\"BoxAnnotation\"}},\"id\":\"1127\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"callback\":null,\"data\":{\"age\":[27.044058744993325,19.0],\"names\":[\"Peak Age\",\"Max\"],\"ops\":[0.792,1.0768867592290237]},\"selected\":{\"id\":\"1166\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1167\",\"type\":\"UnionRenderers\"}},\"id\":\"1101\",\"type\":\"ColumnDataSource\"},{\"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},\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1168\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"source\":{\"id\":\"1144\",\"type\":\"ColumnDataSource\"}},\"id\":\"1148\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1128\",\"type\":\"SaveTool\"},{\"attributes\":{\"data_source\":{\"id\":\"1138\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1140\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1141\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"1143\",\"type\":\"CDSView\"}},\"id\":\"1142\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1111\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1129\",\"type\":\"ResetTool\"},{\"attributes\":{\"callback\":null},\"id\":\"1109\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1130\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"1165\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1164\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1113\",\"type\":\"LinearScale\"},{\"attributes\":{\"dimension\":\"height\",\"line_dash\":[6],\"location\":27.044058744993325},\"id\":\"1099\",\"type\":\"Span\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"age\"},\"y\":{\"field\":\"OPS\"}},\"id\":\"1141\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1161\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"axis_label\":\"AGE\",\"formatter\":{\"id\":\"1159\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1116\",\"type\":\"BasicTicker\"}},\"id\":\"1115\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1116\",\"type\":\"BasicTicker\"},{\"attributes\":{\"fill_color\":{\"value\":\"black\"},\"x\":{\"field\":\"age\"},\"y\":{\"field\":\"OPS\"}},\"id\":\"1140\",\"type\":\"Circle\"},{\"attributes\":{\"axis_label\":\"OPS\",\"formatter\":{\"id\":\"1161\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1121\",\"type\":\"BasicTicker\"}},\"id\":\"1120\",\"type\":\"LinearAxis\"},{\"attributes\":{\"ticker\":{\"id\":\"1116\",\"type\":\"BasicTicker\"}},\"id\":\"1119\",\"type\":\"Grid\"},{\"attributes\":{\"data_source\":{\"id\":\"1144\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1145\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1146\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"1148\",\"type\":\"CDSView\"}},\"id\":\"1147\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1121\",\"type\":\"BasicTicker\"},{\"attributes\":{\"callback\":null,\"data\":{\"2B\":[11,37,24,17,25,22,28,21,23,17,16,15,8,25,12,12,17,14],\"3B\":[5,7,3,12,11,5,6,1,4,6,6,1,0,2,1,1,0,1],\"AB\":[341,549,461,543,517,533,474,519,541,527,514,377,172,465,361,333,440,435],\"BA\":{\"__ndarray__\":\"SgwCK4cW0T+BlUOLbOfTP+F6FK5H4dI/MzMzMzMz0z8v3SQGgZXTP2Q730+Nl9Y/XI/C9Shc1z/b+X5qvHTTPz0K16NwPdI/mpmZmZmZ0T99PzVeuknUPyUGgZVDi9Q/f2q8dJMY1D8xCKwcWmTTP1K4HoXrUdA/O99PjZdu0j9cj8L1KFzPP7x0kxgEVs4/\",\"dtype\":\"float64\",\"shape\":[18]},\"BB\":[43,75,79,102,113,112,146,129,93,111,126,122,40,99,73,57,107,106],\"CS\":{\"__ndarray__\":\"AAAAAAAAHEAAAAAAAADwPwAAAAAAABBAAAAAAAAAAEAAAAAAAADwPwAAAAAAAPA/AAAAAAAACEAAAAAAAAAIQAAAAAAAAAhAAAAAAAAACEAAAAAAAADwPwAAAAAAAAAAAAAAAAAA8D8AAAAAAAAIQAAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAABA\",\"dtype\":\"float64\",\"shape\":[18]},\"G\":[96,142,127,146,147,150,144,150,144,153,153,123,65,143,122,108,144,144],\"GIDP\":{\"__ndarray__\":\"AAAAAAAACEAAAAAAAAAUQAAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABBAAAAAAAAAFEAAAAAAAAAmQAAAAAAAABxAAAAAAAAAJkAAAAAAAAAAQAAAAAAAABBAAAAAAAAAFEAAAAAAAAAiQAAAAAAAACZAAAAAAAAAIkAAAAAAAAAiQAAAAAAAACJA\",\"dtype\":\"float64\",\"shape\":[18]},\"H\":[91,171,136,163,158,188,173,158,154,145,163,121,54,141,92,96,108,103],\"HBP\":{\"__ndarray__\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIQAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAABAAAAAAAAA8D8AAAAAAAAAAAAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwPwAAAAAAAPA/\",\"dtype\":\"float64\",\"shape\":[18]},\"HR\":[13,23,21,27,37,52,34,42,31,40,54,30,15,35,19,23,22,18],\"IBB\":{\"__ndarray__\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYQAAAAAAAABhAAAAAAAAAN0AAAAAAAAAqQAAAAAAAABhAAAAAAAAAGEAAAAAAAAAiQAAAAAAAACJAAAAAAAAAEEAAAAAAAAAyQAAAAAAAABxAAAAAAAAAFEAAAAAAAAAcQAAAAAAAABxA\",\"dtype\":\"float64\",\"shape\":[18]},\"OBP\":{\"__ndarray__\":\"vHSTGARW1j+e76fGSzfZP0a28/3UeNk/6SYxCKwc2j8v3SQGgZXbPxkEVg4tst0//Knx0k1i4D8nMQisHFrcP/YoXI/C9dg/8KfGSzeJ2T956SYxCKzcP7TIdr6fGt8/001iEFg53D/fT42XbhLbP6jGSzeJQdg/TDeJQWDl2D+gGi/dJAbZP6RwPQrXo9g/\",\"dtype\":\"float64\",\"shape\":[18]},\"OPS\":{\"__ndarray__\":\"8tJNYhBY6T/FILByaJHtP6RwPQrXo+w/QmDl0CLb7T956SYxCKzwP05iEFg5tPI/okW28/3U8j+PwvUoXI/wPyGwcmiR7ew/OrTIdr6f7j8pXI/C9SjyP9v5fmq8dPE/NV66SQwC8T/TTWIQWDnwP2Q730+Nl+o/RIts5/up7T9mZmZmZmbqP3WTGARWDuk/\",\"dtype\":\"float64\",\"shape\":[18]},\"R\":[61,94,105,129,121,132,121,127,104,119,132,96,40,92,44,40,63,57],\"RBI\":{\"__ndarray__\":\"AAAAAABAUEAAAAAAAMBVQAAAAAAAAFdAAAAAAACAWUAAAAAAAMBYQAAAAAAAQGBAAAAAAACAV0AAAAAAAEBYQAAAAAAAwFJAAAAAAACAV0AAAAAAAABgQAAAAAAAQFZAAAAAAACAQUAAAAAAAMBbQAAAAAAAAEdAAAAAAAAATEAAAAAAAIBLQAAAAAAAAEtA\",\"dtype\":\"float64\",\"shape\":[18]},\"SB\":{\"__ndarray__\":\"AAAAAAAAIEAAAAAAAAAQQAAAAAAAACBAAAAAAAAAFEAAAAAAAAAgQAAAAAAAACRAAAAAAAAAMEAAAAAAAAAyQAAAAAAAADVAAAAAAAAALEAAAAAAAAAoQAAAAAAAACJAAAAAAAAAAEAAAAAAAAAYQAAAAAAAABBAAAAAAAAA8D8AAAAAAADwPwAAAAAAABhA\",\"dtype\":\"float64\",\"shape\":[18]},\"SF\":{\"__ndarray__\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEEAAAAAAAAAIQAAAAAAAABBAAAAAAAAACEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAFEAAAAAAAAAUQAAAAAAAAABAAAAAAAAA8D8AAAAAAAAIQAAAAAAAAPA/AAAAAAAACEAAAAAAAAAUQAAAAAAAABBA\",\"dtype\":\"float64\",\"shape\":[18]},\"SH\":{\"__ndarray__\":\"AAAAAAAAAEAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAQAAAAAAAAPA/AAAAAAAAAAAAAAAAAADwPwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA/\",\"dtype\":\"float64\",\"shape\":[18]},\"SINGLE\":[62,104,88,107,85,109,105,94,96,82,87,75,31,79,60,60,69,70],\"SLG\":{\"__ndarray__\":\"JzEIrBxa3D/2KFyPwvXgPwIrhxbZzt8/zczMzMzM4D9aZDvfT43jP4/C9Shcj+Y/SOF6FK5H5T+LbOf7qfHiP6abxCCwcuA/QmDl0CLb4T+WQ4ts5/vlP1yPwvUoXOM/gZVDi2zn4z+28/3UeOniPyGwcmiR7dw/nu+nxks34T8tsp3vp8bbP0a28/3UeNk/\",\"dtype\":\"float64\",\"shape\":[18]},\"SO\":{\"__ndarray__\":\"AAAAAACAUkAAAAAAAMBbQAAAAAAAgFZAAAAAAADAWkAAAAAAAEBYQAAAAAAAwFhAAAAAAADAUkAAAAAAAABeQAAAAAAAgF9AAAAAAABAX0AAAAAAAABcQAAAAAAAgFNAAAAAAAAAQEAAAAAAAIBZQAAAAAAAAFNAAAAAAAAAU0AAAAAAAEBcQAAAAAAAQFhA\",\"dtype\":\"float64\",\"shape\":[18]},\"TB\":[151,291,229,285,316,376,315,307,278,294,353,228,107,275,163,179,191,173],\"age\":{\"__ndarray__\":\"AAAAAAAAM0AAAAAAAAA0QAAAAAAAADVAAAAAAAAANkAAAAAAAAA3QAAAAAAAADhAAAAAAAAAOUAAAAAAAAA6QAAAAAAAADtAAAAAAAAAPEAAAAAAAAA9QAAAAAAAAD5AAAAAAAAAP0AAAAAAAABAQAAAAAAAgEBAAAAAAAAAQUAAAAAAAIBBQAAAAAAAAEJA\",\"dtype\":\"float64\",\"shape\":[18]},\"index\":[35380,36008,36620,37199,37823,38467,39074,39706,40347,40992,41674,42389,43145,43883,44643,45412,46188,46924],\"lgID\":[\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\",\"AL\"],\"playerID\":[\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\",\"mantlmi01\"],\"playerName\":[\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\",\"Mickey Mantle\"],\"stint\":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],\"teamID\":[\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\",\"NYA\"],\"yearID\":[1951,1952,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,1967,1968]},\"selected\":{\"id\":\"1162\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1163\",\"type\":\"UnionRenderers\"}},\"id\":\"1138\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"dimension\":1,\"ticker\":{\"id\":\"1121\",\"type\":\"BasicTicker\"}},\"id\":\"1124\",\"type\":\"Grid\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b4\",\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1146\",\"type\":\"Line\"},{\"attributes\":{\"level\":\"glyph\",\"source\":{\"id\":\"1101\",\"type\":\"ColumnDataSource\"},\"text\":{\"field\":\"names\"},\"x\":{\"field\":\"age\"},\"x_offset\":{\"value\":5},\"y\":{\"field\":\"ops\"},\"y_offset\":{\"value\":5}},\"id\":\"1102\",\"type\":\"LabelSet\"},{\"attributes\":{\"source\":{\"id\":\"1138\",\"type\":\"ColumnDataSource\"}},\"id\":\"1143\",\"type\":\"CDSView\"},{\"attributes\":{\"callback\":null,\"data\":{\"x\":{\"__ndarray__\":\"AAAAAAAAM0AAAAAAAAA0QAAAAAAAADVAAAAAAAAANkAAAAAAAAA3QAAAAAAAADhAAAAAAAAAOUAAAAAAAAA6QAAAAAAAADtAAAAAAAAAPEAAAAAAAAA9QAAAAAAAAD5AAAAAAAAAP0AAAAAAAABAQAAAAAAAgEBAAAAAAAAAQUAAAAAAAIBBQAAAAAAAAEJA\",\"dtype\":\"float64\",\"shape\":[18]},\"y\":{\"__ndarray__\":\"noiO/TN16j8iGH+S5lLsPzKWSPhG8e0/tgLrLlVQ7z/dLjObCDjwP51TXYc9qPA/ne/zW8n48D/bAvcYrCnxP1mNZr7lOvE/E49CTHYs8T8NCIvCXf7wP0f4PyGcsPA/v19haDFD8D/qfN4vO2zvP9Yo01/BEu4/PsOgYPV57D8iTEcy16HqP4bDxtRmiug/\",\"dtype\":\"float64\",\"shape\":[18]}},\"selected\":{\"id\":\"1164\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1165\",\"type\":\"UnionRenderers\"}},\"id\":\"1144\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1163\",\"type\":\"UnionRenderers\"}],\"root_ids\":[\"1104\"]},\"title\":\"Bokeh Application\",\"version\":\"1.4.0\"}};\n",
" var render_items = [{\"docid\":\"21bd1608-ec48-4ff2-8690-6afb4b1d7856\",\"roots\":{\"1104\":\"7fa07d9e-f76a-4b02-b1fe-f10e652ad3ec\"}}];\n",
" root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
"\n",
" }\n",
" if (root.Bokeh !== undefined) {\n",
" embed_document(root);\n",
" } else {\n",
" var attempts = 0;\n",
" var timer = setInterval(function(root) {\n",
" if (root.Bokeh !== undefined) {\n",
" clearInterval(timer);\n",
" embed_document(root);\n",
" } else {\n",
" attempts++;\n",
" if (attempts > 100) {\n",
" clearInterval(timer);\n",
" console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
" }\n",
" }\n",
" }, 10, root)\n",
" }\n",
"})(window);"
],
"application/vnd.bokehjs_exec.v0+json": ""
},
"metadata": {
"application/vnd.bokehjs_exec.v0+json": {
"id": "1104"
}
},
"output_type": "display_data"
}
],
"source": [
"# OPS + Peekを可視化\n",
"from scipy.interpolate import CubicSpline\n",
"import numpy as np\n",
"from bokeh.models import Span, ColumnDataSource, LabelSet\n",
"\n",
"output_notebook()\n",
"\n",
"# Vertical line\n",
"vline = Span(location=age_max, dimension='height', line_color='black', line_dash='dashed', line_width=1)\n",
"# Horizontal line\n",
"hline = Span(location=_max, dimension='width', line_color='black', line_dash='dashed', line_width=1)\n",
"\n",
"x = df_mantle['age'].values\n",
"y= df_mantle['OPS'].values\n",
"\n",
"\n",
"res = np.polyfit(x, y, 2)\n",
"y1 = np.poly1d(res)(x)\n",
"spl = CubicSpline(x, y1)\n",
"y_smooth = spl(x)\n",
"\n",
"# Label\n",
"source = ColumnDataSource(data=dict(ops=[y[0], _max], \n",
" age=[age_max, x[0]],\n",
" names=['Peak Age', 'Max']))\n",
"labels = LabelSet(x='age', y='ops', text='names', level='glyph', x_offset=5, y_offset=5, source=source, render_mode='canvas')\n",
"\n",
"\n",
"\n",
"p = figure(\n",
" plot_width=840, \n",
" plot_height=840, \n",
" title=\"Mickey Mantle OPS\",\n",
" x_axis_label='AGE',\n",
" y_axis_label='OPS',\n",
")\n",
"p.circle(x='age', y='OPS', size=4, source=df_mantle, fill_color='black', line_color='black')\n",
"p.line(x, y_smooth, line_color='blue')\n",
"p.add_layout(vline)\n",
"p.add_layout(hline)\n",
"p.add_layout(labels)\n",
"\n",
"show(p)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment