Skip to content

Instantly share code, notes, and snippets.

@Shinichi-Nakagawa
Created November 27, 2019 15:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shinichi-Nakagawa/9265d402365e86ba88166fef7109cbef to your computer and use it in GitHub Desktop.
Save Shinichi-Nakagawa/9265d402365e86ba88166fef7109cbef to your computer and use it in GitHub Desktop.
PySpark検証サンプル
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div>\n",
" <p><b>SparkContext</b></p>\n",
"\n",
" <p><a href=\"http://shinichinombp:4040\">Spark UI</a></p>\n",
"\n",
" <dl>\n",
" <dt>Version</dt>\n",
" <dd><code>v2.4.4</code></dd>\n",
" <dt>Master</dt>\n",
" <dd><code>local[*]</code></dd>\n",
" <dt>AppName</dt>\n",
" <dd><code>PySparkShell</code></dd>\n",
" </dl>\n",
" </div>\n",
" "
],
"text/plain": [
"<SparkContext master=local[*] appName=PySparkShell>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# SparkのVersionを確認\n",
"sc"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# データ\n",
"from pyspark.sql.types import *\n",
"\n",
"# 後ほどDataFrameのデータ型を指定するため使う\n",
"STATS_COLUMNS = ['G', 'AB', 'R', 'H', '2B', '3B', 'HR', 'RBI', 'SB', 'CS', 'BB', 'SO', 'IBB', 'HBP', 'SH', 'SF', 'GIDP']\n",
"\n",
"# 打撃成績Schemaのデータ型\n",
"schema_batting = StructType(\n",
" [\n",
" StructField('playerID', StringType(), False),\n",
" StructField('yearID', IntegerType(), False),\n",
" StructField('stint', IntegerType(), False),\n",
" StructField('teamID', StringType(), False),\n",
" StructField('lgID', StringType(), False),\n",
" StructField('G', IntegerType(), True),\n",
" StructField('AB', IntegerType(), True),\n",
" StructField('R', IntegerType(), True),\n",
" StructField('H', IntegerType(), True),\n",
" StructField('2B', IntegerType(), True),\n",
" StructField('3B', IntegerType(), True),\n",
" StructField('HR', IntegerType(), True),\n",
" StructField('RBI', IntegerType(), True),\n",
" StructField('SB', IntegerType(), True),\n",
" StructField('CS', IntegerType(), True),\n",
" StructField('BB', IntegerType(), True),\n",
" StructField('SO', IntegerType(), True),\n",
" StructField('IBB', IntegerType(), True),\n",
" StructField('HBP', IntegerType(), True),\n",
" StructField('SH', IntegerType(), True),\n",
" StructField('SF', IntegerType(), True),\n",
" StructField('GIDP', IntegerType(), True),\n",
" ]\n",
")\n",
"\n",
"# 選手情報Schemaのデータ型\n",
"schema_player = StructType(\n",
" [\n",
" StructField('playerID', StringType(), False),\n",
" StructField('birthYear', IntegerType(), True),\n",
" StructField('birthMonth', IntegerType(), True),\n",
" StructField('birthDay', IntegerType(), True),\n",
" StructField('birthCountry', StringType(), True),\n",
" StructField('birthState', StringType(), True),\n",
" StructField('birthCity', StringType(), True),\n",
" StructField('deathYear', IntegerType(), True),\n",
" StructField('deathMonth', IntegerType(), True),\n",
" StructField('deathDay', IntegerType(), True),\n",
" StructField('deathCountry', StringType(), True),\n",
" StructField('deathState', StringType(), True),\n",
" StructField('deathCity', StringType(), True),\n",
" StructField('nameFirst', StringType(), True),\n",
" StructField('nameLast', StringType(), True),\n",
" StructField('nameGiven', StringType(), True),\n",
" StructField('weight', IntegerType(), True),\n",
" StructField('height', IntegerType(), True),\n",
" StructField('bats', StringType(), True),\n",
" StructField('throws', StringType(), True),\n",
" StructField('debut', DateType(), True),\n",
" StructField('finalGame', DateType(), True),\n",
" StructField('retroID', StringType(), True),\n",
" StructField('bbrefID', StringType(), True), \n",
" ]\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# RDD(耐障害文選データセット)をCSVから作る\n",
"rdd_batting = sc.textFile('../../../baseballdatabank/core/Batting.csv', 4)\n",
"rdd_player = sc.textFile('../../../baseballdatabank/core/People.csv', 4)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# RDDをDataFrameに変換\n",
"batting = spark.read.option(\"header\",\"true\").format(\"csv\").schema(schema_batting).csv(rdd_batting)\n",
"player = spark.read.option(\"header\",\"true\").format(\"csv\").schema(schema_player).csv(rdd_player)\n",
"batting = batting.fillna(0, subset=STATS_COLUMNS)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Spark SQLからデータを操作するため一時的なテーブルを作る\n",
"batting.createOrReplaceTempView('batting')\n",
"player.createOrReplaceTempView('player')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"+----+-------------------+---+---+\n",
"|YEAR| BA| HR|RBI|\n",
"+----+-------------------+---+---+\n",
"|2001|0.34971098265895956| 8| 69|\n",
"|2002| 0.321483771251932| 8| 51|\n",
"|2003| 0.3122238586156112| 13| 62|\n",
"|2004| 0.3721590909090909| 8| 60|\n",
"|2005|0.30338733431516934| 15| 68|\n",
"|2006|0.32230215827338127| 9| 49|\n",
"|2007|0.35103244837758113| 6| 68|\n",
"|2008| 0.3104956268221574| 6| 42|\n",
"|2009| 0.352112676056338| 11| 46|\n",
"|2010|0.31470588235294117| 6| 43|\n",
"|2011| 0.2717872968980798| 5| 47|\n",
"|2012|0.26119402985074625| 4| 28|\n",
"|2012|0.32158590308370044| 5| 27|\n",
"|2013|0.26153846153846155| 7| 35|\n",
"|2014| 0.2841225626740947| 1| 22|\n",
"|2015| 0.228643216080402| 1| 21|\n",
"|2016| 0.290519877675841| 1| 22|\n",
"|2017|0.25510204081632654| 3| 20|\n",
"|2018|0.20454545454545456| 0| 0|\n",
"+----+-------------------+---+---+\n",
"\n"
]
}
],
"source": [
"# 挨拶代わりにIchiro Suzukiの打率(BA), ホームラン(HR), 打点(RBI)を出す\n",
"query = '''\n",
"select \n",
"b.yearID as YEAR,\n",
"b.H / b.AB as BA,\n",
"b.HR as HR,\n",
"b.RBI as RBI\n",
"from batting as b join player as p on b.playerID = p.playerID \n",
"where p.nameFirst=\"{firstname}\" and p.nameLast=\"{lastname}\"\n",
"order by b.yearID asc\n",
"'''\n",
"\n",
"spark.sql(query.format(firstname=\"Ichiro\", lastname=\"Suzuki\")).show()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# 本命のデータ分析(OPSの年度別経過)を見るため関数を定義する\n",
"\n",
"from pyspark.sql.functions import udf, col\n",
"\n",
"# sabrは野球指標を計算するためのライブラリ\n",
"from sabr.stats import Stats\n",
"\n",
"@udf(DoubleType())\n",
"def ba(ab, h):\n",
" # 打率\n",
" try:\n",
" return Stats.avg(ab=ab, h=h)\n",
" except ZeroDivisionError as e:\n",
" return 0.0\n",
"\n",
"@udf(DoubleType())\n",
"def obp(ab, h, bb, sf, hbp):\n",
" # 出塁率\n",
" try:\n",
" return Stats.obp(ab=ab, h=h, bb=bb, sf=sf, hbp=hbp)\n",
" except ZeroDivisionError as e:\n",
" return 0.0\n",
"\n",
"@udf(DoubleType())\n",
"def slg(ab, h, _2b, _3b, hr):\n",
" # 長打率\n",
" try:\n",
" _1b = h - (_2b + _3b + hr)\n",
" tb = _1b * 1 + _2b * 2 + _3b * 3 + hr * 4\n",
" return Stats.slg(ab=ab, tb=tb)\n",
" except ZeroDivisionError as e:\n",
" return 0.0"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# DataFrameに打率, 出塁率, 長打率, OPS(出塁率 + 長打率)を追加\n",
"batting = batting.withColumn('BA', ba('AB', 'H'))\n",
"batting = batting.withColumn('OBP', obp('AB', 'H', 'BB', 'SF', 'HBP'))\n",
"batting = batting.withColumn('SLG', slg('AB', 'H', '2B', '3B', 'HR'))\n",
"batting = batting.withColumn('OPS', col('OBP') + col('SLG'))\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# テーブルを更新\n",
"batting.createOrReplaceTempView('batting')\n",
"\n",
"# 年齢, OPSおよび予測に必要なデータを返す関数\n",
"def batting_stats(firstname, lastname):\n",
" query = '''\n",
" select \n",
" b.yearID - p.birthYear - 1 as AGE,\n",
" b.OPS as OPS,\n",
" (b.yearID - p.birthYear - 1) -30 as age_30,\n",
" pow((b.yearID - p.birthYear - 1) -30, 2) as age_30_2\n",
" from batting as b join player as p on b.playerID = p.playerID \n",
" where p.nameFirst=\"{firstname}\" and p.nameLast=\"{lastname}\"\n",
" order by b.yearID asc\n",
" '''\n",
" return spark.sql(query.format(firstname=firstname, lastname=lastname))\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"+---+-------------------+------+--------+\n",
"|AGE| OPS|age_30|age_30_2|\n",
"+---+-------------------+------+--------+\n",
"| 27| 0.8380000000000001| -3| 9.0|\n",
"| 28| 0.813| -2| 4.0|\n",
"| 29| 0.788| -1| 1.0|\n",
"| 30| 0.869| 0| 0.0|\n",
"| 31| 0.786| 1| 1.0|\n",
"| 32| 0.786| 2| 4.0|\n",
"| 33| 0.827| 3| 9.0|\n",
"| 34| 0.747| 4| 16.0|\n",
"| 35| 0.851| 5| 25.0|\n",
"| 36| 0.753| 6| 36.0|\n",
"| 37| 0.645| 7| 49.0|\n",
"| 38| 0.641| 8| 64.0|\n",
"| 38| 0.794| 8| 64.0|\n",
"| 39| 0.639| 9| 81.0|\n",
"| 40| 0.664| 10| 100.0|\n",
"| 41| 0.5609999999999999| 11| 121.0|\n",
"| 42| 0.73| 12| 144.0|\n",
"| 43| 0.65| 13| 169.0|\n",
"| 44|0.45999999999999996| 14| 196.0|\n",
"+---+-------------------+------+--------+\n",
"\n"
]
}
],
"source": [
"# Ichiro Suzukiの成績を出力して動作チェック\n",
"ichiro = batting_stats(\"Ichiro\", \"Suzuki\")\n",
"ichiro.show()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"+---+------------------+------+--------+\n",
"|AGE| OPS|age_30|age_30_2|\n",
"+---+------------------+------+--------+\n",
"| 19| 0.792| -11| 121.0|\n",
"| 20| 0.924| -10| 100.0|\n",
"| 21| 0.895| -9| 81.0|\n",
"| 22| 0.933| -8| 64.0|\n",
"| 23| 1.042| -7| 49.0|\n",
"| 24| 1.169| -6| 36.0|\n",
"| 25| 1.177| -5| 25.0|\n",
"| 26| 1.035| -4| 16.0|\n",
"| 27| 0.904| -3| 9.0|\n",
"| 28|0.9570000000000001| -2| 4.0|\n",
"| 29| 1.135| -1| 1.0|\n",
"| 30| 1.091| 0| 0.0|\n",
"| 31| 1.063| 1| 1.0|\n",
"| 32| 1.014| 2| 4.0|\n",
"| 33| 0.831| 3| 9.0|\n",
"| 34| 0.927| 4| 16.0|\n",
"| 35| 0.825| 5| 25.0|\n",
"| 36| 0.783| 6| 36.0|\n",
"+---+------------------+------+--------+\n",
"\n"
]
}
],
"source": [
"# 良さげなので実際に分析・可視化してみる。ミッキー・マントル選手のOPSを年齢ベースで予測する\n",
"mantle = batting_stats(\"Mickey\", \"Mantle\")\n",
"mantle.show()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# scikit-learnで線形回帰するため、pandas Dataframeに変換\n",
"df_mantle = mantle.toPandas()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"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>OPS</th>\n",
" <th>age_30</th>\n",
" <th>age_30_2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>19</td>\n",
" <td>0.792</td>\n",
" <td>-11</td>\n",
" <td>121.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>20</td>\n",
" <td>0.924</td>\n",
" <td>-10</td>\n",
" <td>100.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>21</td>\n",
" <td>0.895</td>\n",
" <td>-9</td>\n",
" <td>81.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>22</td>\n",
" <td>0.933</td>\n",
" <td>-8</td>\n",
" <td>64.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>23</td>\n",
" <td>1.042</td>\n",
" <td>-7</td>\n",
" <td>49.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>24</td>\n",
" <td>1.169</td>\n",
" <td>-6</td>\n",
" <td>36.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>25</td>\n",
" <td>1.177</td>\n",
" <td>-5</td>\n",
" <td>25.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>26</td>\n",
" <td>1.035</td>\n",
" <td>-4</td>\n",
" <td>16.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>27</td>\n",
" <td>0.904</td>\n",
" <td>-3</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>28</td>\n",
" <td>0.957</td>\n",
" <td>-2</td>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>29</td>\n",
" <td>1.135</td>\n",
" <td>-1</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>30</td>\n",
" <td>1.091</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>31</td>\n",
" <td>1.063</td>\n",
" <td>1</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>32</td>\n",
" <td>1.014</td>\n",
" <td>2</td>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>33</td>\n",
" <td>0.831</td>\n",
" <td>3</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>34</td>\n",
" <td>0.927</td>\n",
" <td>4</td>\n",
" <td>16.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>35</td>\n",
" <td>0.825</td>\n",
" <td>5</td>\n",
" <td>25.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>36</td>\n",
" <td>0.783</td>\n",
" <td>6</td>\n",
" <td>36.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" AGE OPS age_30 age_30_2\n",
"0 19 0.792 -11 121.0\n",
"1 20 0.924 -10 100.0\n",
"2 21 0.895 -9 81.0\n",
"3 22 0.933 -8 64.0\n",
"4 23 1.042 -7 49.0\n",
"5 24 1.169 -6 36.0\n",
"6 25 1.177 -5 25.0\n",
"7 26 1.035 -4 16.0\n",
"8 27 0.904 -3 9.0\n",
"9 28 0.957 -2 4.0\n",
"10 29 1.135 -1 1.0\n",
"11 30 1.091 0 0.0\n",
"12 31 1.063 1 1.0\n",
"13 32 1.014 2 4.0\n",
"14 33 0.831 3 9.0\n",
"15 34 0.927 4 16.0\n",
"16 35 0.825 5 25.0\n",
"17 36 0.783 6 36.0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 念の為チェック\n",
"df_mantle"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# sklearnで線形回帰\n",
"from sklearn.linear_model import LinearRegression\n",
"Y = df_mantle['OPS'].values\n",
"X = df_mantle[['age_30', 'age_30_2']].values"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ml = LinearRegression()\n",
"ml.fit(X, Y)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0431176470588235"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 切片を確認\n",
"ml.intercept_"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(27.044058744993325, 1.0768867592290237)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Peak年齢と, そのOPS予測値を出す\n",
"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": 18,
"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=\"b3d6eb8b-3177-4bff-89ee-9251df15a90b\" data-root-id=\"1007\"></div>\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"(function(root) {\n",
" function embed_document(root) {\n",
" \n",
" var docs_json = {\"d40e2d6a-1f24-47c0-88ca-9c3a005f92ff\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1018\",\"type\":\"LinearAxis\"}],\"center\":[{\"id\":\"1022\",\"type\":\"Grid\"},{\"id\":\"1027\",\"type\":\"Grid\"},{\"id\":\"1002\",\"type\":\"Span\"},{\"id\":\"1003\",\"type\":\"Span\"},{\"id\":\"1005\",\"type\":\"LabelSet\"}],\"left\":[{\"id\":\"1023\",\"type\":\"LinearAxis\"}],\"plot_height\":840,\"plot_width\":840,\"renderers\":[{\"id\":\"1045\",\"type\":\"GlyphRenderer\"},{\"id\":\"1050\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"1008\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1034\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1010\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"1014\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1012\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"1016\",\"type\":\"LinearScale\"}},\"id\":\"1007\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"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\":\"1059\",\"type\":\"BoxAnnotation\"},{\"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\":\"1044\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1033\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"1060\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1016\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1062\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"axis_label\":\"AGE\",\"formatter\":{\"id\":\"1053\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1019\",\"type\":\"BasicTicker\"}},\"id\":\"1018\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1063\",\"type\":\"Selection\"},{\"attributes\":{\"callback\":null,\"data\":{\"age\":[27.044058744993325,19],\"names\":[\"Peak Age\",\"Max\"],\"ops\":[0.792,1.0768867592290237]},\"selected\":{\"id\":\"1058\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1057\",\"type\":\"UnionRenderers\"}},\"id\":\"1004\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"dimension\":1,\"ticker\":{\"id\":\"1024\",\"type\":\"BasicTicker\"}},\"id\":\"1027\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1019\",\"type\":\"BasicTicker\"},{\"attributes\":{\"ticker\":{\"id\":\"1019\",\"type\":\"BasicTicker\"}},\"id\":\"1022\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1053\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1024\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis_label\":\"OPS\",\"formatter\":{\"id\":\"1055\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1024\",\"type\":\"BasicTicker\"}},\"id\":\"1023\",\"type\":\"LinearAxis\"},{\"attributes\":{\"source\":{\"id\":\"1047\",\"type\":\"ColumnDataSource\"}},\"id\":\"1051\",\"type\":\"CDSView\"},{\"attributes\":{\"text\":\"Mickey Mantle OPS\"},\"id\":\"1008\",\"type\":\"Title\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b4\",\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1049\",\"type\":\"Line\"},{\"attributes\":{\"data_source\":{\"id\":\"1041\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1043\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1044\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"1046\",\"type\":\"CDSView\"}},\"id\":\"1045\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1028\",\"type\":\"PanTool\"},{\"id\":\"1029\",\"type\":\"WheelZoomTool\"},{\"id\":\"1030\",\"type\":\"BoxZoomTool\"},{\"id\":\"1031\",\"type\":\"SaveTool\"},{\"id\":\"1032\",\"type\":\"ResetTool\"},{\"id\":\"1033\",\"type\":\"HelpTool\"}]},\"id\":\"1034\",\"type\":\"Toolbar\"},{\"attributes\":{\"data_source\":{\"id\":\"1047\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1048\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1049\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"1051\",\"type\":\"CDSView\"}},\"id\":\"1050\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"callback\":null,\"data\":{\"AGE\":{\"__ndarray__\":\"EwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAB0AAAAeAAAAHwAAACAAAAAhAAAAIgAAACMAAAAkAAAA\",\"dtype\":\"int32\",\"shape\":[18]},\"OPS\":{\"__ndarray__\":\"8tJNYhBY6T/FILByaJHtP6RwPQrXo+w/QmDl0CLb7T956SYxCKzwP05iEFg5tPI/okW28/3U8j+PwvUoXI/wPyGwcmiR7ew/OrTIdr6f7j8pXI/C9SjyP9v5fmq8dPE/NV66SQwC8T/TTWIQWDnwP2Q730+Nl+o/RIts5/up7T9mZmZmZmbqP3WTGARWDuk/\",\"dtype\":\"float64\",\"shape\":[18]},\"age_30\":{\"__ndarray__\":\"9f////b////3////+P////n////6////+/////z////9/////v////////8AAAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAA\",\"dtype\":\"int32\",\"shape\":[18]},\"age_30_2\":{\"__ndarray__\":\"AAAAAABAXkAAAAAAAABZQAAAAAAAQFRAAAAAAAAAUEAAAAAAAIBIQAAAAAAAAEJAAAAAAAAAOUAAAAAAAAAwQAAAAAAAACJAAAAAAAAAEEAAAAAAAADwPwAAAAAAAAAAAAAAAAAA8D8AAAAAAAAQQAAAAAAAACJAAAAAAAAAMEAAAAAAAAA5QAAAAAAAAEJA\",\"dtype\":\"float64\",\"shape\":[18]},\"index\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]},\"selected\":{\"id\":\"1061\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1060\",\"type\":\"UnionRenderers\"}},\"id\":\"1041\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"callback\":null,\"data\":{\"x\":{\"__ndarray__\":\"EwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAB0AAAAeAAAAHwAAACAAAAAhAAAAIgAAACMAAAAkAAAA\",\"dtype\":\"int32\",\"shape\":[18]},\"y\":{\"__ndarray__\":\"noiO/TN16j8iGH+S5lLsPzKWSPhG8e0/tgLrLlVQ7z/dLjObCDjwP51TXYc9qPA/ne/zW8n48D/bAvcYrCnxP1mNZr7lOvE/E49CTHYs8T8NCIvCXf7wP0f4PyGcsPA/v19haDFD8D/qfN4vO2zvP9Yo01/BEu4/PsOgYPV57D8iTEcy16HqP4bDxtRmiug/\",\"dtype\":\"float64\",\"shape\":[18]}},\"selected\":{\"id\":\"1063\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1062\",\"type\":\"UnionRenderers\"}},\"id\":\"1047\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1061\",\"type\":\"Selection\"},{\"attributes\":{\"callback\":null},\"id\":\"1010\",\"type\":\"DataRange1d\"},{\"attributes\":{\"source\":{\"id\":\"1041\",\"type\":\"ColumnDataSource\"}},\"id\":\"1046\",\"type\":\"CDSView\"},{\"attributes\":{\"fill_color\":{\"value\":\"black\"},\"x\":{\"field\":\"AGE\"},\"y\":{\"field\":\"OPS\"}},\"id\":\"1043\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1028\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1029\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1055\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"overlay\":{\"id\":\"1059\",\"type\":\"BoxAnnotation\"}},\"id\":\"1030\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"line_color\":\"blue\",\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1048\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1057\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"dimension\":\"height\",\"line_dash\":[6],\"location\":27.044058744993325},\"id\":\"1002\",\"type\":\"Span\"},{\"attributes\":{\"line_dash\":[6],\"location\":1.0768867592290237},\"id\":\"1003\",\"type\":\"Span\"},{\"attributes\":{\"level\":\"glyph\",\"source\":{\"id\":\"1004\",\"type\":\"ColumnDataSource\"},\"text\":{\"field\":\"names\"},\"x\":{\"field\":\"age\"},\"x_offset\":{\"value\":5},\"y\":{\"field\":\"ops\"},\"y_offset\":{\"value\":5}},\"id\":\"1005\",\"type\":\"LabelSet\"},{\"attributes\":{},\"id\":\"1032\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1014\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1031\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1058\",\"type\":\"Selection\"},{\"attributes\":{\"callback\":null},\"id\":\"1012\",\"type\":\"DataRange1d\"}],\"root_ids\":[\"1007\"]},\"title\":\"Bokeh Application\",\"version\":\"1.4.0\"}};\n",
" var render_items = [{\"docid\":\"d40e2d6a-1f24-47c0-88ca-9c3a005f92ff\",\"roots\":{\"1007\":\"b3d6eb8b-3177-4bff-89ee-9251df15a90b\"}}];\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": "1007"
}
},
"output_type": "display_data"
}
],
"source": [
"# 実際のOPS(点グラフ)と成長曲線を出力\n",
"from scipy.interpolate import CubicSpline\n",
"import numpy as np\n",
"from bokeh.models import Span, ColumnDataSource, LabelSet\n",
"from bokeh.plotting import figure, show, output_notebook\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": []
}
],
"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": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment