├── readme.md ├── Discovery-Jp-Test.ipynb └── sakura-predict-csv.ipynb /readme.md: -------------------------------------------------------------------------------- 1 | readme 2 | -------------------------------------------------------------------------------- /Discovery-Jp-Test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Discoveryでコレクション作成" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# 個別設定\n", 17 | "# 管理コンソールのcredentail情報をコピーします。\n", 18 | "credencial = {\n", 19 | " \"url\": \"https://gateway.watsonplatform.net/discovery/api\",\n", 20 | " \"username\": \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\",\n", 21 | " \"password\": \"xxxxxxxxxxxx\"\n", 22 | "}" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "# ライブラリ読み込み\n", 32 | "import json\n", 33 | "from watson_developer_cloud import DiscoveryV1\n", 34 | "# In the constructor, letting the SDK manage the IAM token\n", 35 | "discovery = DiscoveryV1(version = '2018-03-05',\n", 36 | " username = credencial['username'],\n", 37 | " password = credencial['password'])" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "# Environment作成\n", 47 | "response = discovery.create_environment(\n", 48 | " name=\"my_environment\"\n", 49 | ")\n", 50 | "# environment_idの設定\n", 51 | "environment_id = response['environment_id']\n", 52 | "print(environment_id)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "# Collection作成\n", 62 | "col_name = 'JP_TEST'\n", 63 | "new_collection = discovery.create_collection(\n", 64 | " environment_id = environment_id, \n", 65 | " name = col_name, \n", 66 | " language='ja')\n", 67 | "\n", 68 | "# collection_idの設定\n", 69 | "collection_id = new_collection['collection_id']\n", 70 | "print(collection_id)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "# 必要であれば、このタイミングでエンリッチ設定の変更を行います。\n", 80 | "# 一度コレクションを日本語で作ってしまえば、エンリッチ設定変更はUIから可能です。" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "# CSVファイル読込み" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "infile = 'data_xxxxxx.csv'\n", 97 | "\n", 98 | "# このセルは、ツール生成のものを一部手で修正します。\n", 99 | "\n", 100 | "import sys\n", 101 | "import types\n", 102 | "import pandas as pd\n", 103 | "from botocore.client import Config\n", 104 | "import ibm_boto3\n", 105 | "\n", 106 | "def __iter__(self): return 0\n", 107 | "\n", 108 | "client_xxxxxx = ibm_boto3.client(service_name='s3',\n", 109 | " ibm_api_key_id='xxxxxx',\n", 110 | " ibm_auth_endpoint=\"https://iam.ng.bluemix.net/oidc/token\",\n", 111 | " config=Config(signature_version='oauth'),\n", 112 | " endpoint_url='https://s3-api.us-geo.objectstorage.service.networklayer.com')\n", 113 | "\n", 114 | "# Key=のパラメータは変数利用に修正しました\n", 115 | "body = client_xxxxxx.get_object(Bucket='xxxxxxx',Key=infile)['Body']\n", 116 | "# add missing __iter__ method, so pandas accepts body as file-like object\n", 117 | "if not hasattr(body, \"__iter__\"): body.__iter__ = types.MethodType( __iter__, body )\n", 118 | "\n", 119 | "# DataFrame名は df_dataに修正しました\n", 120 | "df_data = pd.read_csv(body)\n", 121 | "df_data.head()" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "# 読み込んだデータをDiscovey Collectionに書き込み" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": { 135 | "scrolled": true 136 | }, 137 | "outputs": [], 138 | "source": [ 139 | "import os\n", 140 | "import time\n", 141 | "\n", 142 | "for i in range(len(df_data)):\n", 143 | " item = df_data.iloc[i,:]\n", 144 | " data = {}\n", 145 | " \n", 146 | " # 以下はCSVの項目名によって修正して下さい\n", 147 | " data['pat_id'] = str(item['pat_id'])\n", 148 | " data['pat_name'] = item['pat_name']\n", 149 | " data['summury'] = item['pat_summury']\n", 150 | " data['text'] = item['pat_text']\n", 151 | " # print(data)\n", 152 | " filename = data['pat_id'] + '.json'\n", 153 | " f = open(filename, 'w')\n", 154 | " json.dump(data, f)\n", 155 | " f.close()\n", 156 | "\n", 157 | " collection = discovery.get_collection(environment_id, collection_id)\n", 158 | " proc_docs = collection['document_counts']['processing']\n", 159 | " while True:\n", 160 | " if proc_docs < 20:\n", 161 | " break\n", 162 | " print('busy. waiting..')\n", 163 | " time.sleep(10)\n", 164 | " collection = discovery.get_collection(environment_id, collection_id)\n", 165 | " proc_docs = collection['document_counts']['processing']\n", 166 | "\n", 167 | " # print(json.dumps(collection, indent=2))\n", 168 | " with open(filename) as f:\n", 169 | " add_doc = discovery.add_document(environment_id, collection_id, file = f)\n", 170 | " os.remove(filename)\n", 171 | " # print(json.dumps(add_doc, indent=2))" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "# 検索テスト" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": null, 191 | "metadata": {}, 192 | "outputs": [], 193 | "source": [ 194 | "query = '検索テスト'\n", 195 | "filter = '検索テスト'" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": null, 201 | "metadata": {}, 202 | "outputs": [], 203 | "source": [ 204 | "my_query = discovery.query(\n", 205 | " environment_id = environment_id, \n", 206 | " collection_id = collection_id, \n", 207 | " query = query, \n", 208 | " filter = filter, \n", 209 | " return_fields = '['pat_id', 'pat_name']')\n", 210 | "print(json.dumps(my_query, indent=2))" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "# データ削除用 (MAX100件)" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "results = discovery.query(environment_id, collection_id, return_fields='id', count=100)[\"results\"]\n", 234 | "ids = [item[\"id\"] for item in results]\n", 235 | "\n", 236 | "for id in ids:\n", 237 | " print('deleting doc: id =' + id)\n", 238 | " discovery.delete_document(environment_id, collection_id, id)" 239 | ] 240 | } 241 | ], 242 | "metadata": { 243 | "kernelspec": { 244 | "display_name": "Python 3.5", 245 | "language": "python", 246 | "name": "python3" 247 | }, 248 | "language_info": { 249 | "codemirror_mode": { 250 | "name": "ipython", 251 | "version": 3 252 | }, 253 | "file_extension": ".py", 254 | "mimetype": "text/x-python", 255 | "name": "python", 256 | "nbconvert_exporter": "python", 257 | "pygments_lexer": "ipython3", 258 | "version": "3.5.4" 259 | } 260 | }, 261 | "nbformat": 4, 262 | "nbformat_minor": 1 263 | } 264 | -------------------------------------------------------------------------------- /sakura-predict-csv.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 桜開花予想" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## データの読み込み\n", 15 | "データ分析に必要なデータをCSVファイルから読み込む。" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "text/html": [ 26 | "
\n", 27 | "\n", 40 | "\n", 41 | " \n", 42 | " \n", 43 | " \n", 44 | " \n", 45 | " \n", 46 | " \n", 47 | " \n", 48 | " \n", 49 | " \n", 50 | " \n", 51 | " \n", 52 | " \n", 53 | " \n", 54 | " \n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | "
location_nameprec_idblock_idlatitudelongitudeheight
0稚内114740145.4150141.67832.8
1旭川124740743.7567142.3717120.0
2札幌144741243.0600141.328317.2
3網走174740944.0167144.278337.6
4釧路194741842.9850144.37674.5
\n", 100 | "
" 101 | ], 102 | "text/plain": [ 103 | " location_name prec_id block_id latitude longitude height\n", 104 | "0 稚内 11 47401 45.4150 141.6783 2.8\n", 105 | "1 旭川 12 47407 43.7567 142.3717 120.0\n", 106 | "2 札幌 14 47412 43.0600 141.3283 17.2\n", 107 | "3 網走 17 47409 44.0167 144.2783 37.6\n", 108 | "4 釧路 19 47418 42.9850 144.3767 4.5" 109 | ] 110 | }, 111 | "execution_count": 1, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "# セル1-1. obs_station.csvを、PandasのDataframeとして読み込む(変数名をdf_stationとする)\n", 118 | "import sys\n", 119 | "import types\n", 120 | "import pandas as pd\n", 121 | "from botocore.client import Config\n", 122 | "import ibm_boto3\n", 123 | "\n", 124 | "def __iter__(self): return 0\n", 125 | "\n", 126 | "# @hidden_cell\n", 127 | "# The following code accesses a file in your IBM Cloud Object Storage. It includes your credentials.\n", 128 | "# You might want to remove those credentials before you share your notebook.\n", 129 | "client_1539ecb44f134a9c99ef6869a2839be9 = ibm_boto3.client(service_name='s3',\n", 130 | " ibm_api_key_id='hWqSqualyGC4H1YfmJfirrY-KDDqP51f3NrvLc2vJHlH',\n", 131 | " ibm_auth_endpoint=\"https://iam.ng.bluemix.net/oidc/token\",\n", 132 | " config=Config(signature_version='oauth'),\n", 133 | " endpoint_url='https://s3-api.us-geo.objectstorage.service.networklayer.com')\n", 134 | "\n", 135 | "body = client_1539ecb44f134a9c99ef6869a2839be9.get_object(Bucket='think2018aka2-donotdelete-pr-wlhe61fd1hungw',Key='obs_station.csv')['Body']\n", 136 | "# add missing __iter__ method, so pandas accepts body as file-like object\n", 137 | "if not hasattr(body, \"__iter__\"): body.__iter__ = types.MethodType( __iter__, body )\n", 138 | "\n", 139 | "df_station = pd.read_csv(body)\n", 140 | "df_station.head()" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 2, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/html": [ 151 | "
\n", 152 | "\n", 165 | "\n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | " \n", 183 | " \n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | " \n", 238 | " \n", 239 | " \n", 240 | " \n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | "
location_namey2007y2008y2009y2010y2011y2012y2013y2014y2015y2016y2017prec_idblock_id
0旭川2007-05-062008-04-232009-05-032010-05-112011-05-092012-05-022013-05-182014-05-022015-04-272016-05-032017-05-031247407
1宇都宮2007-03-262008-03-282009-03-262010-04-022011-04-062012-04-082013-03-212014-03-292015-03-302016-03-282017-04-034147615
2横浜2007-03-232008-03-232009-03-222010-03-222011-03-302012-04-022013-03-182014-03-252015-03-232016-03-232017-03-254647670
3岡山2007-03-262008-03-262009-03-212010-03-222011-03-312012-04-032013-03-242014-03-282015-03-282016-03-262017-04-016647768
4下関2007-03-232008-03-262009-03-182010-03-202011-03-302012-03-302013-03-192014-03-252015-03-252016-03-282017-03-298147762
\n", 273 | "
" 274 | ], 275 | "text/plain": [ 276 | " location_name y2007 y2008 y2009 y2010 y2011 \\\n", 277 | "0 旭川 2007-05-06 2008-04-23 2009-05-03 2010-05-11 2011-05-09 \n", 278 | "1 宇都宮 2007-03-26 2008-03-28 2009-03-26 2010-04-02 2011-04-06 \n", 279 | "2 横浜 2007-03-23 2008-03-23 2009-03-22 2010-03-22 2011-03-30 \n", 280 | "3 岡山 2007-03-26 2008-03-26 2009-03-21 2010-03-22 2011-03-31 \n", 281 | "4 下関 2007-03-23 2008-03-26 2009-03-18 2010-03-20 2011-03-30 \n", 282 | "\n", 283 | " y2012 y2013 y2014 y2015 y2016 y2017 \\\n", 284 | "0 2012-05-02 2013-05-18 2014-05-02 2015-04-27 2016-05-03 2017-05-03 \n", 285 | "1 2012-04-08 2013-03-21 2014-03-29 2015-03-30 2016-03-28 2017-04-03 \n", 286 | "2 2012-04-02 2013-03-18 2014-03-25 2015-03-23 2016-03-23 2017-03-25 \n", 287 | "3 2012-04-03 2013-03-24 2014-03-28 2015-03-28 2016-03-26 2017-04-01 \n", 288 | "4 2012-03-30 2013-03-19 2014-03-25 2015-03-25 2016-03-28 2017-03-29 \n", 289 | "\n", 290 | " prec_id block_id \n", 291 | "0 12 47407 \n", 292 | "1 41 47615 \n", 293 | "2 46 47670 \n", 294 | "3 66 47768 \n", 295 | "4 81 47762 " 296 | ] 297 | }, 298 | "execution_count": 2, 299 | "metadata": {}, 300 | "output_type": "execute_result" 301 | } 302 | ], 303 | "source": [ 304 | "# セル1-2. cherry_blooming.csvを、PandasのDataframeとして読み込む(変数名をdf_cherry_bloomingとする)\n", 305 | "body = client_1539ecb44f134a9c99ef6869a2839be9.get_object(Bucket='think2018aka2-donotdelete-pr-wlhe61fd1hungw',Key='cherry_blooming.csv')['Body']\n", 306 | "# add missing __iter__ method, so pandas accepts body as file-like object\n", 307 | "if not hasattr(body, \"__iter__\"): body.__iter__ = types.MethodType( __iter__, body )\n", 308 | "\n", 309 | "df_cherry_blooming = pd.read_csv(body)\n", 310 | "df_cherry_blooming.head()" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 3, 316 | "metadata": { 317 | "scrolled": true 318 | }, 319 | "outputs": [ 320 | { 321 | "data": { 322 | "text/html": [ 323 | "
\n", 324 | "\n", 337 | "\n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | "
location_nameprec_idblock_iddateatmospheric_pressureprecipitationtemperature_avgtemperature_highesttemperature_lowesthumiditydaylight_hoursblooming
0旭川12474072007-01-011004.13.50.54.1-5.574.04.10
1旭川12474072007-01-021001.51.0-2.20.8-9.683.01.60
2旭川12474072007-01-031005.00.0-5.5-0.7-11.179.03.10
3旭川12474072007-01-041004.50.0-3.10.5-9.879.03.60
4旭川12474072007-01-051009.00.0-6.20.0-10.582.01.90
\n", 433 | "
" 434 | ], 435 | "text/plain": [ 436 | " location_name prec_id block_id date atmospheric_pressure \\\n", 437 | "0 旭川 12 47407 2007-01-01 1004.1 \n", 438 | "1 旭川 12 47407 2007-01-02 1001.5 \n", 439 | "2 旭川 12 47407 2007-01-03 1005.0 \n", 440 | "3 旭川 12 47407 2007-01-04 1004.5 \n", 441 | "4 旭川 12 47407 2007-01-05 1009.0 \n", 442 | "\n", 443 | " precipitation temperature_avg temperature_highest temperature_lowest \\\n", 444 | "0 3.5 0.5 4.1 -5.5 \n", 445 | "1 1.0 -2.2 0.8 -9.6 \n", 446 | "2 0.0 -5.5 -0.7 -11.1 \n", 447 | "3 0.0 -3.1 0.5 -9.8 \n", 448 | "4 0.0 -6.2 0.0 -10.5 \n", 449 | "\n", 450 | " humidity daylight_hours blooming \n", 451 | "0 74.0 4.1 0 \n", 452 | "1 83.0 1.6 0 \n", 453 | "2 79.0 3.1 0 \n", 454 | "3 79.0 3.6 0 \n", 455 | "4 82.0 1.9 0 " 456 | ] 457 | }, 458 | "execution_count": 3, 459 | "metadata": {}, 460 | "output_type": "execute_result" 461 | } 462 | ], 463 | "source": [ 464 | "# セル1-3. weather_data.csvを、PandasのDataframeとして読み込む(変数名をdf_weatherとする)\n", 465 | "body = client_1539ecb44f134a9c99ef6869a2839be9.get_object(Bucket='think2018aka2-donotdelete-pr-wlhe61fd1hungw',Key='weather_data.csv')['Body']\n", 466 | "# add missing __iter__ method, so pandas accepts body as file-like object\n", 467 | "if not hasattr(body, \"__iter__\"): body.__iter__ = types.MethodType( __iter__, body )\n", 468 | "\n", 469 | "df_weather = pd.read_csv(body)\n", 470 | "df_weather.head()" 471 | ] 472 | }, 473 | { 474 | "cell_type": "markdown", 475 | "metadata": {}, 476 | "source": [ 477 | "## データの理解\n", 478 | "分析対象のデータを可視化する。" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 4, 484 | "metadata": { 485 | "scrolled": false 486 | }, 487 | "outputs": [ 488 | { 489 | "data": { 490 | "text/html": [ 491 | "
\n", 492 | "\n", 505 | "\n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | " \n", 516 | " \n", 517 | " \n", 518 | " \n", 519 | " \n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | " \n", 527 | " \n", 528 | " \n", 529 | " \n", 530 | " \n", 531 | " \n", 532 | " \n", 533 | " \n", 534 | " \n", 535 | " \n", 536 | " \n", 537 | " \n", 538 | " \n", 539 | " \n", 540 | " \n", 541 | " \n", 542 | " \n", 543 | " \n", 544 | " \n", 545 | " \n", 546 | " \n", 547 | " \n", 548 | " \n", 549 | " \n", 550 | " \n", 551 | " \n", 552 | " \n", 553 | " \n", 554 | " \n", 555 | " \n", 556 | " \n", 557 | " \n", 558 | " \n", 559 | " \n", 560 | " \n", 561 | " \n", 562 | " \n", 563 | " \n", 564 | " \n", 565 | " \n", 566 | " \n", 567 | " \n", 568 | " \n", 569 | " \n", 570 | " \n", 571 | " \n", 572 | " \n", 573 | " \n", 574 | " \n", 575 | " \n", 576 | " \n", 577 | " \n", 578 | " \n", 579 | " \n", 580 | " \n", 581 | " \n", 582 | " \n", 583 | " \n", 584 | " \n", 585 | " \n", 586 | " \n", 587 | " \n", 588 | " \n", 589 | " \n", 590 | " \n", 591 | " \n", 592 | " \n", 593 | " \n", 594 | " \n", 595 | " \n", 596 | " \n", 597 | " \n", 598 | " \n", 599 | " \n", 600 | " \n", 601 | " \n", 602 | " \n", 603 | " \n", 604 | " \n", 605 | " \n", 606 | " \n", 607 | " \n", 608 | " \n", 609 | " \n", 610 | " \n", 611 | " \n", 612 | "
location_namey2007y2008y2009y2010y2011y2012y2013y2014y2015y2016y2017prec_idblock_id
旭川旭川2007-05-062008-04-232009-05-032010-05-112011-05-092012-05-022013-05-182014-05-022015-04-272016-05-032017-05-031247407
宇都宮宇都宮2007-03-262008-03-282009-03-262010-04-022011-04-062012-04-082013-03-212014-03-292015-03-302016-03-282017-04-034147615
横浜横浜2007-03-232008-03-232009-03-222010-03-222011-03-302012-04-022013-03-182014-03-252015-03-232016-03-232017-03-254647670
岡山岡山2007-03-262008-03-262009-03-212010-03-222011-03-312012-04-032013-03-242014-03-282015-03-282016-03-262017-04-016647768
下関下関2007-03-232008-03-262009-03-182010-03-202011-03-302012-03-302013-03-192014-03-252015-03-252016-03-282017-03-298147762
\n", 613 | "
" 614 | ], 615 | "text/plain": [ 616 | " location_name y2007 y2008 y2009 y2010 y2011 \\\n", 617 | "旭川 旭川 2007-05-06 2008-04-23 2009-05-03 2010-05-11 2011-05-09 \n", 618 | "宇都宮 宇都宮 2007-03-26 2008-03-28 2009-03-26 2010-04-02 2011-04-06 \n", 619 | "横浜 横浜 2007-03-23 2008-03-23 2009-03-22 2010-03-22 2011-03-30 \n", 620 | "岡山 岡山 2007-03-26 2008-03-26 2009-03-21 2010-03-22 2011-03-31 \n", 621 | "下関 下関 2007-03-23 2008-03-26 2009-03-18 2010-03-20 2011-03-30 \n", 622 | "\n", 623 | " y2012 y2013 y2014 y2015 y2016 y2017 \\\n", 624 | "旭川 2012-05-02 2013-05-18 2014-05-02 2015-04-27 2016-05-03 2017-05-03 \n", 625 | "宇都宮 2012-04-08 2013-03-21 2014-03-29 2015-03-30 2016-03-28 2017-04-03 \n", 626 | "横浜 2012-04-02 2013-03-18 2014-03-25 2015-03-23 2016-03-23 2017-03-25 \n", 627 | "岡山 2012-04-03 2013-03-24 2014-03-28 2015-03-28 2016-03-26 2017-04-01 \n", 628 | "下関 2012-03-30 2013-03-19 2014-03-25 2015-03-25 2016-03-28 2017-03-29 \n", 629 | "\n", 630 | " prec_id block_id \n", 631 | "旭川 12 47407 \n", 632 | "宇都宮 41 47615 \n", 633 | "横浜 46 47670 \n", 634 | "岡山 66 47768 \n", 635 | "下関 81 47762 " 636 | ] 637 | }, 638 | "execution_count": 4, 639 | "metadata": {}, 640 | "output_type": "execute_result" 641 | } 642 | ], 643 | "source": [ 644 | "# セル2-1. DataFrameのIndexの変更\n", 645 | "\n", 646 | "pd.set_option(\"display.max_rows\", 500)\n", 647 | "\n", 648 | "# df_weatherのIndexを日付に変更する\n", 649 | "df_weather.index = pd.to_datetime(df_weather['date'].tolist())\n", 650 | "df_weather['year'] = df_weather.index.year\n", 651 | "\n", 652 | "# df_stationとdf_cherry_bloomingのIndexを観測所名に変更する\n", 653 | "df_station.index = df_station['location_name'].tolist()\n", 654 | "\n", 655 | "df_cherry_blooming.index = df_cherry_blooming['location_name'].tolist()\n", 656 | "df_cherry_blooming.head()" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 5, 662 | "metadata": {}, 663 | "outputs": [ 664 | { 665 | "data": { 666 | "text/html": [ 667 | "\n", 682 | "\n", 683 | "\n", 684 | "\n", 685 | "\n", 686 | "\n", 687 | "\n", 690 | "\n", 691 | "
\n", 692 | "" 693 | ], 694 | "text/plain": [ 695 | "" 696 | ] 697 | }, 698 | "metadata": {}, 699 | "output_type": "display_data" 700 | }, 701 | { 702 | "data": { 703 | "application/javascript": [ 704 | "/*\n", 705 | " * Copyright (c) 2015 IBM Corporation and others.\n", 706 | " *\n", 707 | " * Licensed under the Apache License, Version 2.0 (the \"License\");\n", 708 | " * You may not use this file except in compliance with the License.\n", 709 | " * You may obtain a copy of the License at\n", 710 | " *\n", 711 | " * http://www.apache.org/licenses/LICENSE-2.0\n", 712 | " *\n", 713 | " * Unless required by applicable law or agreed to in writing, software\n", 714 | " * distributed under the License is distributed on an \"AS IS\" BASIS,\n", 715 | " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", 716 | " * See the License for the specific language governing permissions and\n", 717 | " * limitations under the License.\n", 718 | " */\n", 719 | "\n", 720 | "require.config({\n", 721 | " waitSeconds: 60,\n", 722 | " paths: {\n", 723 | " 'd3': '//cdnjs.cloudflare.com/ajax/libs/d3/4.2.1/d3.min',\n", 724 | " 'topojson': '//cdnjs.cloudflare.com/ajax/libs/topojson/1.6.20/topojson.min',\n", 725 | " 'brunel' : '/data/jupyter2/static-file-content-delivery-network/nbextensions/brunel_ext/brunel.2.3.min',\n", 726 | " 'brunelControls' : '/data/jupyter2/static-file-content-delivery-network/nbextensions/brunel_ext/brunel.controls.2.3.min'\n", 727 | " },\n", 728 | " shim: {\n", 729 | " 'brunel' : {\n", 730 | " exports: 'BrunelD3',\n", 731 | " deps: ['d3', 'topojson'],\n", 732 | " init: function() {\n", 733 | " return {\n", 734 | " BrunelD3 : BrunelD3,\n", 735 | " BrunelData : BrunelData\n", 736 | " }\n", 737 | " }\n", 738 | " },\n", 739 | " 'brunelControls' : {\n", 740 | " exports: 'BrunelEventHandlers',\n", 741 | " init: function() {\n", 742 | " return {\n", 743 | " BrunelEventHandlers: BrunelEventHandlers,\n", 744 | " BrunelJQueryControlFactory: BrunelJQueryControlFactory\n", 745 | " }\n", 746 | " }\n", 747 | " }\n", 748 | "\n", 749 | " }\n", 750 | "\n", 751 | "});\n", 752 | "\n", 753 | "require([\"d3\"], function(d3) {\n", 754 | " require([\"brunel\", \"brunelControls\"], function(brunel, brunelControls) {\n", 755 | " function BrunelVis(visId) {\n", 756 | " \"use strict\"; // strict mode\n", 757 | " var datasets = [], // array of datasets for the original data\n", 758 | " pre = function(d, i) { return d }, // default pre-process does nothing\n", 759 | " post = function(d, i) { return d }, // default post-process does nothing\n", 760 | " transitionTime = 200, // transition time for animations\n", 761 | " charts = [], // the charts in the system\n", 762 | " vis = d3.select('#' + visId).attr('class', 'brunel'); // the SVG container\n", 763 | "\n", 764 | " BrunelD3.addDefinitions(vis); // ensure standard symbols present\n", 765 | "\n", 766 | " // Define chart #1 in the visualization //////////////////////////////////////////////////////////\n", 767 | "\n", 768 | " charts[0] = function(parentNode, filterRows) {\n", 769 | " var geom = BrunelD3.geometry(parentNode || vis.node(), 0, 0, 1, 1, 5, 43, 37, 62),\n", 770 | " elements = []; // array of elements in this chart\n", 771 | "\n", 772 | " // Define groups for the chart parts ///////////////////////////////////////////////////////////\n", 773 | "\n", 774 | " var chart = vis.append('g').attr('class', 'chart1')\n", 775 | " .attr('transform','translate(' + geom.chart_left + ',' + geom.chart_top + ')');\n", 776 | " var overlay = chart.append('g').attr('class', 'element').attr('class', 'overlay');\n", 777 | " var zoom = d3.zoom().scaleExtent([1/3,3]);\n", 778 | " var zoomNode = overlay.append('rect').attr('class', 'overlay')\n", 779 | " .attr('x', geom.inner_left).attr('y', geom.inner_top)\n", 780 | " .attr('width', geom.inner_rawWidth).attr('height', geom.inner_rawHeight)\n", 781 | " .style('cursor', 'move').call(zoom)\n", 782 | " .node();\n", 783 | " zoomNode.__zoom = d3.zoomIdentity;\n", 784 | " chart.append('rect').attr('class', 'background').attr('width', geom.chart_right-geom.chart_left).attr('height', geom.chart_bottom-geom.chart_top);\n", 785 | " var interior = chart.append('g').attr('class', 'interior zoomNone')\n", 786 | " .attr('transform','translate(' + geom.inner_left + ',' + geom.inner_top + ')')\n", 787 | " .attr('clip-path', 'url(#clip_visid8fca92a2-6981-11e8-ae66-3a5f3782500e_chart1_inner)');\n", 788 | " interior.append('rect').attr('class', 'inner').attr('width', geom.inner_width).attr('height', geom.inner_height);\n", 789 | " var gridGroup = interior.append('g').attr('class', 'grid');\n", 790 | " var axes = chart.append('g').attr('class', 'axis')\n", 791 | " .attr('transform','translate(' + geom.inner_left + ',' + geom.inner_top + ')');\n", 792 | " var legends = chart.append('g').attr('class', 'legend')\n", 793 | " .attr('transform','translate(' + (geom.chart_right-geom.chart_left - 3) + ',' + 0 + ')');\n", 794 | " vis.append('clipPath').attr('id', 'clip_visid8fca92a2-6981-11e8-ae66-3a5f3782500e_chart1_inner').append('rect')\n", 795 | " .attr('x', 0).attr('y', 0)\n", 796 | " .attr('width', geom.inner_rawWidth+1).attr('height', geom.inner_rawHeight+1);\n", 797 | "\n", 798 | " // Scales //////////////////////////////////////////////////////////////////////////////////////\n", 799 | "\n", 800 | " var scale_x = d3.scaleTime().domain([new Date('2014-10-01'), new Date('2017-10-01')])\n", 801 | " .range([0, geom.inner_width]);\n", 802 | " var scale_inner = d3.scaleLinear().domain([0,1])\n", 803 | " .range([-0.5, 0.5]);\n", 804 | " var scale_y = d3.scaleLinear().domain([0, 35.000003])\n", 805 | " .range([geom.inner_height, 0]);\n", 806 | " var base_scales = [scale_x, scale_y]; // untransformed original scales\n", 807 | "\n", 808 | " // Axes ////////////////////////////////////////////////////////////////////////////////////////\n", 809 | "\n", 810 | " axes.append('g').attr('class', 'x axis')\n", 811 | " .attr('transform','translate(0,' + geom.inner_rawHeight + ')')\n", 812 | " .attr('clip-path', 'url(#clip_visid8fca92a2-6981-11e8-ae66-3a5f3782500e_chart1_haxis)');\n", 813 | " vis.append('clipPath').attr('id', 'clip_visid8fca92a2-6981-11e8-ae66-3a5f3782500e_chart1_haxis').append('polyline')\n", 814 | " .attr('points', '-1,-1000, -1,-1 -5,5, -1000,5, -100,1000, 10000,1000 10000,-1000');\n", 815 | " axes.select('g.axis.x').append('text').attr('class', 'title').text('Date').style('text-anchor', 'middle')\n", 816 | " .attr('x',geom.inner_rawWidth/2)\n", 817 | " .attr('y', geom.inner_bottom - 2.0).attr('dy','-0.27em');\n", 818 | " axes.append('g').attr('class', 'y axis')\n", 819 | " .attr('clip-path', 'url(#clip_visid8fca92a2-6981-11e8-ae66-3a5f3782500e_chart1_vaxis)');\n", 820 | " vis.append('clipPath').attr('id', 'clip_visid8fca92a2-6981-11e8-ae66-3a5f3782500e_chart1_vaxis').append('polyline')\n", 821 | " .attr('points', '-1000,-10000, 10000,-10000, 10000,' + (geom.inner_rawHeight+1) + ', -1,' + (geom.inner_rawHeight+1) + ', -1,' + (geom.inner_rawHeight+5) + ', -1000,' + (geom.inner_rawHeight+5) );\n", 822 | " axes.select('g.axis.y').append('text').attr('class', 'title').text('Temperature Highest').style('text-anchor', 'middle')\n", 823 | " .attr('x',-geom.inner_rawHeight/2)\n", 824 | " .attr('y', 4-geom.inner_left).attr('dy', '0.7em').attr('transform', 'rotate(270)');\n", 825 | "\n", 826 | " var axis_bottom = d3.axisBottom(scale_x).ticks(Math.min(10, Math.round(geom.inner_width / 99.0)));\n", 827 | " var axis_left = d3.axisLeft(scale_y).ticks(Math.min(10, Math.round(geom.inner_width / 20)));\n", 828 | "\n", 829 | " function buildAxes(time) {\n", 830 | " var axis_x = axes.select('g.axis.x');\n", 831 | " BrunelD3.transition(axis_x, time).call(axis_bottom.scale(scale_x));\n", 832 | " var axis_y = axes.select('g.axis.y');\n", 833 | " BrunelD3.transition(axis_y, time).call(axis_left.scale(scale_y));\n", 834 | " }\n", 835 | " zoom.on('zoom', function(t, time) {\n", 836 | " t = t ||BrunelD3.restrictZoom(d3.event.transform, geom, this);\n", 837 | " scale_x = t.rescaleX(base_scales[0]);\n", 838 | " scale_y = t.rescaleY(base_scales[1]);\n", 839 | " zoomNode.__zoom = t;\n", 840 | " interior.attr('class', 'interior ' + BrunelD3.zoomLabel(t.k));;\n", 841 | " build(time || -1);\n", 842 | " });\n", 843 | "\n", 844 | " // Define element #1 ///////////////////////////////////////////////////////////////////////////\n", 845 | "\n", 846 | " elements[0] = function() {\n", 847 | " var original, processed, // data sets passed in and then transformed\n", 848 | " element, data, // brunel element information and brunel data\n", 849 | " selection, merged; // d3 selection and merged selection\n", 850 | " var elementGroup = interior.append('g').attr('class', 'element1'),\n", 851 | " main = elementGroup.append('g').attr('class', 'main'),\n", 852 | " labels = BrunelD3.undoTransform(elementGroup.append('g').attr('class', 'labels').attr('aria-hidden', 'true'), elementGroup);\n", 853 | "\n", 854 | " function makeData() {\n", 855 | " original = datasets[0];\n", 856 | " if (filterRows) original = original.retainRows(filterRows);\n", 857 | " processed = pre(original, 0)\n", 858 | " .filter('temperature_highest valid');\n", 859 | " processed = post(processed, 0);\n", 860 | " var f0 = processed.field('date'),\n", 861 | " f1 = processed.field('temperature_highest'),\n", 862 | " f2 = processed.field('blooming'),\n", 863 | " f3 = processed.field('location_name'),\n", 864 | " f4 = processed.field('precipitation'),\n", 865 | " f5 = processed.field('#row'),\n", 866 | " f6 = processed.field('#selection');\n", 867 | " var keyFunc = function(d) { return f0.value(d) };\n", 868 | " data = {\n", 869 | " date: function(d) { return f0.value(d.row) },\n", 870 | " temperature_highest:function(d) { return f1.value(d.row) },\n", 871 | " blooming: function(d) { return f2.value(d.row) },\n", 872 | " location_name:function(d) { return f3.value(d.row) },\n", 873 | " precipitation:function(d) { return f4.value(d.row) },\n", 874 | " $row: function(d) { return f5.value(d.row) },\n", 875 | " $selection: function(d) { return f6.value(d.row) },\n", 876 | " date_f: function(d) { return f0.valueFormatted(d.row) },\n", 877 | " temperature_highest_f:function(d) { return f1.valueFormatted(d.row) },\n", 878 | " blooming_f: function(d) { return f2.valueFormatted(d.row) },\n", 879 | " location_name_f:function(d) { return f3.valueFormatted(d.row) },\n", 880 | " precipitation_f:function(d) { return f4.valueFormatted(d.row) },\n", 881 | " $row_f: function(d) { return f5.valueFormatted(d.row) },\n", 882 | " $selection_f: function(d) { return f6.valueFormatted(d.row) },\n", 883 | " _split: function(d) { return f2.value(d.row) },\n", 884 | " _key: keyFunc,\n", 885 | " _rows: BrunelD3.makeRowsWithKeys(keyFunc, processed.rowCount())\n", 886 | " };\n", 887 | " }\n", 888 | " // Aesthetic Functions\n", 889 | " var scale_color = d3.scaleLinear().domain([0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1])\n", 890 | " .interpolate(d3.interpolateHcl)\n", 891 | " .range([ '#045a8d', '#2b8cbe', '#74a9cf', '#bdc9e1', '#f8efe8', '#fef0d9', \n", 892 | " '#fdcc8a', '#fc8d59', '#e34a33']);\n", 893 | " var color = function(d) { return scale_color(data.blooming(d)) };\n", 894 | " legends._legend = legends._legend || { title: ['Blooming'], \n", 895 | " ticks: [1, 0.8, 0.6, 0.4, 0.2, 0]};\n", 896 | " legends._legend.color = scale_color;\n", 897 | "\n", 898 | " // Build element from data ///////////////////////////////////////////////////////////////////\n", 899 | "\n", 900 | " function build(transitionMillis) {\n", 901 | " element = elements[0];\n", 902 | " var w = geom.default_point_size;\n", 903 | " var x = function(d) { return scale_x(data.date(d))};\n", 904 | " var h = geom.default_point_size;\n", 905 | " var y = function(d) { return scale_y(data.temperature_highest(d))};\n", 906 | "\n", 907 | " // Define selection entry operations\n", 908 | " function initialState(selection) {\n", 909 | " selection\n", 910 | " .attr('class', 'element point filled')\n", 911 | " }\n", 912 | "\n", 913 | " // Define selection update operations on merged data\n", 914 | " function updateState(selection) {\n", 915 | " selection\n", 916 | " .attr('cx',function(d) { return scale_x(data.date(d))})\n", 917 | " .attr('cy',function(d) { return scale_y(data.temperature_highest(d))})\n", 918 | " .attr('r',geom.default_point_size / 2)\n", 919 | " .filter(BrunelD3.hasData) // following only performed for data items\n", 920 | " .style('fill', color);\n", 921 | " }\n", 922 | "\n", 923 | " // Define labeling for the selection\n", 924 | " function label(selection, transitionMillis) {\n", 925 | "\n", 926 | " var tooltipLabeling = {\n", 927 | " index: -1, method: 'box', location: ['center', 'top'], inside: true, align: 'middle', pad: 0, dy: 0.7,\n", 928 | " fit: true, granularity: 0,\n", 929 | " content: function(d) {\n", 930 | " return d.row == null ? null : 'Location Name: '\n", 931 | "\t\t\t+ '' + data.location_name_f(d) + ''\n", 932 | "\t\t\t+ '
'\n", 933 | "\t\t\t+ 'Date: '\n", 934 | "\t\t\t+ '' + data.date_f(d) + ''\n", 935 | "\t\t\t+ '
'\n", 936 | "\t\t\t+ 'Precipitation: '\n", 937 | "\t\t\t+ '' + data.precipitation_f(d) + ''\n", 938 | " }\n", 939 | " };\n", 940 | " BrunelD3.addTooltip(selection, tooltipLabeling, geom);\n", 941 | " }\n", 942 | " // Create selections, set the initial state and transition updates\n", 943 | " selection = main.selectAll('.element').data(data._rows, function(d) { return d.key });\n", 944 | " var added = selection.enter().append('circle');\n", 945 | " merged = selection.merge(added);\n", 946 | " initialState(added);\n", 947 | " selection.filter(BrunelD3.hasData)\n", 948 | " .classed('selected', BrunelD3.isSelected(data))\n", 949 | " .filter(BrunelD3.isSelected(data)).raise();\n", 950 | " updateState(BrunelD3.transition(merged, transitionMillis));\n", 951 | " label(merged, transitionMillis);\n", 952 | "\n", 953 | " BrunelD3.transition(selection.exit(), transitionMillis/3)\n", 954 | " .style('opacity', 0.5).each( function() {\n", 955 | " this.remove(); BrunelD3.removeLabels(this); \n", 956 | " });\n", 957 | " }\n", 958 | "\n", 959 | " return {\n", 960 | " data: function() { return processed },\n", 961 | " original: function() { return original },\n", 962 | " internal: function() { return data },\n", 963 | " selection: function() { return merged },\n", 964 | " makeData: makeData,\n", 965 | " build: build,\n", 966 | " chart: function() { return charts[0] },\n", 967 | " group: function() { return elementGroup },\n", 968 | " fields: {\n", 969 | " x: ['date'],\n", 970 | " y: ['temperature_highest'],\n", 971 | " key: ['date'],\n", 972 | " color: ['blooming']\n", 973 | " }\n", 974 | " };\n", 975 | " }();\n", 976 | "\n", 977 | " function build(time, noData) {\n", 978 | " var first = elements[0].data() == null;\n", 979 | " if (first) time = 0; // no transition for first call\n", 980 | " buildAxes(time);\n", 981 | " if ((first || time > -1) && !noData) {\n", 982 | " elements[0].makeData();\n", 983 | " BrunelD3.addLegend(legends, legends._legend);\n", 984 | " }\n", 985 | " elements[0].build(time);\n", 986 | " }\n", 987 | "\n", 988 | " // Expose the following components of the chart\n", 989 | " return {\n", 990 | " elements : elements,\n", 991 | " interior : interior,\n", 992 | " scales: {x:scale_x, y:scale_y},\n", 993 | " zoom: function(params, time) {\n", 994 | " if (params) zoom.on('zoom').call(zoomNode, params, time);\n", 995 | " return d3.zoomTransform(zoomNode);\n", 996 | " },\n", 997 | " build : build\n", 998 | " };\n", 999 | " }();\n", 1000 | "\n", 1001 | " function setData(rowData, i) { datasets[i||0] = BrunelD3.makeData(rowData) }\n", 1002 | " function updateAll(time) { charts.forEach(function(x) {x.build(time || 0)}) }\n", 1003 | " function buildAll() {\n", 1004 | " for (var i=0;i" 1308 | ] 1309 | }, 1310 | "execution_count": 5, 1311 | "metadata": {}, 1312 | "output_type": "execute_result" 1313 | } 1314 | ], 1315 | "source": [ 1316 | "# セル2-2. Brunelによるデータの可視化\n", 1317 | "\n", 1318 | "# グラフ作成用のライブラリーBrunelをインポート\n", 1319 | "import brunel \n", 1320 | "\n", 1321 | "# 東京の2015年以降のデータだけを抽出する\n", 1322 | "df_tokyo = df_weather[(df_weather.location_name == '東京') & (df_weather.year >= 2015)]\n", 1323 | "\n", 1324 | "# 横軸に日付、縦軸に最高気温を用いて、散布図を作成する\n", 1325 | "%brunel data('df_tokyo') x(date) y(temperature_highest) color(blooming) tooltip(location_name,date,precipitation) ::width=1200" 1326 | ] 1327 | }, 1328 | { 1329 | "cell_type": "code", 1330 | "execution_count": 6, 1331 | "metadata": {}, 1332 | "outputs": [ 1333 | { 1334 | "data": { 1335 | "text/html": [ 1336 | "
\n", 1337 | "\n", 1350 | "\n", 1351 | " \n", 1352 | " \n", 1353 | " \n", 1354 | " \n", 1355 | " \n", 1356 | " \n", 1357 | " \n", 1358 | " \n", 1359 | " \n", 1360 | " \n", 1361 | " \n", 1362 | " \n", 1363 | " \n", 1364 | " \n", 1365 | " \n", 1366 | " \n", 1367 | " \n", 1368 | " \n", 1369 | " \n", 1370 | " \n", 1371 | " \n", 1372 | " \n", 1373 | " \n", 1374 | " \n", 1375 | " \n", 1376 | " \n", 1377 | " \n", 1378 | " \n", 1379 | " \n", 1380 | " \n", 1381 | " \n", 1382 | " \n", 1383 | " \n", 1384 | " \n", 1385 | " \n", 1386 | " \n", 1387 | " \n", 1388 | " \n", 1389 | " \n", 1390 | " \n", 1391 | " \n", 1392 | " \n", 1393 | " \n", 1394 | " \n", 1395 | " \n", 1396 | " \n", 1397 | " \n", 1398 | " \n", 1399 | " \n", 1400 | " \n", 1401 | " \n", 1402 | " \n", 1403 | " \n", 1404 | " \n", 1405 | " \n", 1406 | " \n", 1407 | " \n", 1408 | " \n", 1409 | " \n", 1410 | " \n", 1411 | " \n", 1412 | " \n", 1413 | " \n", 1414 | " \n", 1415 | " \n", 1416 | " \n", 1417 | " \n", 1418 | " \n", 1419 | " \n", 1420 | " \n", 1421 | " \n", 1422 | " \n", 1423 | " \n", 1424 | " \n", 1425 | " \n", 1426 | " \n", 1427 | " \n", 1428 | " \n", 1429 | " \n", 1430 | " \n", 1431 | " \n", 1432 | " \n", 1433 | " \n", 1434 | " \n", 1435 | " \n", 1436 | " \n", 1437 | " \n", 1438 | " \n", 1439 | " \n", 1440 | " \n", 1441 | " \n", 1442 | " \n", 1443 | " \n", 1444 | " \n", 1445 | " \n", 1446 | " \n", 1447 | " \n", 1448 | " \n", 1449 | " \n", 1450 | " \n", 1451 | " \n", 1452 | " \n", 1453 | " \n", 1454 | " \n", 1455 | " \n", 1456 | " \n", 1457 | " \n", 1458 | " \n", 1459 | " \n", 1460 | " \n", 1461 | " \n", 1462 | " \n", 1463 | " \n", 1464 | " \n", 1465 | " \n", 1466 | " \n", 1467 | " \n", 1468 | " \n", 1469 | " \n", 1470 | " \n", 1471 | " \n", 1472 | " \n", 1473 | " \n", 1474 | " \n", 1475 | "
location_nameprec_idblock_idlatitudelongitudeheighty2007y2008y2009y2010y2011y2012y2013y2014y2015y2016y2017
下関下関814776233.9483130.92503.32007-03-232008-03-262009-03-182010-03-202011-03-302012-03-302013-03-192014-03-252015-03-252016-03-282017-03-29
京都京都614775935.0150135.731741.42007-03-252008-03-242009-03-192010-03-192011-03-282012-04-032013-03-222014-03-272015-03-272016-03-232017-03-31
仙台仙台344759038.2617140.896738.92007-04-062008-04-052009-04-072010-04-132011-04-122012-04-182013-04-092014-04-072015-04-032016-04-012017-04-07
佐賀佐賀854781333.2650130.30505.52007-03-242008-03-242009-03-192010-03-212011-03-222012-03-282013-03-182014-03-192015-03-222016-03-232017-03-30
函館函館234743041.8167140.753335.02007-04-302008-04-222009-04-252010-05-052011-05-022012-05-022013-05-082014-04-282015-04-212016-04-242017-04-27
\n", 1476 | "
" 1477 | ], 1478 | "text/plain": [ 1479 | " location_name prec_id block_id latitude longitude height y2007 \\\n", 1480 | "下関 下関 81 47762 33.9483 130.9250 3.3 2007-03-23 \n", 1481 | "京都 京都 61 47759 35.0150 135.7317 41.4 2007-03-25 \n", 1482 | "仙台 仙台 34 47590 38.2617 140.8967 38.9 2007-04-06 \n", 1483 | "佐賀 佐賀 85 47813 33.2650 130.3050 5.5 2007-03-24 \n", 1484 | "函館 函館 23 47430 41.8167 140.7533 35.0 2007-04-30 \n", 1485 | "\n", 1486 | " y2008 y2009 y2010 y2011 y2012 y2013 \\\n", 1487 | "下関 2008-03-26 2009-03-18 2010-03-20 2011-03-30 2012-03-30 2013-03-19 \n", 1488 | "京都 2008-03-24 2009-03-19 2010-03-19 2011-03-28 2012-04-03 2013-03-22 \n", 1489 | "仙台 2008-04-05 2009-04-07 2010-04-13 2011-04-12 2012-04-18 2013-04-09 \n", 1490 | "佐賀 2008-03-24 2009-03-19 2010-03-21 2011-03-22 2012-03-28 2013-03-18 \n", 1491 | "函館 2008-04-22 2009-04-25 2010-05-05 2011-05-02 2012-05-02 2013-05-08 \n", 1492 | "\n", 1493 | " y2014 y2015 y2016 y2017 \n", 1494 | "下関 2014-03-25 2015-03-25 2016-03-28 2017-03-29 \n", 1495 | "京都 2014-03-27 2015-03-27 2016-03-23 2017-03-31 \n", 1496 | "仙台 2014-04-07 2015-04-03 2016-04-01 2017-04-07 \n", 1497 | "佐賀 2014-03-19 2015-03-22 2016-03-23 2017-03-30 \n", 1498 | "函館 2014-04-28 2015-04-21 2016-04-24 2017-04-27 " 1499 | ] 1500 | }, 1501 | "execution_count": 6, 1502 | "metadata": {}, 1503 | "output_type": "execute_result" 1504 | } 1505 | ], 1506 | "source": [ 1507 | "# セル2-3. データの結合\n", 1508 | "\n", 1509 | "#桜の開花日データを可視化するために、df_stationとdf_cherry_bloomingを結合する\n", 1510 | "df_cherry_blooming = pd.concat([df_station, df_cherry_blooming[['y2007', 'y2008', 'y2009', 'y2010', 'y2011', 'y2012', 'y2013', 'y2014', 'y2015', 'y2016', 'y2017']]], axis=1)\n", 1511 | "df_cherry_blooming.head()" 1512 | ] 1513 | }, 1514 | { 1515 | "cell_type": "code", 1516 | "execution_count": 7, 1517 | "metadata": {}, 1518 | "outputs": [ 1519 | { 1520 | "data": { 1521 | "text/html": [ 1522 | "\n", 1537 | "\n", 1538 | "\n", 1539 | "\n", 1540 | "\n", 1541 | "\n", 1542 | "\n", 1545 | "\n", 1546 | "
\n", 1547 | "" 1548 | ], 1549 | "text/plain": [ 1550 | "" 1551 | ] 1552 | }, 1553 | "metadata": {}, 1554 | "output_type": "display_data" 1555 | }, 1556 | { 1557 | "data": { 1558 | "application/javascript": [ 1559 | "/*\n", 1560 | " * Copyright (c) 2015 IBM Corporation and others.\n", 1561 | " *\n", 1562 | " * Licensed under the Apache License, Version 2.0 (the \"License\");\n", 1563 | " * You may not use this file except in compliance with the License.\n", 1564 | " * You may obtain a copy of the License at\n", 1565 | " *\n", 1566 | " * http://www.apache.org/licenses/LICENSE-2.0\n", 1567 | " *\n", 1568 | " * Unless required by applicable law or agreed to in writing, software\n", 1569 | " * distributed under the License is distributed on an \"AS IS\" BASIS,\n", 1570 | " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", 1571 | " * See the License for the specific language governing permissions and\n", 1572 | " * limitations under the License.\n", 1573 | " */\n", 1574 | "\n", 1575 | "require.config({\n", 1576 | " waitSeconds: 60,\n", 1577 | " paths: {\n", 1578 | " 'd3': '//cdnjs.cloudflare.com/ajax/libs/d3/4.2.1/d3.min',\n", 1579 | " 'topojson': '//cdnjs.cloudflare.com/ajax/libs/topojson/1.6.20/topojson.min',\n", 1580 | " 'brunel' : '/data/jupyter2/static-file-content-delivery-network/nbextensions/brunel_ext/brunel.2.3.min',\n", 1581 | " 'brunelControls' : '/data/jupyter2/static-file-content-delivery-network/nbextensions/brunel_ext/brunel.controls.2.3.min'\n", 1582 | " },\n", 1583 | " shim: {\n", 1584 | " 'brunel' : {\n", 1585 | " exports: 'BrunelD3',\n", 1586 | " deps: ['d3', 'topojson'],\n", 1587 | " init: function() {\n", 1588 | " return {\n", 1589 | " BrunelD3 : BrunelD3,\n", 1590 | " BrunelData : BrunelData\n", 1591 | " }\n", 1592 | " }\n", 1593 | " },\n", 1594 | " 'brunelControls' : {\n", 1595 | " exports: 'BrunelEventHandlers',\n", 1596 | " init: function() {\n", 1597 | " return {\n", 1598 | " BrunelEventHandlers: BrunelEventHandlers,\n", 1599 | " BrunelJQueryControlFactory: BrunelJQueryControlFactory\n", 1600 | " }\n", 1601 | " }\n", 1602 | " }\n", 1603 | "\n", 1604 | " }\n", 1605 | "\n", 1606 | "});\n", 1607 | "\n", 1608 | "require([\"d3\"], function(d3) {\n", 1609 | " require([\"brunel\", \"brunelControls\"], function(brunel, brunelControls) {\n", 1610 | " function BrunelVis(visId) {\n", 1611 | " \"use strict\"; // strict mode\n", 1612 | " var datasets = [], // array of datasets for the original data\n", 1613 | " pre = function(d, i) { return d }, // default pre-process does nothing\n", 1614 | " post = function(d, i) { return d }, // default post-process does nothing\n", 1615 | " transitionTime = 200, // transition time for animations\n", 1616 | " charts = [], // the charts in the system\n", 1617 | " vis = d3.select('#' + visId).attr('class', 'brunel'); // the SVG container\n", 1618 | "\n", 1619 | " BrunelD3.addDefinitions(vis); // ensure standard symbols present\n", 1620 | "\n", 1621 | " // Define chart #1 in the visualization //////////////////////////////////////////////////////////\n", 1622 | "\n", 1623 | " charts[0] = function(parentNode, filterRows) {\n", 1624 | " var geom = BrunelD3.geometry(parentNode || vis.node(), 0, 0, 1, 1, 0, 0, 0, 110),\n", 1625 | " elements = []; // array of elements in this chart\n", 1626 | "\n", 1627 | " // Define groups for the chart parts ///////////////////////////////////////////////////////////\n", 1628 | "\n", 1629 | " var chart = vis.append('g').attr('class', 'chart1')\n", 1630 | " .attr('transform','translate(' + geom.chart_left + ',' + geom.chart_top + ')');\n", 1631 | " var overlay = chart.append('g').attr('class', 'element').attr('class', 'overlay');\n", 1632 | " var zoom = d3.zoom().scaleExtent([1/5,5]);\n", 1633 | " var zoomNode = overlay.append('rect').attr('class', 'overlay')\n", 1634 | " .attr('x', geom.inner_left).attr('y', geom.inner_top)\n", 1635 | " .attr('width', geom.inner_rawWidth).attr('height', geom.inner_rawHeight)\n", 1636 | " .style('cursor', 'move').call(zoom)\n", 1637 | " .node();\n", 1638 | " zoomNode.__zoom = d3.zoomIdentity;\n", 1639 | " chart.append('rect').attr('class', 'background').attr('width', geom.chart_right-geom.chart_left).attr('height', geom.chart_bottom-geom.chart_top);\n", 1640 | " var interior = chart.append('g').attr('class', 'interior zoomNone')\n", 1641 | " .attr('transform','translate(' + geom.inner_left + ',' + geom.inner_top + ')')\n", 1642 | " .attr('clip-path', 'url(#clip_visid95cf16fa-6981-11e8-ae66-3a5f3782500e_chart1_inner)');\n", 1643 | " interior.append('rect').attr('class', 'inner').attr('width', geom.inner_width).attr('height', geom.inner_height);\n", 1644 | " var gridGroup = interior.append('g').attr('class', 'grid');\n", 1645 | " var legends = chart.append('g').attr('class', 'legend')\n", 1646 | " .attr('transform','translate(' + (geom.chart_right-geom.chart_left - 3) + ',' + 0 + ')');\n", 1647 | " vis.append('clipPath').attr('id', 'clip_visid95cf16fa-6981-11e8-ae66-3a5f3782500e_chart1_inner').append('rect')\n", 1648 | " .attr('x', 0).attr('y', 0)\n", 1649 | " .attr('width', geom.inner_rawWidth+1).attr('height', geom.inner_rawHeight+1);\n", 1650 | "\n", 1651 | " // Projection //////////////////////////////////////////////////////////////////////////////////\n", 1652 | "\n", 1653 | " var base = d3.geoMercator()\n", 1654 | " .translate([geom.inner_width/2, geom.inner_height/2])\n", 1655 | " .scale(Math.min((geom.inner_width-4)/0.3039, (geom.inner_height-4)/0.3739))\n", 1656 | " .center([137.1217, 38.9699])\n", 1657 | " function projection(p) {\n", 1658 | " var q = base(p), t = d3.zoomTransform(zoomNode);\n", 1659 | " return q ? [t.k*q[0]+t.x, t.k*q[1]+t.y] : null;\n", 1660 | " };\n", 1661 | " function project_center(v) { return (v ? projection([v.c, v.d]) : null) || [-9e6, -9e6] };\n", 1662 | " var path = d3.geoPath().projection(BrunelD3.geoStream(projection));\n", 1663 | " var scale_x = d3.scaleLinear(), scale_y = d3.scaleLinear();\n", 1664 | " var base_scales = [scale_x, scale_y]; // untransformed original scales\n", 1665 | " zoom.on('zoom', function(t, time) {\n", 1666 | " t = t || d3.event.transform;\n", 1667 | " zoomNode.__zoom = t;\n", 1668 | " interior.attr('class', 'interior ' + BrunelD3.zoomLabel(t.k));;\n", 1669 | " build(time || -1);\n", 1670 | " });\n", 1671 | "\n", 1672 | " // Define element #1 ///////////////////////////////////////////////////////////////////////////\n", 1673 | "\n", 1674 | " elements[0] = function() {\n", 1675 | " var original, processed, // data sets passed in and then transformed\n", 1676 | " element, data, // brunel element information and brunel data\n", 1677 | " selection, merged; // d3 selection and merged selection\n", 1678 | " var elementGroup = interior.append('g').attr('class', 'element1'),\n", 1679 | " main = elementGroup.append('g').attr('class', 'main'),\n", 1680 | " labels = BrunelD3.undoTransform(elementGroup.append('g').attr('class', 'labels').attr('aria-hidden', 'true'), elementGroup);\n", 1681 | "\n", 1682 | " function makeData() {\n", 1683 | " original = datasets[0];\n", 1684 | " if (filterRows) original = original.retainRows(filterRows);\n", 1685 | " processed = pre(original, 0);\n", 1686 | " processed = post(processed, 0);\n", 1687 | " var f0 = processed.field('#row'),\n", 1688 | " f1 = processed.field('#selection');\n", 1689 | " var keyFunc = function(d) { return f0.value(d) };\n", 1690 | " data = {\n", 1691 | " $row: function(d) { return f0.value(d.row) },\n", 1692 | " $selection: function(d) { return f1.value(d.row) },\n", 1693 | " $row_f: function(d) { return f0.valueFormatted(d.row) },\n", 1694 | " $selection_f: function(d) { return f1.valueFormatted(d.row) },\n", 1695 | " _split: function(d) { return 'ALL' },\n", 1696 | " _key: keyFunc,\n", 1697 | " _rows: BrunelD3.makeRowsWithKeys(keyFunc, processed.rowCount())\n", 1698 | " };\n", 1699 | " }\n", 1700 | "\n", 1701 | " // Build element from data ///////////////////////////////////////////////////////////////////\n", 1702 | "\n", 1703 | " function build(transitionMillis) {\n", 1704 | " element = elements[0];\n", 1705 | " // Read in the feature data and call build again when done\n", 1706 | " var features = {\n", 1707 | " 'https://brunelvis.org/geo/2.3/med/Japan.json': {\n", 1708 | " }\n", 1709 | " };\n", 1710 | " if (BrunelD3.addFeatures(data, features, null, null, this, transitionMillis)) return;\n", 1711 | " main.attr('class', 'diagram map');\n", 1712 | "\n", 1713 | " // Define selection entry operations\n", 1714 | " function initialState(selection) {\n", 1715 | " selection\n", 1716 | " .attr('class', 'element polygon filled')\n", 1717 | " .classed('nondata', function(d) {return !d || d.row == null})\n", 1718 | " .style('pointer-events', 'none')\n", 1719 | " }\n", 1720 | "\n", 1721 | " // Define selection update operations on merged data\n", 1722 | " function updateState(selection) {\n", 1723 | " selection\n", 1724 | " .attr('d', function(d) { return path(d).replace(/,L/g, 'L').replace(/,Z/g, 'Z') });\n", 1725 | " }\n", 1726 | " // Create selections, set the initial state and transition updates\n", 1727 | " selection = main.selectAll('.element').data(data._rows, function(d) { return d.key });\n", 1728 | " var added = selection.enter().append('path');\n", 1729 | " merged = selection.merge(added);\n", 1730 | " initialState(added);\n", 1731 | " selection.filter(BrunelD3.hasData)\n", 1732 | " .classed('selected', BrunelD3.isSelected(data))\n", 1733 | " .filter(BrunelD3.isSelected(data)).raise();\n", 1734 | " updateState(BrunelD3.transition(merged, transitionMillis));\n", 1735 | "\n", 1736 | " BrunelD3.transition(selection.exit(), transitionMillis/3)\n", 1737 | " .style('opacity', 0.5).each( function() {\n", 1738 | " this.remove(); BrunelD3.removeLabels(this); \n", 1739 | " });\n", 1740 | " }\n", 1741 | "\n", 1742 | " return {\n", 1743 | " data: function() { return processed },\n", 1744 | " original: function() { return original },\n", 1745 | " internal: function() { return data },\n", 1746 | " selection: function() { return merged },\n", 1747 | " makeData: makeData,\n", 1748 | " build: build,\n", 1749 | " chart: function() { return charts[0] },\n", 1750 | " group: function() { return elementGroup },\n", 1751 | " fields: {\n", 1752 | " key: ['#row']\n", 1753 | " }\n", 1754 | " };\n", 1755 | " }();\n", 1756 | "\n", 1757 | " // Define element #2 ///////////////////////////////////////////////////////////////////////////\n", 1758 | "\n", 1759 | " elements[1] = function() {\n", 1760 | " var original, processed, // data sets passed in and then transformed\n", 1761 | " element, data, // brunel element information and brunel data\n", 1762 | " selection, merged; // d3 selection and merged selection\n", 1763 | " var elementGroup = interior.append('g').attr('class', 'element2'),\n", 1764 | " main = elementGroup.append('g').attr('class', 'main'),\n", 1765 | " labels = BrunelD3.undoTransform(elementGroup.append('g').attr('class', 'labels').attr('aria-hidden', 'true'), elementGroup);\n", 1766 | "\n", 1767 | " function makeData() {\n", 1768 | " original = datasets[1];\n", 1769 | " if (filterRows) original = original.retainRows(filterRows);\n", 1770 | " processed = pre(original, 1);\n", 1771 | " processed = post(processed, 1);\n", 1772 | " var f0 = processed.field('longitude'),\n", 1773 | " f1 = processed.field('latitude'),\n", 1774 | " f2 = processed.field('y2016'),\n", 1775 | " f3 = processed.field('location_name'),\n", 1776 | " f4 = processed.field('#row'),\n", 1777 | " f5 = processed.field('#selection');\n", 1778 | " var keyFunc = function(d) { return f4.value(d) };\n", 1779 | " data = {\n", 1780 | " longitude: function(d) { return f0.value(d.row) },\n", 1781 | " latitude: function(d) { return f1.value(d.row) },\n", 1782 | " y2016: function(d) { return f2.value(d.row) },\n", 1783 | " location_name:function(d) { return f3.value(d.row) },\n", 1784 | " $row: function(d) { return f4.value(d.row) },\n", 1785 | " $selection: function(d) { return f5.value(d.row) },\n", 1786 | " longitude_f: function(d) { return f0.valueFormatted(d.row) },\n", 1787 | " latitude_f: function(d) { return f1.valueFormatted(d.row) },\n", 1788 | " y2016_f: function(d) { return f2.valueFormatted(d.row) },\n", 1789 | " location_name_f:function(d) { return f3.valueFormatted(d.row) },\n", 1790 | " $row_f: function(d) { return f4.valueFormatted(d.row) },\n", 1791 | " $selection_f: function(d) { return f5.valueFormatted(d.row) },\n", 1792 | " _split: function(d) { return f2.value(d.row) },\n", 1793 | " _key: keyFunc,\n", 1794 | " _rows: BrunelD3.makeRowsWithKeys(keyFunc, processed.rowCount())\n", 1795 | " };\n", 1796 | " }\n", 1797 | " // Aesthetic Functions\n", 1798 | " var scale_color = d3.scaleTime().domain([new Date('2016-03-19'), new Date('2016-03-28'), new Date('2016-04-06'), new Date('2016-04-15'), new Date('2016-04-24'), new Date('2016-05-03'), new Date('2016-05-13')])\n", 1799 | " .interpolate(d3.interpolateHcl)\n", 1800 | " .range([ '#FF0000', '#FFC0CB', '#edf8fb', '#b2e2e2', '#66c2a4', '#2ca25f', \n", 1801 | " '#006d2c']);\n", 1802 | " var color = function(d) { return scale_color(data.y2016(d)) };\n", 1803 | " legends._legend = legends._legend || { title: ['Y 2016'], dateFormat: BrunelData.util_DateFormat.YearMonthDay, \n", 1804 | " ticks: [new Date('2016-05-17'), new Date('2016-05-10'), new Date('2016-05-03'), new Date('2016-04-26'), new Date('2016-04-19'), new Date('2016-04-12'), new Date('2016-04-05'), new Date('2016-03-29'), new Date('2016-03-22'), new Date('2016-03-15')]};\n", 1805 | " legends._legend.color = scale_color;\n", 1806 | "\n", 1807 | " // Build element from data ///////////////////////////////////////////////////////////////////\n", 1808 | "\n", 1809 | " function build(transitionMillis) {\n", 1810 | " element = elements[1];\n", 1811 | " var w = geom.default_point_size;\n", 1812 | " var x = function(d) { return projection([data.longitude(d),data.latitude(d)])[0]};\n", 1813 | " var h = geom.default_point_size;\n", 1814 | " var y = function(d) { return projection([data.longitude(d),data.latitude(d)])[1]};\n", 1815 | " var labeling = [{\n", 1816 | " index: 0, method: 'box', location: ['right', 'center'], inside: false, align: 'start', pad: 3, dy: 0.3,\n", 1817 | " fit: false, granularity: 2,\n", 1818 | " content: function(d) {\n", 1819 | " return d.row == null ? null : data.location_name_f(d)\n", 1820 | "\t\t\t+ ', '\n", 1821 | "\t\t\t+ data.y2016_f(d)\n", 1822 | " }\n", 1823 | " }];\n", 1824 | "\n", 1825 | " // Define selection entry operations\n", 1826 | " function initialState(selection) {\n", 1827 | " selection\n", 1828 | " .attr('class', 'element point filled')\n", 1829 | " }\n", 1830 | "\n", 1831 | " // Define selection update operations on merged data\n", 1832 | " function updateState(selection) {\n", 1833 | " selection\n", 1834 | " .attr('cx',function(d) { return projection([data.longitude(d),data.latitude(d)])[0]})\n", 1835 | " .attr('cy',function(d) { return projection([data.longitude(d),data.latitude(d)])[1]})\n", 1836 | " .attr('r',geom.default_point_size / 2)\n", 1837 | " .filter(BrunelD3.hasData) // following only performed for data items\n", 1838 | " .style('fill', color);\n", 1839 | " }\n", 1840 | "\n", 1841 | " // Define labeling for the selection\n", 1842 | " function label(selection, transitionMillis) {\n", 1843 | " BrunelD3.label(selection, labels, transitionMillis, geom, labeling);\n", 1844 | "\n", 1845 | " var tooltipLabeling = {\n", 1846 | " index: -1, method: 'box', location: ['center', 'top'], inside: true, align: 'middle', pad: 0, dy: 0.7,\n", 1847 | " fit: true, granularity: 0,\n", 1848 | " content: function(d) {\n", 1849 | " return d.row == null ? null : 'Location Name: '\n", 1850 | "\t\t\t+ '' + data.location_name_f(d) + ''\n", 1851 | "\t\t\t+ '
'\n", 1852 | "\t\t\t+ 'Y 2016: '\n", 1853 | "\t\t\t+ '' + data.y2016_f(d) + ''\n", 1854 | " }\n", 1855 | " };\n", 1856 | " BrunelD3.addTooltip(selection, tooltipLabeling, geom);\n", 1857 | " }\n", 1858 | " // Create selections, set the initial state and transition updates\n", 1859 | " selection = main.selectAll('.element').data(data._rows, function(d) { return d.key });\n", 1860 | " var added = selection.enter().append('circle');\n", 1861 | " merged = selection.merge(added);\n", 1862 | " initialState(added);\n", 1863 | " selection.filter(BrunelD3.hasData)\n", 1864 | " .classed('selected', BrunelD3.isSelected(data))\n", 1865 | " .filter(BrunelD3.isSelected(data)).raise();\n", 1866 | " updateState(BrunelD3.transition(merged, transitionMillis));\n", 1867 | " label(merged, transitionMillis);\n", 1868 | "\n", 1869 | " BrunelD3.transition(selection.exit(), transitionMillis/3)\n", 1870 | " .style('opacity', 0.5).each( function() {\n", 1871 | " this.remove(); BrunelD3.removeLabels(this); \n", 1872 | " });\n", 1873 | " }\n", 1874 | "\n", 1875 | " return {\n", 1876 | " data: function() { return processed },\n", 1877 | " original: function() { return original },\n", 1878 | " internal: function() { return data },\n", 1879 | " selection: function() { return merged },\n", 1880 | " makeData: makeData,\n", 1881 | " build: build,\n", 1882 | " chart: function() { return charts[0] },\n", 1883 | " group: function() { return elementGroup },\n", 1884 | " fields: {\n", 1885 | " x: ['longitude'],\n", 1886 | " y: ['latitude'],\n", 1887 | " key: ['#row'],\n", 1888 | " color: ['y2016']\n", 1889 | " }\n", 1890 | " };\n", 1891 | " }();\n", 1892 | "\n", 1893 | " function build(time, noData) {\n", 1894 | " var first = elements[0].data() == null;\n", 1895 | " if (first) time = 0; // no transition for first call\n", 1896 | " if ((first || time > -1) && !noData) {\n", 1897 | " elements[0].makeData();\n", 1898 | " elements[1].makeData();\n", 1899 | " BrunelD3.addLegend(legends, legends._legend);\n", 1900 | " }\n", 1901 | " elements[0].build(time);\n", 1902 | " elements[1].build(time);\n", 1903 | " }\n", 1904 | "\n", 1905 | " // Expose the following components of the chart\n", 1906 | " return {\n", 1907 | " elements : elements,\n", 1908 | " interior : interior,\n", 1909 | " zoom: function(params, time) {\n", 1910 | " if (params) zoom.on('zoom').call(zoomNode, params, time);\n", 1911 | " return d3.zoomTransform(zoomNode);\n", 1912 | " },\n", 1913 | " build : build\n", 1914 | " };\n", 1915 | " }();\n", 1916 | "\n", 1917 | " function setData(rowData, i) { datasets[i||0] = BrunelD3.makeData(rowData) }\n", 1918 | " function updateAll(time) { charts.forEach(function(x) {x.build(time || 0)}) }\n", 1919 | " function buildAll() {\n", 1920 | " for (var i=0;i" 1986 | ] 1987 | }, 1988 | "execution_count": 7, 1989 | "metadata": {}, 1990 | "output_type": "execute_result" 1991 | } 1992 | ], 1993 | "source": [ 1994 | "# セル2-4. 地図上でのデータの可視化\n", 1995 | "\n", 1996 | "# 観測所の位置情報を元に、2016年の開花日データを日本地図にプロットする\n", 1997 | "%brunel map('japan') + data('df_cherry_blooming') x(longitude) y(latitude) tooltip(location_name, y2016) \\\n", 1998 | "color(y2016:[red,pink,greens]) label(location_name,y2016) ::width=1200, height=600" 1999 | ] 2000 | }, 2001 | { 2002 | "cell_type": "markdown", 2003 | "metadata": {}, 2004 | "source": [ 2005 | "## データの加工\n", 2006 | "予測に有用な特徴量を作成する。" 2007 | ] 2008 | }, 2009 | { 2010 | "cell_type": "code", 2011 | "execution_count": 8, 2012 | "metadata": { 2013 | "scrolled": true 2014 | }, 2015 | "outputs": [ 2016 | { 2017 | "name": "stdout", 2018 | "output_type": "stream", 2019 | "text": [ 2020 | "['旭川' '宇都宮' '横浜' '岡山' '下関' '岐阜' '宮崎' '京都' '金沢' '釧路' '熊谷' '熊本' '広島' '甲府'\n", 2021 | " '高松' '高知' '佐賀' '札幌' '山形' '鹿児島' '室蘭' '秋田' '松江' '松山' '新潟' '神戸' '水戸' '盛岡'\n", 2022 | " '青森' '静岡' '仙台' '前橋' '帯広' '大阪' '大分' '稚内' '銚子' '長崎' '長野' '鳥取' '津' '東京' '徳島'\n", 2023 | " '奈良' '函館' '彦根' '富山' '福井' '福岡' '福島' '名古屋' '網走' '和歌山']\n", 2024 | "[2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017]\n" 2025 | ] 2026 | } 2027 | ], 2028 | "source": [ 2029 | "# セル3-1. ループ処理用のリスト作成\n", 2030 | "\n", 2031 | "# location_nameとyearの一覧を取得する\n", 2032 | "location_list = df_weather['location_name'].drop_duplicates().values\n", 2033 | "year_list = df_weather['year'].drop_duplicates().values\n", 2034 | "\n", 2035 | "print(location_list)\n", 2036 | "print(year_list)" 2037 | ] 2038 | }, 2039 | { 2040 | "cell_type": "code", 2041 | "execution_count": 9, 2042 | "metadata": { 2043 | "scrolled": true 2044 | }, 2045 | "outputs": [ 2046 | { 2047 | "name": "stdout", 2048 | "output_type": "stream", 2049 | "text": [ 2050 | "Feature extraction is started at 2018/06/06 12:03:23\n", 2051 | "Feature extraction is completed at 2018/06/06 12:03:55\n" 2052 | ] 2053 | } 2054 | ], 2055 | "source": [ 2056 | "# セル3-2. 特徴量の作成\n", 2057 | "\n", 2058 | "from datetime import datetime\n", 2059 | "\n", 2060 | "print('Feature extraction is started at ', datetime.now().strftime(\"%Y/%m/%d %H:%M:%S\"))\n", 2061 | "\n", 2062 | "# 加工後のデータを格納するための空DataFrameを作成しておく\n", 2063 | "df_weather_custom = pd.DataFrame(index=[], columns=[])\n", 2064 | "\n", 2065 | "for current_location in location_list:\n", 2066 | " for current_year in year_list:\n", 2067 | " df_one = df_weather[(df_weather.location_name == current_location) & (df_weather.year == current_year)].copy()\n", 2068 | "\n", 2069 | " # NULL値(NaN)を0へ置換する。降水量(precipitation)に関して、0mmの降雨と降雨なし(Null)は異なるが、今回は同じとみなす\n", 2070 | " df_one = df_one.fillna(0)\n", 2071 | "\n", 2072 | " # 「3日後に開花しているかどうか」をラベル(モデルが出力すべき正解の値)として定義する\n", 2073 | " df_one['label'] = df_one['blooming'].shift(-3)\n", 2074 | "\n", 2075 | " # データ期間の終わりである6/27 - 6/30は「3日後のデータ」が存在せず、NaNとなるので1(開花)に変更する\n", 2076 | " df_one['label'] = df_one['label'].fillna(1)\n", 2077 | "\n", 2078 | " # 気温0℃以下を一律0℃とみなす\n", 2079 | " df_one['temperature_highest'] = df_one['temperature_highest'].where(df_one['temperature_highest'] >=0,0)\n", 2080 | " df_one['temperature_avg'] = df_one['temperature_avg'].where(df_one['temperature_avg'] >=0,0)\n", 2081 | "\n", 2082 | " # 最高気温の7日間移動平均を算出する\n", 2083 | " df_one['th_mean7'] = df_one['temperature_highest'].rolling(window=7, min_periods=1).mean()\n", 2084 | "\n", 2085 | " # 2月以降、3月以降の平均気温の累積値をそれぞれ算出する\n", 2086 | " df_one['ta_cumsum2'] = df_one['temperature_avg'].where(df_one.index.month >= 2,0).cumsum()\n", 2087 | " df_one['ta_cumsum3'] = df_one['temperature_avg'].where(df_one.index.month >= 3,0).cumsum()\n", 2088 | " \n", 2089 | " # 気温3℃以下を一律3℃とみなす\n", 2090 | " df_one['temperature_highest'] = df_one['temperature_highest'].where(df_one['temperature_highest'] >=3,3)\n", 2091 | " df_one['temperature_avg'] = df_one['temperature_avg'].where(df_one['temperature_avg'] >=3,3)\n", 2092 | " \n", 2093 | " # 1月の平均気温の平均を算出する\n", 2094 | " df_one['ta_mean1'] = df_one['temperature_avg'].where(df_one.index.month == 1,0).mean()\n", 2095 | " \n", 2096 | "\n", 2097 | " df_weather_custom = pd.concat([df_weather_custom, df_one])\n", 2098 | "\n", 2099 | "print('Feature extraction is completed at ', datetime.now().strftime(\"%Y/%m/%d %H:%M:%S\"))" 2100 | ] 2101 | }, 2102 | { 2103 | "cell_type": "code", 2104 | "execution_count": 10, 2105 | "metadata": {}, 2106 | "outputs": [ 2107 | { 2108 | "data": { 2109 | "text/html": [ 2110 | "
\n", 2111 | "\n", 2124 | "\n", 2125 | " \n", 2126 | " \n", 2127 | " \n", 2128 | " \n", 2129 | " \n", 2130 | " \n", 2131 | " \n", 2132 | " \n", 2133 | " \n", 2134 | " \n", 2135 | " \n", 2136 | " \n", 2137 | " \n", 2138 | " \n", 2139 | " \n", 2140 | " \n", 2141 | " \n", 2142 | " \n", 2143 | " \n", 2144 | " \n", 2145 | " \n", 2146 | " \n", 2147 | " \n", 2148 | " \n", 2149 | " \n", 2150 | " \n", 2151 | " \n", 2152 | " \n", 2153 | " \n", 2154 | " \n", 2155 | " \n", 2156 | " \n", 2157 | " \n", 2158 | " \n", 2159 | " \n", 2160 | " \n", 2161 | " \n", 2162 | " \n", 2163 | " \n", 2164 | " \n", 2165 | " \n", 2166 | " \n", 2167 | " \n", 2168 | " \n", 2169 | " \n", 2170 | " \n", 2171 | " \n", 2172 | " \n", 2173 | " \n", 2174 | " \n", 2175 | " \n", 2176 | " \n", 2177 | " \n", 2178 | " \n", 2179 | " \n", 2180 | " \n", 2181 | " \n", 2182 | " \n", 2183 | " \n", 2184 | " \n", 2185 | " \n", 2186 | " \n", 2187 | " \n", 2188 | " \n", 2189 | " \n", 2190 | " \n", 2191 | " \n", 2192 | " \n", 2193 | " \n", 2194 | " \n", 2195 | " \n", 2196 | " \n", 2197 | " \n", 2198 | " \n", 2199 | " \n", 2200 | " \n", 2201 | " \n", 2202 | " \n", 2203 | " \n", 2204 | " \n", 2205 | " \n", 2206 | " \n", 2207 | " \n", 2208 | " \n", 2209 | " \n", 2210 | " \n", 2211 | " \n", 2212 | " \n", 2213 | " \n", 2214 | " \n", 2215 | " \n", 2216 | " \n", 2217 | " \n", 2218 | " \n", 2219 | " \n", 2220 | " \n", 2221 | " \n", 2222 | " \n", 2223 | " \n", 2224 | " \n", 2225 | " \n", 2226 | " \n", 2227 | " \n", 2228 | " \n", 2229 | " \n", 2230 | " \n", 2231 | " \n", 2232 | " \n", 2233 | " \n", 2234 | " \n", 2235 | " \n", 2236 | " \n", 2237 | " \n", 2238 | " \n", 2239 | " \n", 2240 | " \n", 2241 | " \n", 2242 | " \n", 2243 | " \n", 2244 | " \n", 2245 | " \n", 2246 | " \n", 2247 | " \n", 2248 | " \n", 2249 | " \n", 2250 | " \n", 2251 | " \n", 2252 | " \n", 2253 | " \n", 2254 | " \n", 2255 | " \n", 2256 | " \n", 2257 | " \n", 2258 | " \n", 2259 | " \n", 2260 | " \n", 2261 | " \n", 2262 | " \n", 2263 | " \n", 2264 | " \n", 2265 | " \n", 2266 | " \n", 2267 | " \n", 2268 | " \n", 2269 | " \n", 2270 | " \n", 2271 | " \n", 2272 | " \n", 2273 | " \n", 2274 | " \n", 2275 | " \n", 2276 | " \n", 2277 | " \n", 2278 | " \n", 2279 | " \n", 2280 | " \n", 2281 | " \n", 2282 | " \n", 2283 | " \n", 2284 | " \n", 2285 | " \n", 2286 | " \n", 2287 | " \n", 2288 | " \n", 2289 | " \n", 2290 | " \n", 2291 | " \n", 2292 | " \n", 2293 | " \n", 2294 | " \n", 2295 | " \n", 2296 | " \n", 2297 | " \n", 2298 | " \n", 2299 | " \n", 2300 | "
prec_idblock_idatmospheric_pressureprecipitationtemperature_avgtemperature_highesttemperature_lowesthumiditydaylight_hoursbloomingyearlabelth_mean7ta_cumsum2ta_cumsum3ta_mean1
count105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000105682.000000
mean52.35849147665.1886791007.2626433.80439011.54765115.7239936.86745666.9461885.4439480.4863362012.0000000.50288615.233320576.027336477.3476980.822720
std21.470656140.43315830.96565410.6294597.0933318.1801688.10544814.1226554.1944240.4998163.1624510.4999947.943180628.167811578.2442440.278733
min11.00000047401.0000000.0000000.0000003.0000003.000000-25.7000000.0000000.0000000.0000002007.0000000.0000000.0000000.0000000.0000000.510989
25%36.00000047595.0000001003.2000000.0000004.8000009.1000000.80000057.0000001.1000000.0000002009.0000000.0000009.18571425.8000000.0000000.533702
50%53.00000047648.0000001009.5000000.00000010.30000015.2000006.00000067.0000005.4000000.0000002012.0000001.00000014.685714337.800000208.7000000.773077
75%68.00000047772.0000001015.3000002.00000017.90000022.80000013.50000077.0000009.1000001.0000002015.0000001.00000022.328571973.375000836.7000001.054696
max88.00000047895.0000001034.200000247.50000031.30000039.80000028.500000100.00000015.0000001.0000002017.0000001.00000034.9857142657.5000002379.6000001.690110
\n", 2301 | "
" 2302 | ], 2303 | "text/plain": [ 2304 | " prec_id block_id atmospheric_pressure precipitation \\\n", 2305 | "count 105682.000000 105682.000000 105682.000000 105682.000000 \n", 2306 | "mean 52.358491 47665.188679 1007.262643 3.804390 \n", 2307 | "std 21.470656 140.433158 30.965654 10.629459 \n", 2308 | "min 11.000000 47401.000000 0.000000 0.000000 \n", 2309 | "25% 36.000000 47595.000000 1003.200000 0.000000 \n", 2310 | "50% 53.000000 47648.000000 1009.500000 0.000000 \n", 2311 | "75% 68.000000 47772.000000 1015.300000 2.000000 \n", 2312 | "max 88.000000 47895.000000 1034.200000 247.500000 \n", 2313 | "\n", 2314 | " temperature_avg temperature_highest temperature_lowest \\\n", 2315 | "count 105682.000000 105682.000000 105682.000000 \n", 2316 | "mean 11.547651 15.723993 6.867456 \n", 2317 | "std 7.093331 8.180168 8.105448 \n", 2318 | "min 3.000000 3.000000 -25.700000 \n", 2319 | "25% 4.800000 9.100000 0.800000 \n", 2320 | "50% 10.300000 15.200000 6.000000 \n", 2321 | "75% 17.900000 22.800000 13.500000 \n", 2322 | "max 31.300000 39.800000 28.500000 \n", 2323 | "\n", 2324 | " humidity daylight_hours blooming year \\\n", 2325 | "count 105682.000000 105682.000000 105682.000000 105682.000000 \n", 2326 | "mean 66.946188 5.443948 0.486336 2012.000000 \n", 2327 | "std 14.122655 4.194424 0.499816 3.162451 \n", 2328 | "min 0.000000 0.000000 0.000000 2007.000000 \n", 2329 | "25% 57.000000 1.100000 0.000000 2009.000000 \n", 2330 | "50% 67.000000 5.400000 0.000000 2012.000000 \n", 2331 | "75% 77.000000 9.100000 1.000000 2015.000000 \n", 2332 | "max 100.000000 15.000000 1.000000 2017.000000 \n", 2333 | "\n", 2334 | " label th_mean7 ta_cumsum2 ta_cumsum3 \\\n", 2335 | "count 105682.000000 105682.000000 105682.000000 105682.000000 \n", 2336 | "mean 0.502886 15.233320 576.027336 477.347698 \n", 2337 | "std 0.499994 7.943180 628.167811 578.244244 \n", 2338 | "min 0.000000 0.000000 0.000000 0.000000 \n", 2339 | "25% 0.000000 9.185714 25.800000 0.000000 \n", 2340 | "50% 1.000000 14.685714 337.800000 208.700000 \n", 2341 | "75% 1.000000 22.328571 973.375000 836.700000 \n", 2342 | "max 1.000000 34.985714 2657.500000 2379.600000 \n", 2343 | "\n", 2344 | " ta_mean1 \n", 2345 | "count 105682.000000 \n", 2346 | "mean 0.822720 \n", 2347 | "std 0.278733 \n", 2348 | "min 0.510989 \n", 2349 | "25% 0.533702 \n", 2350 | "50% 0.773077 \n", 2351 | "75% 1.054696 \n", 2352 | "max 1.690110 " 2353 | ] 2354 | }, 2355 | "execution_count": 10, 2356 | "metadata": {}, 2357 | "output_type": "execute_result" 2358 | } 2359 | ], 2360 | "source": [ 2361 | "# セル3-3. データの統計量の確認\n", 2362 | "df_weather_custom.describe()" 2363 | ] 2364 | }, 2365 | { 2366 | "cell_type": "code", 2367 | "execution_count": 11, 2368 | "metadata": { 2369 | "scrolled": false 2370 | }, 2371 | "outputs": [ 2372 | { 2373 | "data": { 2374 | "text/html": [ 2375 | "\n", 2390 | "\n", 2391 | "\n", 2392 | "\n", 2393 | "\n", 2394 | "\n", 2395 | "\n", 2398 | "\n", 2399 | "
\n", 2400 | "" 2401 | ], 2402 | "text/plain": [ 2403 | "" 2404 | ] 2405 | }, 2406 | "metadata": {}, 2407 | "output_type": "display_data" 2408 | }, 2409 | { 2410 | "data": { 2411 | "application/javascript": [ 2412 | "/*\n", 2413 | " * Copyright (c) 2015 IBM Corporation and others.\n", 2414 | " *\n", 2415 | " * Licensed under the Apache License, Version 2.0 (the \"License\");\n", 2416 | " * You may not use this file except in compliance with the License.\n", 2417 | " * You may obtain a copy of the License at\n", 2418 | " *\n", 2419 | " * http://www.apache.org/licenses/LICENSE-2.0\n", 2420 | " *\n", 2421 | " * Unless required by applicable law or agreed to in writing, software\n", 2422 | " * distributed under the License is distributed on an \"AS IS\" BASIS,\n", 2423 | " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", 2424 | " * See the License for the specific language governing permissions and\n", 2425 | " * limitations under the License.\n", 2426 | " */\n", 2427 | "\n", 2428 | "require.config({\n", 2429 | " waitSeconds: 60,\n", 2430 | " paths: {\n", 2431 | " 'd3': '//cdnjs.cloudflare.com/ajax/libs/d3/4.2.1/d3.min',\n", 2432 | " 'topojson': '//cdnjs.cloudflare.com/ajax/libs/topojson/1.6.20/topojson.min',\n", 2433 | " 'brunel' : '/data/jupyter2/static-file-content-delivery-network/nbextensions/brunel_ext/brunel.2.3.min',\n", 2434 | " 'brunelControls' : '/data/jupyter2/static-file-content-delivery-network/nbextensions/brunel_ext/brunel.controls.2.3.min'\n", 2435 | " },\n", 2436 | " shim: {\n", 2437 | " 'brunel' : {\n", 2438 | " exports: 'BrunelD3',\n", 2439 | " deps: ['d3', 'topojson'],\n", 2440 | " init: function() {\n", 2441 | " return {\n", 2442 | " BrunelD3 : BrunelD3,\n", 2443 | " BrunelData : BrunelData\n", 2444 | " }\n", 2445 | " }\n", 2446 | " },\n", 2447 | " 'brunelControls' : {\n", 2448 | " exports: 'BrunelEventHandlers',\n", 2449 | " init: function() {\n", 2450 | " return {\n", 2451 | " BrunelEventHandlers: BrunelEventHandlers,\n", 2452 | " BrunelJQueryControlFactory: BrunelJQueryControlFactory\n", 2453 | " }\n", 2454 | " }\n", 2455 | " }\n", 2456 | "\n", 2457 | " }\n", 2458 | "\n", 2459 | "});\n", 2460 | "\n", 2461 | "require([\"d3\"], function(d3) {\n", 2462 | " require([\"brunel\", \"brunelControls\"], function(brunel, brunelControls) {\n", 2463 | " function BrunelVis(visId) {\n", 2464 | " \"use strict\"; // strict mode\n", 2465 | " var datasets = [], // array of datasets for the original data\n", 2466 | " pre = function(d, i) { return d }, // default pre-process does nothing\n", 2467 | " post = function(d, i) { return d }, // default post-process does nothing\n", 2468 | " transitionTime = 200, // transition time for animations\n", 2469 | " charts = [], // the charts in the system\n", 2470 | " vis = d3.select('#' + visId).attr('class', 'brunel'); // the SVG container\n", 2471 | "\n", 2472 | " BrunelD3.addDefinitions(vis); // ensure standard symbols present\n", 2473 | "\n", 2474 | " // Define chart #1 in the visualization //////////////////////////////////////////////////////////\n", 2475 | "\n", 2476 | " charts[0] = function(parentNode, filterRows) {\n", 2477 | " var geom = BrunelD3.geometry(parentNode || vis.node(), 0, 0, 1, 1, 5, 35, 37, 148),\n", 2478 | " elements = []; // array of elements in this chart\n", 2479 | "\n", 2480 | " // Define groups for the chart parts ///////////////////////////////////////////////////////////\n", 2481 | "\n", 2482 | " var chart = vis.append('g').attr('class', 'chart1')\n", 2483 | " .attr('transform','translate(' + geom.chart_left + ',' + geom.chart_top + ')');\n", 2484 | " var overlay = chart.append('g').attr('class', 'element').attr('class', 'overlay');\n", 2485 | " var zoom = d3.zoom().scaleExtent([1/3,3]);\n", 2486 | " var zoomNode = overlay.append('rect').attr('class', 'overlay')\n", 2487 | " .attr('x', geom.inner_left).attr('y', geom.inner_top)\n", 2488 | " .attr('width', geom.inner_rawWidth).attr('height', geom.inner_rawHeight)\n", 2489 | " .style('cursor', 'move').call(zoom)\n", 2490 | " .node();\n", 2491 | " zoomNode.__zoom = d3.zoomIdentity;\n", 2492 | " chart.append('rect').attr('class', 'background').attr('width', geom.chart_right-geom.chart_left).attr('height', geom.chart_bottom-geom.chart_top);\n", 2493 | " var interior = chart.append('g').attr('class', 'interior zoomNone')\n", 2494 | " .attr('transform','translate(' + geom.inner_left + ',' + geom.inner_top + ')')\n", 2495 | " .attr('clip-path', 'url(#clip_visidb4d08e62-6981-11e8-ae66-3a5f3782500e_chart1_inner)');\n", 2496 | " interior.append('rect').attr('class', 'inner').attr('width', geom.inner_width).attr('height', geom.inner_height);\n", 2497 | " var gridGroup = interior.append('g').attr('class', 'grid');\n", 2498 | " var axes = chart.append('g').attr('class', 'axis')\n", 2499 | " .attr('transform','translate(' + geom.inner_left + ',' + geom.inner_top + ')');\n", 2500 | " var legends = chart.append('g').attr('class', 'legend')\n", 2501 | " .attr('transform','translate(' + (geom.chart_right-geom.chart_left - 3) + ',' + 0 + ')');\n", 2502 | " vis.append('clipPath').attr('id', 'clip_visidb4d08e62-6981-11e8-ae66-3a5f3782500e_chart1_inner').append('rect')\n", 2503 | " .attr('x', 0).attr('y', 0)\n", 2504 | " .attr('width', geom.inner_rawWidth+1).attr('height', geom.inner_rawHeight+1);\n", 2505 | "\n", 2506 | " // Scales //////////////////////////////////////////////////////////////////////////////////////\n", 2507 | "\n", 2508 | " var scale_x = d3.scaleTime().domain([new Date('2016-01-01'), new Date('2016-06-30')])\n", 2509 | " .range([0, geom.inner_width]);\n", 2510 | " var scale_inner = d3.scaleLinear().domain([0,1])\n", 2511 | " .range([-0.5, 0.5]);\n", 2512 | " var scale_y = d3.scaleLinear().domain([0, 35.000003])\n", 2513 | " .range([geom.inner_height, 0]);\n", 2514 | " var base_scales = [scale_x, scale_y]; // untransformed original scales\n", 2515 | "\n", 2516 | " // Axes ////////////////////////////////////////////////////////////////////////////////////////\n", 2517 | "\n", 2518 | " axes.append('g').attr('class', 'x axis')\n", 2519 | " .attr('transform','translate(0,' + geom.inner_rawHeight + ')')\n", 2520 | " .attr('clip-path', 'url(#clip_visidb4d08e62-6981-11e8-ae66-3a5f3782500e_chart1_haxis)');\n", 2521 | " vis.append('clipPath').attr('id', 'clip_visidb4d08e62-6981-11e8-ae66-3a5f3782500e_chart1_haxis').append('polyline')\n", 2522 | " .attr('points', '-1,-1000, -1,-1 -5,5, -1000,5, -100,1000, 10000,1000 10000,-1000');\n", 2523 | " axes.select('g.axis.x').append('text').attr('class', 'title').text('Date').style('text-anchor', 'middle')\n", 2524 | " .attr('x',geom.inner_rawWidth/2)\n", 2525 | " .attr('y', geom.inner_bottom - 2.0).attr('dy','-0.27em');\n", 2526 | " axes.append('g').attr('class', 'y axis')\n", 2527 | " .attr('clip-path', 'url(#clip_visidb4d08e62-6981-11e8-ae66-3a5f3782500e_chart1_vaxis)');\n", 2528 | " vis.append('clipPath').attr('id', 'clip_visidb4d08e62-6981-11e8-ae66-3a5f3782500e_chart1_vaxis').append('polyline')\n", 2529 | " .attr('points', '-1000,-10000, 10000,-10000, 10000,' + (geom.inner_rawHeight+1) + ', -1,' + (geom.inner_rawHeight+1) + ', -1,' + (geom.inner_rawHeight+5) + ', -1000,' + (geom.inner_rawHeight+5) );\n", 2530 | "\n", 2531 | " var axis_bottom = d3.axisBottom(scale_x).ticks(Math.min(10, Math.round(geom.inner_width / 99.0)));\n", 2532 | " var axis_left = d3.axisLeft(scale_y).ticks(Math.min(10, Math.round(geom.inner_width / 20)));\n", 2533 | "\n", 2534 | " function buildAxes(time) {\n", 2535 | " var axis_x = axes.select('g.axis.x');\n", 2536 | " BrunelD3.transition(axis_x, time).call(axis_bottom.scale(scale_x));\n", 2537 | " var axis_y = axes.select('g.axis.y');\n", 2538 | " BrunelD3.transition(axis_y, time).call(axis_left.scale(scale_y));\n", 2539 | " }\n", 2540 | " zoom.on('zoom', function(t, time) {\n", 2541 | " t = t ||BrunelD3.restrictZoom(d3.event.transform, geom, this);\n", 2542 | " scale_x = t.rescaleX(base_scales[0]);\n", 2543 | " scale_y = t.rescaleY(base_scales[1]);\n", 2544 | " zoomNode.__zoom = t;\n", 2545 | " interior.attr('class', 'interior ' + BrunelD3.zoomLabel(t.k));;\n", 2546 | " build(time || -1);\n", 2547 | " });\n", 2548 | "\n", 2549 | " // Define element #1 ///////////////////////////////////////////////////////////////////////////\n", 2550 | "\n", 2551 | " elements[0] = function() {\n", 2552 | " var original, processed, // data sets passed in and then transformed\n", 2553 | " element, data, // brunel element information and brunel data\n", 2554 | " selection, merged; // d3 selection and merged selection\n", 2555 | " var elementGroup = interior.append('g').attr('class', 'element1'),\n", 2556 | " main = elementGroup.append('g').attr('class', 'main'),\n", 2557 | " labels = BrunelD3.undoTransform(elementGroup.append('g').attr('class', 'labels').attr('aria-hidden', 'true'), elementGroup);\n", 2558 | "\n", 2559 | " function makeData() {\n", 2560 | " original = datasets[0];\n", 2561 | " if (filterRows) original = original.retainRows(filterRows);\n", 2562 | " processed = pre(original, 0)\n", 2563 | " .reduce('date; temperature_highest; th_mean7; #series; #row; #count')\n", 2564 | " .series('temperature_highest,th_mean7;date, temperature_highest, th_mean7')\n", 2565 | " .sortRows('date:ascending');\n", 2566 | " processed = post(processed, 0);\n", 2567 | " var f0 = processed.field('#series'),\n", 2568 | " f1 = processed.field('#values'),\n", 2569 | " f2 = processed.field('date'),\n", 2570 | " f3 = processed.field('temperature_highest'),\n", 2571 | " f4 = processed.field('th_mean7'),\n", 2572 | " f5 = processed.field('#row'),\n", 2573 | " f6 = processed.field('#selection');\n", 2574 | " var keyFunc = function(d) { return f0.value(d) };\n", 2575 | " data = {\n", 2576 | " $series: function(d) { return f0.value(d.row) },\n", 2577 | " $values: function(d) { return f1.value(d.row) },\n", 2578 | " date: function(d) { return f2.value(d.row) },\n", 2579 | " temperature_highest:function(d) { return f3.value(d.row) },\n", 2580 | " th_mean7: function(d) { return f4.value(d.row) },\n", 2581 | " $row: function(d) { return f5.value(d.row) },\n", 2582 | " $selection: function(d) { return f6.value(d.row) },\n", 2583 | " $series_f: function(d) { return f0.valueFormatted(d.row) },\n", 2584 | " $values_f: function(d) { return f1.valueFormatted(d.row) },\n", 2585 | " date_f: function(d) { return f2.valueFormatted(d.row) },\n", 2586 | " temperature_highest_f:function(d) { return f3.valueFormatted(d.row) },\n", 2587 | " th_mean7_f: function(d) { return f4.valueFormatted(d.row) },\n", 2588 | " $row_f: function(d) { return f5.valueFormatted(d.row) },\n", 2589 | " $selection_f: function(d) { return f6.valueFormatted(d.row) },\n", 2590 | " _split: function(d) { return f0.value(d.row) },\n", 2591 | " _key: keyFunc,\n", 2592 | " _rows: BrunelD3.makeRowsWithKeys(keyFunc, processed.rowCount())\n", 2593 | " };\n", 2594 | " }\n", 2595 | " // Aesthetic Functions\n", 2596 | " var scale_color = d3.scaleOrdinal()\n", 2597 | " .domain(['temperature_highest', 'th_mean7'])\n", 2598 | " .range([ '#00538A', '#C10020', '#F4C800', '#007D34', '#803E75', '#FF6800', \n", 2599 | " '#817066', '#FFB300', '#F6768E', '#93AA00', '#53377A', '#FF8E00', '#B32851', \n", 2600 | " '#CEA262', '#FF7A5C', '#7F180D', '#593315', '#F13A13', '#232C16']);\n", 2601 | " var color = function(d) { return scale_color(data.$series(d)) };\n", 2602 | " legends._legend = legends._legend || { title: ['Series'], \n", 2603 | " ticks: scale_color.domain()};\n", 2604 | " legends._legend.color = scale_color;\n", 2605 | "\n", 2606 | " // Build element from data ///////////////////////////////////////////////////////////////////\n", 2607 | "\n", 2608 | " function build(transitionMillis) {\n", 2609 | " element = elements[0];\n", 2610 | " var w = geom.default_point_size;\n", 2611 | " var x = function(d) { return scale_x(data.date(d))};\n", 2612 | " var h = geom.default_point_size;\n", 2613 | " var y = function(d) { return scale_y(data.$values(d))};\n", 2614 | " // Define paths\n", 2615 | " var path = d3.line().x(x).y(y);\n", 2616 | " var splits = BrunelD3.makePathSplits(data, path, x);\n", 2617 | "\n", 2618 | " // Define selection entry operations\n", 2619 | " function initialState(selection) {\n", 2620 | " selection\n", 2621 | " .attr('class', 'element line')\n", 2622 | " }\n", 2623 | "\n", 2624 | " // Define selection update operations on merged data\n", 2625 | " function updateState(selection) {\n", 2626 | " selection\n", 2627 | " .attr('d', function(d) { return d.path })\n", 2628 | " .filter(BrunelD3.hasData) // following only performed for data items\n", 2629 | " .style('stroke', color);\n", 2630 | " }\n", 2631 | "\n", 2632 | " // Define labeling for the selection\n", 2633 | " function label(selection, transitionMillis) {\n", 2634 | "\n", 2635 | " var tooltipLabeling = {\n", 2636 | " index: -1, method: 'path', location: ['center', 'center'], inside: true, align: 'middle', pad: 0, dy: 0.3,\n", 2637 | " fit: true, granularity: 0,\n", 2638 | " path: path,\n", 2639 | " content: function(d) {\n", 2640 | " return d.row == null ? null : 'Date: '\n", 2641 | "\t\t\t+ '' + data.date_f(d) + ''\n", 2642 | "\t\t\t+ '
'\n", 2643 | "\t\t\t+ 'Temperature Highest: '\n", 2644 | "\t\t\t+ '' + data.temperature_highest_f(d) + ''\n", 2645 | "\t\t\t+ '
'\n", 2646 | "\t\t\t+ 'Th Mean 7: '\n", 2647 | "\t\t\t+ '' + data.th_mean7_f(d) + ''\n", 2648 | " }\n", 2649 | " };\n", 2650 | " BrunelD3.addTooltip(selection, tooltipLabeling, geom);\n", 2651 | " }\n", 2652 | " // Create selections, set the initial state and transition updates\n", 2653 | " selection = main.selectAll('.element').data(splits, function(d) { return d.key });\n", 2654 | " var added = selection.enter().append('path');\n", 2655 | " merged = selection.merge(added);\n", 2656 | " initialState(added);\n", 2657 | " selection.filter(BrunelD3.hasData)\n", 2658 | " .classed('selected', BrunelD3.isSelected(data))\n", 2659 | " .filter(BrunelD3.isSelected(data)).raise();\n", 2660 | " updateState(BrunelD3.transition(merged, transitionMillis));\n", 2661 | " label(merged, transitionMillis);\n", 2662 | "\n", 2663 | " BrunelD3.transition(selection.exit(), transitionMillis/3)\n", 2664 | " .style('opacity', 0.5).each( function() {\n", 2665 | " this.remove(); BrunelD3.removeLabels(this); \n", 2666 | " });\n", 2667 | " }\n", 2668 | "\n", 2669 | " return {\n", 2670 | " data: function() { return processed },\n", 2671 | " original: function() { return original },\n", 2672 | " internal: function() { return data },\n", 2673 | " selection: function() { return merged },\n", 2674 | " makeData: makeData,\n", 2675 | " build: build,\n", 2676 | " chart: function() { return charts[0] },\n", 2677 | " group: function() { return elementGroup },\n", 2678 | " fields: {\n", 2679 | " x: ['date'],\n", 2680 | " y: ['temperature_highest', 'th_mean7'],\n", 2681 | " key: ['#series'],\n", 2682 | " color: ['#series']\n", 2683 | " }\n", 2684 | " };\n", 2685 | " }();\n", 2686 | "\n", 2687 | " function build(time, noData) {\n", 2688 | " var first = elements[0].data() == null;\n", 2689 | " if (first) time = 0; // no transition for first call\n", 2690 | " buildAxes(time);\n", 2691 | " if ((first || time > -1) && !noData) {\n", 2692 | " elements[0].makeData();\n", 2693 | " BrunelD3.addLegend(legends, legends._legend);\n", 2694 | " }\n", 2695 | " elements[0].build(time);\n", 2696 | " }\n", 2697 | "\n", 2698 | " // Expose the following components of the chart\n", 2699 | " return {\n", 2700 | " elements : elements,\n", 2701 | " interior : interior,\n", 2702 | " scales: {x:scale_x, y:scale_y},\n", 2703 | " zoom: function(params, time) {\n", 2704 | " if (params) zoom.on('zoom').call(zoomNode, params, time);\n", 2705 | " return d3.zoomTransform(zoomNode);\n", 2706 | " },\n", 2707 | " build : build\n", 2708 | " };\n", 2709 | " }();\n", 2710 | "\n", 2711 | " function setData(rowData, i) { datasets[i||0] = BrunelD3.makeData(rowData) }\n", 2712 | " function updateAll(time) { charts.forEach(function(x) {x.build(time || 0)}) }\n", 2713 | " function buildAll() {\n", 2714 | " for (var i=0;i" 2808 | ] 2809 | }, 2810 | "execution_count": 11, 2811 | "metadata": {}, 2812 | "output_type": "execute_result" 2813 | } 2814 | ], 2815 | "source": [ 2816 | "# セル3-4. 特徴量の可視化\n", 2817 | "df_tokyo_2016 = df_weather_custom[(df_weather_custom.location_name == '東京') & (df_weather_custom.year == 2016)]\n", 2818 | "%brunel data('df_tokyo_2016') line x(date) y(temperature_highest, th_mean7) color(#series) tooltip(date,temperature_highest,th_mean7) ::width=1200" 2819 | ] 2820 | }, 2821 | { 2822 | "cell_type": "markdown", 2823 | "metadata": {}, 2824 | "source": [ 2825 | "## モデル作成\n", 2826 | "機械学習のライブラリーを利用して予測モデルを作成する。" 2827 | ] 2828 | }, 2829 | { 2830 | "cell_type": "code", 2831 | "execution_count": 12, 2832 | "metadata": {}, 2833 | "outputs": [], 2834 | "source": [ 2835 | "# セル4-1. ライブラリーのインポート\n", 2836 | "\n", 2837 | "# 今回はscikit-learnを使用する\n", 2838 | "import numpy as np\n", 2839 | "from sklearn.linear_model import LogisticRegression\n", 2840 | "from sklearn.preprocessing import StandardScaler" 2841 | ] 2842 | }, 2843 | { 2844 | "cell_type": "code", 2845 | "execution_count": 13, 2846 | "metadata": {}, 2847 | "outputs": [ 2848 | { 2849 | "data": { 2850 | "text/plain": [ 2851 | "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n", 2852 | " intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n", 2853 | " penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n", 2854 | " verbose=0, warm_start=False)" 2855 | ] 2856 | }, 2857 | "execution_count": 13, 2858 | "metadata": {}, 2859 | "output_type": "execute_result" 2860 | } 2861 | ], 2862 | "source": [ 2863 | "# セル4-2. モデルの定義と学習\n", 2864 | "\n", 2865 | "#ラベルと説明変数をNumpy Arrayとして用意\n", 2866 | "label = np.array(df_weather_custom['label'])\n", 2867 | "features = np.array(df_weather_custom[[\"ta_mean1\", \"th_mean7\", \"ta_cumsum2\", \"ta_cumsum3\"]])\n", 2868 | "\n", 2869 | "\n", 2870 | "# 2016年以前のデータを学習用に、2017年のデータをテスト用に分割\n", 2871 | "train_index = np.array(df_weather_custom['year'] <= 2016)\n", 2872 | "test_index = np.array(df_weather_custom['year'] > 2016)\n", 2873 | "\n", 2874 | "# スケールの異なる説明変数を含むため標準化を行う\n", 2875 | "sc = StandardScaler()\n", 2876 | "sc.fit(features[train_index])\n", 2877 | "features = sc.transform(features)\n", 2878 | "\n", 2879 | "#ロジスティック回帰モデルを学習\n", 2880 | "classifier = LogisticRegression(C=1.0, penalty='l2')\n", 2881 | "classifier.fit(features[train_index], label[train_index]) " 2882 | ] 2883 | }, 2884 | { 2885 | "cell_type": "code", 2886 | "execution_count": 14, 2887 | "metadata": {}, 2888 | "outputs": [], 2889 | "source": [ 2890 | "# セル4-3. テストデータでの予測\n", 2891 | "df_prediction = df_weather_custom[test_index]\n", 2892 | "# SettingWithCopyWarningを出さないための設定\n", 2893 | "df_prediction.is_copy = False\n", 2894 | "df_prediction['prediction'] = classifier.predict(features[test_index])" 2895 | ] 2896 | }, 2897 | { 2898 | "cell_type": "code", 2899 | "execution_count": 15, 2900 | "metadata": { 2901 | "scrolled": true 2902 | }, 2903 | "outputs": [ 2904 | { 2905 | "data": { 2906 | "text/html": [ 2907 | "
\n", 2908 | "\n", 2921 | "\n", 2922 | " \n", 2923 | " \n", 2924 | " \n", 2925 | " \n", 2926 | " \n", 2927 | " \n", 2928 | " \n", 2929 | " \n", 2930 | " \n", 2931 | " \n", 2932 | " \n", 2933 | " \n", 2934 | " \n", 2935 | " \n", 2936 | " \n", 2937 | " \n", 2938 | " \n", 2939 | " \n", 2940 | " \n", 2941 | " \n", 2942 | " \n", 2943 | " \n", 2944 | " \n", 2945 | " \n", 2946 | " \n", 2947 | " \n", 2948 | " \n", 2949 | " \n", 2950 | " \n", 2951 | " \n", 2952 | " \n", 2953 | " \n", 2954 | " \n", 2955 | " \n", 2956 | " \n", 2957 | " \n", 2958 | " \n", 2959 | " \n", 2960 | " \n", 2961 | " \n", 2962 | " \n", 2963 | " \n", 2964 | " \n", 2965 | " \n", 2966 | " \n", 2967 | " \n", 2968 | "
datelocation_namelabelprediction
2017-03-282017-03-28奈良0.00.0
2017-03-292017-03-29奈良0.00.0
2017-03-302017-03-30奈良0.01.0
2017-03-312017-03-31奈良1.01.0
2017-04-012017-04-01奈良1.01.0
\n", 2969 | "
" 2970 | ], 2971 | "text/plain": [ 2972 | " date location_name label prediction\n", 2973 | "2017-03-28 2017-03-28 奈良 0.0 0.0\n", 2974 | "2017-03-29 2017-03-29 奈良 0.0 0.0\n", 2975 | "2017-03-30 2017-03-30 奈良 0.0 1.0\n", 2976 | "2017-03-31 2017-03-31 奈良 1.0 1.0\n", 2977 | "2017-04-01 2017-04-01 奈良 1.0 1.0" 2978 | ] 2979 | }, 2980 | "execution_count": 15, 2981 | "metadata": {}, 2982 | "output_type": "execute_result" 2983 | } 2984 | ], 2985 | "source": [ 2986 | "# セル4-4. 予測結果の表示\n", 2987 | "df_prediction[df_prediction.location_name == '奈良'][['date', 'location_name', 'label', 'prediction']].iloc[86:91]" 2988 | ] 2989 | }, 2990 | { 2991 | "cell_type": "code", 2992 | "execution_count": 16, 2993 | "metadata": { 2994 | "scrolled": false 2995 | }, 2996 | "outputs": [ 2997 | { 2998 | "data": { 2999 | "text/html": [ 3000 | "
\n", 3001 | "\n", 3014 | "\n", 3015 | " \n", 3016 | " \n", 3017 | " \n", 3018 | " \n", 3019 | " \n", 3020 | " \n", 3021 | " \n", 3022 | " \n", 3023 | " \n", 3024 | " \n", 3025 | " \n", 3026 | " \n", 3027 | " \n", 3028 | " \n", 3029 | " \n", 3030 | " \n", 3031 | " \n", 3032 | " \n", 3033 | " \n", 3034 | " \n", 3035 | " \n", 3036 | " \n", 3037 | " \n", 3038 | " \n", 3039 | " \n", 3040 | " \n", 3041 | " \n", 3042 | " \n", 3043 | " \n", 3044 | " \n", 3045 | " \n", 3046 | " \n", 3047 | " \n", 3048 | " \n", 3049 | " \n", 3050 | " \n", 3051 | " \n", 3052 | " \n", 3053 | " \n", 3054 | " \n", 3055 | " \n", 3056 | " \n", 3057 | " \n", 3058 | " \n", 3059 | " \n", 3060 | " \n", 3061 | " \n", 3062 | " \n", 3063 | " \n", 3064 | " \n", 3065 | " \n", 3066 | " \n", 3067 | " \n", 3068 | " \n", 3069 | " \n", 3070 | " \n", 3071 | " \n", 3072 | " \n", 3073 | " \n", 3074 | " \n", 3075 | " \n", 3076 | " \n", 3077 | " \n", 3078 | " \n", 3079 | " \n", 3080 | " \n", 3081 | " \n", 3082 | " \n", 3083 | " \n", 3084 | " \n", 3085 | " \n", 3086 | " \n", 3087 | " \n", 3088 | " \n", 3089 | " \n", 3090 | " \n", 3091 | " \n", 3092 | " \n", 3093 | " \n", 3094 | " \n", 3095 | " \n", 3096 | " \n", 3097 | " \n", 3098 | " \n", 3099 | " \n", 3100 | " \n", 3101 | " \n", 3102 | " \n", 3103 | " \n", 3104 | " \n", 3105 | " \n", 3106 | " \n", 3107 | " \n", 3108 | " \n", 3109 | " \n", 3110 | " \n", 3111 | " \n", 3112 | " \n", 3113 | " \n", 3114 | " \n", 3115 | " \n", 3116 | " \n", 3117 | " \n", 3118 | " \n", 3119 | " \n", 3120 | " \n", 3121 | " \n", 3122 | " \n", 3123 | " \n", 3124 | " \n", 3125 | " \n", 3126 | " \n", 3127 | " \n", 3128 | " \n", 3129 | " \n", 3130 | " \n", 3131 | " \n", 3132 | " \n", 3133 | " \n", 3134 | " \n", 3135 | " \n", 3136 | " \n", 3137 | " \n", 3138 | " \n", 3139 | " \n", 3140 | " \n", 3141 | " \n", 3142 | " \n", 3143 | " \n", 3144 | " \n", 3145 | " \n", 3146 | " \n", 3147 | " \n", 3148 | " \n", 3149 | " \n", 3150 | " \n", 3151 | " \n", 3152 | " \n", 3153 | " \n", 3154 | " \n", 3155 | " \n", 3156 | " \n", 3157 | " \n", 3158 | " \n", 3159 | " \n", 3160 | " \n", 3161 | " \n", 3162 | " \n", 3163 | " \n", 3164 | " \n", 3165 | " \n", 3166 | " \n", 3167 | " \n", 3168 | " \n", 3169 | " \n", 3170 | " \n", 3171 | " \n", 3172 | " \n", 3173 | " \n", 3174 | " \n", 3175 | " \n", 3176 | " \n", 3177 | " \n", 3178 | " \n", 3179 | " \n", 3180 | " \n", 3181 | " \n", 3182 | " \n", 3183 | " \n", 3184 | " \n", 3185 | " \n", 3186 | " \n", 3187 | " \n", 3188 | " \n", 3189 | " \n", 3190 | " \n", 3191 | " \n", 3192 | " \n", 3193 | " \n", 3194 | " \n", 3195 | " \n", 3196 | " \n", 3197 | " \n", 3198 | " \n", 3199 | " \n", 3200 | " \n", 3201 | " \n", 3202 | " \n", 3203 | " \n", 3204 | " \n", 3205 | " \n", 3206 | " \n", 3207 | " \n", 3208 | " \n", 3209 | " \n", 3210 | " \n", 3211 | " \n", 3212 | " \n", 3213 | " \n", 3214 | " \n", 3215 | " \n", 3216 | " \n", 3217 | " \n", 3218 | " \n", 3219 | " \n", 3220 | " \n", 3221 | " \n", 3222 | " \n", 3223 | " \n", 3224 | " \n", 3225 | " \n", 3226 | " \n", 3227 | " \n", 3228 | " \n", 3229 | " \n", 3230 | " \n", 3231 | " \n", 3232 | " \n", 3233 | " \n", 3234 | " \n", 3235 | " \n", 3236 | " \n", 3237 | " \n", 3238 | " \n", 3239 | " \n", 3240 | " \n", 3241 | " \n", 3242 | " \n", 3243 | " \n", 3244 | " \n", 3245 | " \n", 3246 | " \n", 3247 | " \n", 3248 | " \n", 3249 | " \n", 3250 | " \n", 3251 | " \n", 3252 | " \n", 3253 | " \n", 3254 | " \n", 3255 | " \n", 3256 | " \n", 3257 | " \n", 3258 | " \n", 3259 | " \n", 3260 | " \n", 3261 | " \n", 3262 | " \n", 3263 | " \n", 3264 | " \n", 3265 | " \n", 3266 | " \n", 3267 | " \n", 3268 | " \n", 3269 | " \n", 3270 | " \n", 3271 | " \n", 3272 | " \n", 3273 | " \n", 3274 | " \n", 3275 | " \n", 3276 | " \n", 3277 | " \n", 3278 | " \n", 3279 | " \n", 3280 | " \n", 3281 | " \n", 3282 | " \n", 3283 | " \n", 3284 | " \n", 3285 | " \n", 3286 | " \n", 3287 | " \n", 3288 | " \n", 3289 | " \n", 3290 | " \n", 3291 | " \n", 3292 | " \n", 3293 | " \n", 3294 | " \n", 3295 | " \n", 3296 | " \n", 3297 | " \n", 3298 | " \n", 3299 | " \n", 3300 | " \n", 3301 | " \n", 3302 | " \n", 3303 | " \n", 3304 | " \n", 3305 | " \n", 3306 | " \n", 3307 | " \n", 3308 | " \n", 3309 | " \n", 3310 | " \n", 3311 | " \n", 3312 | " \n", 3313 | " \n", 3314 | " \n", 3315 | " \n", 3316 | " \n", 3317 | " \n", 3318 | " \n", 3319 | " \n", 3320 | " \n", 3321 | " \n", 3322 | " \n", 3323 | " \n", 3324 | " \n", 3325 | " \n", 3326 | " \n", 3327 | " \n", 3328 | " \n", 3329 | " \n", 3330 | " \n", 3331 | " \n", 3332 | " \n", 3333 | " \n", 3334 | " \n", 3335 | " \n", 3336 | " \n", 3337 | " \n", 3338 | " \n", 3339 | " \n", 3340 | " \n", 3341 | " \n", 3342 | " \n", 3343 | " \n", 3344 | " \n", 3345 | " \n", 3346 | " \n", 3347 | " \n", 3348 | " \n", 3349 | " \n", 3350 | " \n", 3351 | " \n", 3352 | " \n", 3353 | " \n", 3354 | " \n", 3355 | " \n", 3356 | " \n", 3357 | " \n", 3358 | " \n", 3359 | " \n", 3360 | " \n", 3361 | " \n", 3362 | " \n", 3363 | " \n", 3364 | " \n", 3365 | " \n", 3366 | " \n", 3367 | " \n", 3368 | " \n", 3369 | " \n", 3370 | " \n", 3371 | " \n", 3372 | " \n", 3373 | " \n", 3374 | " \n", 3375 | " \n", 3376 | " \n", 3377 | " \n", 3378 | " \n", 3379 | " \n", 3380 | " \n", 3381 | " \n", 3382 | " \n", 3383 | " \n", 3384 | " \n", 3385 | " \n", 3386 | " \n", 3387 | " \n", 3388 | " \n", 3389 | " \n", 3390 | " \n", 3391 | " \n", 3392 | " \n", 3393 | " \n", 3394 | " \n", 3395 | " \n", 3396 | " \n", 3397 | " \n", 3398 | " \n", 3399 | " \n", 3400 | " \n", 3401 | " \n", 3402 | " \n", 3403 | " \n", 3404 | "
label2017prediction2017diff2017diff2017_days
location_name
下関2017-03-262017-03-233 days3
京都2017-03-282017-03-280 days0
仙台2017-04-042017-04-06-2 days-2
佐賀2017-03-272017-03-225 days5
函館2017-04-242017-04-213 days3
前橋2017-03-302017-03-300 days0
名古屋2017-03-252017-03-250 days0
和歌山2017-03-272017-03-252 days2
大分2017-04-012017-03-248 days8
大阪2017-03-272017-03-243 days3
奈良2017-03-312017-03-301 days1
宇都宮2017-03-312017-04-01-1 days-1
室蘭2017-04-292017-04-290 days0
宮崎2017-03-302017-03-219 days9
富山2017-04-022017-04-020 days0
山形2017-04-112017-04-101 days1
岐阜2017-03-252017-03-26-1 days-1
岡山2017-03-292017-03-272 days2
帯広2017-04-282017-04-30-2 days-2
広島2017-03-242017-03-240 days0
彦根2017-04-022017-04-011 days1
徳島2017-03-312017-03-247 days7
新潟2017-04-052017-04-050 days0
旭川2017-04-302017-05-03-3 days-3
札幌2017-04-252017-04-26-1 days-1
東京2017-03-182017-03-22-4 days-4
松山2017-03-272017-03-234 days4
松江2017-03-292017-03-290 days0
横浜2017-03-222017-03-23-1 days-1
水戸2017-03-312017-04-01-1 days-1
2017-03-312017-03-283 days3
熊本2017-03-292017-03-218 days8
熊谷2017-03-252017-03-250 days0
甲府2017-03-272017-03-252 days2
盛岡2017-04-142017-04-17-3 days-3
神戸2017-03-312017-03-256 days6
福井2017-04-022017-04-011 days1
福岡2017-03-222017-03-211 days1
福島2017-04-052017-04-050 days0
秋田2017-04-132017-04-103 days3
稚内2017-05-062017-05-060 days0
網走2017-05-022017-05-05-3 days-3
金沢2017-04-012017-03-311 days1
釧路2017-05-112017-05-110 days0
銚子2017-03-292017-03-263 days3
長崎2017-03-272017-03-225 days5
長野2017-04-112017-04-092 days2
青森2017-04-142017-04-16-2 days-2
静岡2017-03-302017-03-237 days7
高松2017-03-302017-03-255 days5
高知2017-03-262017-03-215 days5
鳥取2017-03-302017-03-300 days0
鹿児島2017-04-022017-03-2013 days13
\n", 3405 | "
" 3406 | ], 3407 | "text/plain": [ 3408 | " label2017 prediction2017 diff2017 diff2017_days\n", 3409 | "location_name \n", 3410 | "下関 2017-03-26 2017-03-23 3 days 3\n", 3411 | "京都 2017-03-28 2017-03-28 0 days 0\n", 3412 | "仙台 2017-04-04 2017-04-06 -2 days -2\n", 3413 | "佐賀 2017-03-27 2017-03-22 5 days 5\n", 3414 | "函館 2017-04-24 2017-04-21 3 days 3\n", 3415 | "前橋 2017-03-30 2017-03-30 0 days 0\n", 3416 | "名古屋 2017-03-25 2017-03-25 0 days 0\n", 3417 | "和歌山 2017-03-27 2017-03-25 2 days 2\n", 3418 | "大分 2017-04-01 2017-03-24 8 days 8\n", 3419 | "大阪 2017-03-27 2017-03-24 3 days 3\n", 3420 | "奈良 2017-03-31 2017-03-30 1 days 1\n", 3421 | "宇都宮 2017-03-31 2017-04-01 -1 days -1\n", 3422 | "室蘭 2017-04-29 2017-04-29 0 days 0\n", 3423 | "宮崎 2017-03-30 2017-03-21 9 days 9\n", 3424 | "富山 2017-04-02 2017-04-02 0 days 0\n", 3425 | "山形 2017-04-11 2017-04-10 1 days 1\n", 3426 | "岐阜 2017-03-25 2017-03-26 -1 days -1\n", 3427 | "岡山 2017-03-29 2017-03-27 2 days 2\n", 3428 | "帯広 2017-04-28 2017-04-30 -2 days -2\n", 3429 | "広島 2017-03-24 2017-03-24 0 days 0\n", 3430 | "彦根 2017-04-02 2017-04-01 1 days 1\n", 3431 | "徳島 2017-03-31 2017-03-24 7 days 7\n", 3432 | "新潟 2017-04-05 2017-04-05 0 days 0\n", 3433 | "旭川 2017-04-30 2017-05-03 -3 days -3\n", 3434 | "札幌 2017-04-25 2017-04-26 -1 days -1\n", 3435 | "東京 2017-03-18 2017-03-22 -4 days -4\n", 3436 | "松山 2017-03-27 2017-03-23 4 days 4\n", 3437 | "松江 2017-03-29 2017-03-29 0 days 0\n", 3438 | "横浜 2017-03-22 2017-03-23 -1 days -1\n", 3439 | "水戸 2017-03-31 2017-04-01 -1 days -1\n", 3440 | "津 2017-03-31 2017-03-28 3 days 3\n", 3441 | "熊本 2017-03-29 2017-03-21 8 days 8\n", 3442 | "熊谷 2017-03-25 2017-03-25 0 days 0\n", 3443 | "甲府 2017-03-27 2017-03-25 2 days 2\n", 3444 | "盛岡 2017-04-14 2017-04-17 -3 days -3\n", 3445 | "神戸 2017-03-31 2017-03-25 6 days 6\n", 3446 | "福井 2017-04-02 2017-04-01 1 days 1\n", 3447 | "福岡 2017-03-22 2017-03-21 1 days 1\n", 3448 | "福島 2017-04-05 2017-04-05 0 days 0\n", 3449 | "秋田 2017-04-13 2017-04-10 3 days 3\n", 3450 | "稚内 2017-05-06 2017-05-06 0 days 0\n", 3451 | "網走 2017-05-02 2017-05-05 -3 days -3\n", 3452 | "金沢 2017-04-01 2017-03-31 1 days 1\n", 3453 | "釧路 2017-05-11 2017-05-11 0 days 0\n", 3454 | "銚子 2017-03-29 2017-03-26 3 days 3\n", 3455 | "長崎 2017-03-27 2017-03-22 5 days 5\n", 3456 | "長野 2017-04-11 2017-04-09 2 days 2\n", 3457 | "青森 2017-04-14 2017-04-16 -2 days -2\n", 3458 | "静岡 2017-03-30 2017-03-23 7 days 7\n", 3459 | "高松 2017-03-30 2017-03-25 5 days 5\n", 3460 | "高知 2017-03-26 2017-03-21 5 days 5\n", 3461 | "鳥取 2017-03-30 2017-03-30 0 days 0\n", 3462 | "鹿児島 2017-04-02 2017-03-20 13 days 13" 3463 | ] 3464 | }, 3465 | "execution_count": 16, 3466 | "metadata": {}, 3467 | "output_type": "execute_result" 3468 | } 3469 | ], 3470 | "source": [ 3471 | "# セル4-5. モデルの評価\n", 3472 | "\n", 3473 | "# 実測値(label)および予測結果(prediction)が開花(1)になっている日付をそれぞれ取得する\n", 3474 | "label2017 = df_prediction[df_prediction.label == 1].groupby(['location_name']).min()['date'].astype('datetime64[ns]')\n", 3475 | "prediction2017 = df_prediction[df_prediction.prediction == 1].groupby(['location_name']).min()['date'].astype('datetime64[ns]')\n", 3476 | "\n", 3477 | "# 観測所名(location_name)をIndexとして実測値と予測値の差を計算する\n", 3478 | "prediction_summary = pd.concat([label2017,prediction2017],axis=1)\n", 3479 | "prediction_summary.columns = ['label2017', 'prediction2017']\n", 3480 | "prediction_summary['diff2017'] = pd.to_timedelta(prediction_summary['label2017'] - prediction_summary['prediction2017'])\n", 3481 | "prediction_summary['diff2017_days'] = (prediction_summary['diff2017'] / np.timedelta64(1, 'D')).astype(int)\n", 3482 | "prediction_summary" 3483 | ] 3484 | }, 3485 | { 3486 | "cell_type": "code", 3487 | "execution_count": 17, 3488 | "metadata": { 3489 | "scrolled": false 3490 | }, 3491 | "outputs": [ 3492 | { 3493 | "data": { 3494 | "text/html": [ 3495 | "\n", 3510 | "\n", 3511 | "\n", 3512 | "\n", 3513 | "\n", 3514 | "\n", 3515 | "\n", 3518 | "\n", 3519 | "
\n", 3520 | "" 3521 | ], 3522 | "text/plain": [ 3523 | "" 3524 | ] 3525 | }, 3526 | "metadata": {}, 3527 | "output_type": "display_data" 3528 | }, 3529 | { 3530 | "data": { 3531 | "application/javascript": [ 3532 | "/*\n", 3533 | " * Copyright (c) 2015 IBM Corporation and others.\n", 3534 | " *\n", 3535 | " * Licensed under the Apache License, Version 2.0 (the \"License\");\n", 3536 | " * You may not use this file except in compliance with the License.\n", 3537 | " * You may obtain a copy of the License at\n", 3538 | " *\n", 3539 | " * http://www.apache.org/licenses/LICENSE-2.0\n", 3540 | " *\n", 3541 | " * Unless required by applicable law or agreed to in writing, software\n", 3542 | " * distributed under the License is distributed on an \"AS IS\" BASIS,\n", 3543 | " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", 3544 | " * See the License for the specific language governing permissions and\n", 3545 | " * limitations under the License.\n", 3546 | " */\n", 3547 | "\n", 3548 | "require.config({\n", 3549 | " waitSeconds: 60,\n", 3550 | " paths: {\n", 3551 | " 'd3': '//cdnjs.cloudflare.com/ajax/libs/d3/4.2.1/d3.min',\n", 3552 | " 'topojson': '//cdnjs.cloudflare.com/ajax/libs/topojson/1.6.20/topojson.min',\n", 3553 | " 'brunel' : '/data/jupyter2/static-file-content-delivery-network/nbextensions/brunel_ext/brunel.2.3.min',\n", 3554 | " 'brunelControls' : '/data/jupyter2/static-file-content-delivery-network/nbextensions/brunel_ext/brunel.controls.2.3.min'\n", 3555 | " },\n", 3556 | " shim: {\n", 3557 | " 'brunel' : {\n", 3558 | " exports: 'BrunelD3',\n", 3559 | " deps: ['d3', 'topojson'],\n", 3560 | " init: function() {\n", 3561 | " return {\n", 3562 | " BrunelD3 : BrunelD3,\n", 3563 | " BrunelData : BrunelData\n", 3564 | " }\n", 3565 | " }\n", 3566 | " },\n", 3567 | " 'brunelControls' : {\n", 3568 | " exports: 'BrunelEventHandlers',\n", 3569 | " init: function() {\n", 3570 | " return {\n", 3571 | " BrunelEventHandlers: BrunelEventHandlers,\n", 3572 | " BrunelJQueryControlFactory: BrunelJQueryControlFactory\n", 3573 | " }\n", 3574 | " }\n", 3575 | " }\n", 3576 | "\n", 3577 | " }\n", 3578 | "\n", 3579 | "});\n", 3580 | "\n", 3581 | "require([\"d3\"], function(d3) {\n", 3582 | " require([\"brunel\", \"brunelControls\"], function(brunel, brunelControls) {\n", 3583 | " function BrunelVis(visId) {\n", 3584 | " \"use strict\"; // strict mode\n", 3585 | " var datasets = [], // array of datasets for the original data\n", 3586 | " pre = function(d, i) { return d }, // default pre-process does nothing\n", 3587 | " post = function(d, i) { return d }, // default post-process does nothing\n", 3588 | " transitionTime = 200, // transition time for animations\n", 3589 | " charts = [], // the charts in the system\n", 3590 | " vis = d3.select('#' + visId).attr('class', 'brunel'); // the SVG container\n", 3591 | "\n", 3592 | " BrunelD3.addDefinitions(vis); // ensure standard symbols present\n", 3593 | "\n", 3594 | " // Define chart #1 in the visualization //////////////////////////////////////////////////////////\n", 3595 | "\n", 3596 | " charts[0] = function(parentNode, filterRows) {\n", 3597 | " var geom = BrunelD3.geometry(parentNode || vis.node(), 0, 0, 1, 1, 0, 0, 0, 104),\n", 3598 | " elements = []; // array of elements in this chart\n", 3599 | "\n", 3600 | " // Define groups for the chart parts ///////////////////////////////////////////////////////////\n", 3601 | "\n", 3602 | " var chart = vis.append('g').attr('class', 'chart1')\n", 3603 | " .attr('transform','translate(' + geom.chart_left + ',' + geom.chart_top + ')');\n", 3604 | " var overlay = chart.append('g').attr('class', 'element').attr('class', 'overlay');\n", 3605 | " var zoom = d3.zoom().scaleExtent([1/5,5]);\n", 3606 | " var zoomNode = overlay.append('rect').attr('class', 'overlay')\n", 3607 | " .attr('x', geom.inner_left).attr('y', geom.inner_top)\n", 3608 | " .attr('width', geom.inner_rawWidth).attr('height', geom.inner_rawHeight)\n", 3609 | " .style('cursor', 'move').call(zoom)\n", 3610 | " .node();\n", 3611 | " zoomNode.__zoom = d3.zoomIdentity;\n", 3612 | " chart.append('rect').attr('class', 'background').attr('width', geom.chart_right-geom.chart_left).attr('height', geom.chart_bottom-geom.chart_top);\n", 3613 | " var interior = chart.append('g').attr('class', 'interior zoomNone')\n", 3614 | " .attr('transform','translate(' + geom.inner_left + ',' + geom.inner_top + ')')\n", 3615 | " .attr('clip-path', 'url(#clip_visidc1cfbad4-6981-11e8-ae66-3a5f3782500e_chart1_inner)');\n", 3616 | " interior.append('rect').attr('class', 'inner').attr('width', geom.inner_width).attr('height', geom.inner_height);\n", 3617 | " var gridGroup = interior.append('g').attr('class', 'grid');\n", 3618 | " var legends = chart.append('g').attr('class', 'legend')\n", 3619 | " .attr('transform','translate(' + (geom.chart_right-geom.chart_left - 3) + ',' + 0 + ')');\n", 3620 | " vis.append('clipPath').attr('id', 'clip_visidc1cfbad4-6981-11e8-ae66-3a5f3782500e_chart1_inner').append('rect')\n", 3621 | " .attr('x', 0).attr('y', 0)\n", 3622 | " .attr('width', geom.inner_rawWidth+1).attr('height', geom.inner_rawHeight+1);\n", 3623 | "\n", 3624 | " // Projection //////////////////////////////////////////////////////////////////////////////////\n", 3625 | "\n", 3626 | " var base = d3.geoMercator()\n", 3627 | " .translate([geom.inner_width/2, geom.inner_height/2])\n", 3628 | " .scale(Math.min((geom.inner_width-4)/0.3039, (geom.inner_height-4)/0.3739))\n", 3629 | " .center([137.1217, 38.9699])\n", 3630 | " function projection(p) {\n", 3631 | " var q = base(p), t = d3.zoomTransform(zoomNode);\n", 3632 | " return q ? [t.k*q[0]+t.x, t.k*q[1]+t.y] : null;\n", 3633 | " };\n", 3634 | " function project_center(v) { return (v ? projection([v.c, v.d]) : null) || [-9e6, -9e6] };\n", 3635 | " var path = d3.geoPath().projection(BrunelD3.geoStream(projection));\n", 3636 | " var scale_x = d3.scaleLinear(), scale_y = d3.scaleLinear();\n", 3637 | " var base_scales = [scale_x, scale_y]; // untransformed original scales\n", 3638 | " zoom.on('zoom', function(t, time) {\n", 3639 | " t = t || d3.event.transform;\n", 3640 | " zoomNode.__zoom = t;\n", 3641 | " interior.attr('class', 'interior ' + BrunelD3.zoomLabel(t.k));;\n", 3642 | " build(time || -1);\n", 3643 | " });\n", 3644 | "\n", 3645 | " // Define element #1 ///////////////////////////////////////////////////////////////////////////\n", 3646 | "\n", 3647 | " elements[0] = function() {\n", 3648 | " var original, processed, // data sets passed in and then transformed\n", 3649 | " element, data, // brunel element information and brunel data\n", 3650 | " selection, merged; // d3 selection and merged selection\n", 3651 | " var elementGroup = interior.append('g').attr('class', 'element1'),\n", 3652 | " main = elementGroup.append('g').attr('class', 'main'),\n", 3653 | " labels = BrunelD3.undoTransform(elementGroup.append('g').attr('class', 'labels').attr('aria-hidden', 'true'), elementGroup);\n", 3654 | "\n", 3655 | " function makeData() {\n", 3656 | " original = datasets[0];\n", 3657 | " if (filterRows) original = original.retainRows(filterRows);\n", 3658 | " processed = pre(original, 0);\n", 3659 | " processed = post(processed, 0);\n", 3660 | " var f0 = processed.field('#row'),\n", 3661 | " f1 = processed.field('#selection');\n", 3662 | " var keyFunc = function(d) { return f0.value(d) };\n", 3663 | " data = {\n", 3664 | " $row: function(d) { return f0.value(d.row) },\n", 3665 | " $selection: function(d) { return f1.value(d.row) },\n", 3666 | " $row_f: function(d) { return f0.valueFormatted(d.row) },\n", 3667 | " $selection_f: function(d) { return f1.valueFormatted(d.row) },\n", 3668 | " _split: function(d) { return 'ALL' },\n", 3669 | " _key: keyFunc,\n", 3670 | " _rows: BrunelD3.makeRowsWithKeys(keyFunc, processed.rowCount())\n", 3671 | " };\n", 3672 | " }\n", 3673 | "\n", 3674 | " // Build element from data ///////////////////////////////////////////////////////////////////\n", 3675 | "\n", 3676 | " function build(transitionMillis) {\n", 3677 | " element = elements[0];\n", 3678 | " // Read in the feature data and call build again when done\n", 3679 | " var features = {\n", 3680 | " 'https://brunelvis.org/geo/2.3/med/Japan.json': {\n", 3681 | " }\n", 3682 | " };\n", 3683 | " if (BrunelD3.addFeatures(data, features, null, null, this, transitionMillis)) return;\n", 3684 | " main.attr('class', 'diagram map');\n", 3685 | "\n", 3686 | " // Define selection entry operations\n", 3687 | " function initialState(selection) {\n", 3688 | " selection\n", 3689 | " .attr('class', 'element polygon filled')\n", 3690 | " .classed('nondata', function(d) {return !d || d.row == null})\n", 3691 | " .style('pointer-events', 'none')\n", 3692 | " }\n", 3693 | "\n", 3694 | " // Define selection update operations on merged data\n", 3695 | " function updateState(selection) {\n", 3696 | " selection\n", 3697 | " .attr('d', function(d) { return path(d).replace(/,L/g, 'L').replace(/,Z/g, 'Z') });\n", 3698 | " }\n", 3699 | " // Create selections, set the initial state and transition updates\n", 3700 | " selection = main.selectAll('.element').data(data._rows, function(d) { return d.key });\n", 3701 | " var added = selection.enter().append('path');\n", 3702 | " merged = selection.merge(added);\n", 3703 | " initialState(added);\n", 3704 | " selection.filter(BrunelD3.hasData)\n", 3705 | " .classed('selected', BrunelD3.isSelected(data))\n", 3706 | " .filter(BrunelD3.isSelected(data)).raise();\n", 3707 | " updateState(BrunelD3.transition(merged, transitionMillis));\n", 3708 | "\n", 3709 | " BrunelD3.transition(selection.exit(), transitionMillis/3)\n", 3710 | " .style('opacity', 0.5).each( function() {\n", 3711 | " this.remove(); BrunelD3.removeLabels(this); \n", 3712 | " });\n", 3713 | " }\n", 3714 | "\n", 3715 | " return {\n", 3716 | " data: function() { return processed },\n", 3717 | " original: function() { return original },\n", 3718 | " internal: function() { return data },\n", 3719 | " selection: function() { return merged },\n", 3720 | " makeData: makeData,\n", 3721 | " build: build,\n", 3722 | " chart: function() { return charts[0] },\n", 3723 | " group: function() { return elementGroup },\n", 3724 | " fields: {\n", 3725 | " key: ['#row']\n", 3726 | " }\n", 3727 | " };\n", 3728 | " }();\n", 3729 | "\n", 3730 | " // Define element #2 ///////////////////////////////////////////////////////////////////////////\n", 3731 | "\n", 3732 | " elements[1] = function() {\n", 3733 | " var original, processed, // data sets passed in and then transformed\n", 3734 | " element, data, // brunel element information and brunel data\n", 3735 | " selection, merged; // d3 selection and merged selection\n", 3736 | " var elementGroup = interior.append('g').attr('class', 'element2'),\n", 3737 | " main = elementGroup.append('g').attr('class', 'main'),\n", 3738 | " labels = BrunelD3.undoTransform(elementGroup.append('g').attr('class', 'labels').attr('aria-hidden', 'true'), elementGroup);\n", 3739 | "\n", 3740 | " function makeData() {\n", 3741 | " original = datasets[1];\n", 3742 | " if (filterRows) original = original.retainRows(filterRows);\n", 3743 | " processed = pre(original, 1);\n", 3744 | " processed = post(processed, 1);\n", 3745 | " var f0 = processed.field('longitude'),\n", 3746 | " f1 = processed.field('latitude'),\n", 3747 | " f2 = processed.field('diff2017_days'),\n", 3748 | " f3 = processed.field('location_name'),\n", 3749 | " f4 = processed.field('label2017'),\n", 3750 | " f5 = processed.field('prediction2017'),\n", 3751 | " f6 = processed.field('#row'),\n", 3752 | " f7 = processed.field('#selection');\n", 3753 | " var keyFunc = function(d) { return f6.value(d) };\n", 3754 | " data = {\n", 3755 | " longitude: function(d) { return f0.value(d.row) },\n", 3756 | " latitude: function(d) { return f1.value(d.row) },\n", 3757 | " diff2017_days:function(d) { return f2.value(d.row) },\n", 3758 | " location_name:function(d) { return f3.value(d.row) },\n", 3759 | " label2017: function(d) { return f4.value(d.row) },\n", 3760 | " prediction2017:function(d) { return f5.value(d.row) },\n", 3761 | " $row: function(d) { return f6.value(d.row) },\n", 3762 | " $selection: function(d) { return f7.value(d.row) },\n", 3763 | " longitude_f: function(d) { return f0.valueFormatted(d.row) },\n", 3764 | " latitude_f: function(d) { return f1.valueFormatted(d.row) },\n", 3765 | " diff2017_days_f:function(d) { return f2.valueFormatted(d.row) },\n", 3766 | " location_name_f:function(d) { return f3.valueFormatted(d.row) },\n", 3767 | " label2017_f: function(d) { return f4.valueFormatted(d.row) },\n", 3768 | " prediction2017_f:function(d) { return f5.valueFormatted(d.row) },\n", 3769 | " $row_f: function(d) { return f6.valueFormatted(d.row) },\n", 3770 | " $selection_f: function(d) { return f7.valueFormatted(d.row) },\n", 3771 | " _split: function(d) { return f2.value(d.row) },\n", 3772 | " _key: keyFunc,\n", 3773 | " _rows: BrunelD3.makeRowsWithKeys(keyFunc, processed.rowCount())\n", 3774 | " };\n", 3775 | " }\n", 3776 | " // Aesthetic Functions\n", 3777 | " var scale_color = d3.scaleLinear().domain([-4, -1.875, 0.25, 2.375, 4.5, 6.625, 8.75, 10.875, 13])\n", 3778 | " .interpolate(d3.interpolateHcl)\n", 3779 | " .range([ '#045a8d', '#2b8cbe', '#74a9cf', '#bdc9e1', '#f8efe8', '#fef0d9', \n", 3780 | " '#fdcc8a', '#fc8d59', '#e34a33']);\n", 3781 | " var color = function(d) { return scale_color(data.diff2017_days(d)) };\n", 3782 | " legends._legend = legends._legend || { title: ['Diff 2017 Days'], \n", 3783 | " ticks: [15, 10, 5, 0, -5]};\n", 3784 | " legends._legend.color = scale_color;\n", 3785 | "\n", 3786 | " // Build element from data ///////////////////////////////////////////////////////////////////\n", 3787 | "\n", 3788 | " function build(transitionMillis) {\n", 3789 | " element = elements[1];\n", 3790 | " var w = geom.default_point_size;\n", 3791 | " var x = function(d) { return projection([data.longitude(d),data.latitude(d)])[0]};\n", 3792 | " var h = geom.default_point_size;\n", 3793 | " var y = function(d) { return projection([data.longitude(d),data.latitude(d)])[1]};\n", 3794 | " var labeling = [{\n", 3795 | " index: 0, method: 'box', location: ['right', 'center'], inside: false, align: 'start', pad: 3, dy: 0.3,\n", 3796 | " fit: false, granularity: 2,\n", 3797 | " content: function(d) {\n", 3798 | " return d.row == null ? null : data.diff2017_days_f(d)\n", 3799 | " }\n", 3800 | " }];\n", 3801 | "\n", 3802 | " // Define selection entry operations\n", 3803 | " function initialState(selection) {\n", 3804 | " selection\n", 3805 | " .attr('class', 'element point filled')\n", 3806 | " }\n", 3807 | "\n", 3808 | " // Define selection update operations on merged data\n", 3809 | " function updateState(selection) {\n", 3810 | " selection\n", 3811 | " .attr('cx',function(d) { return projection([data.longitude(d),data.latitude(d)])[0]})\n", 3812 | " .attr('cy',function(d) { return projection([data.longitude(d),data.latitude(d)])[1]})\n", 3813 | " .attr('r',geom.default_point_size / 2)\n", 3814 | " .filter(BrunelD3.hasData) // following only performed for data items\n", 3815 | " .style('fill', color);\n", 3816 | " }\n", 3817 | "\n", 3818 | " // Define labeling for the selection\n", 3819 | " function label(selection, transitionMillis) {\n", 3820 | " BrunelD3.label(selection, labels, transitionMillis, geom, labeling);\n", 3821 | "\n", 3822 | " var tooltipLabeling = {\n", 3823 | " index: -1, method: 'box', location: ['center', 'top'], inside: true, align: 'middle', pad: 0, dy: 0.7,\n", 3824 | " fit: true, granularity: 0,\n", 3825 | " content: function(d) {\n", 3826 | " return d.row == null ? null : 'Location Name: '\n", 3827 | "\t\t\t+ '' + data.location_name_f(d) + ''\n", 3828 | "\t\t\t+ '
'\n", 3829 | "\t\t\t+ 'Label 2017: '\n", 3830 | "\t\t\t+ '' + data.label2017_f(d) + ''\n", 3831 | "\t\t\t+ '
'\n", 3832 | "\t\t\t+ 'Prediction 2017: '\n", 3833 | "\t\t\t+ '' + data.prediction2017_f(d) + ''\n", 3834 | " }\n", 3835 | " };\n", 3836 | " BrunelD3.addTooltip(selection, tooltipLabeling, geom);\n", 3837 | " }\n", 3838 | " // Create selections, set the initial state and transition updates\n", 3839 | " selection = main.selectAll('.element').data(data._rows, function(d) { return d.key });\n", 3840 | " var added = selection.enter().append('circle');\n", 3841 | " merged = selection.merge(added);\n", 3842 | " initialState(added);\n", 3843 | " selection.filter(BrunelD3.hasData)\n", 3844 | " .classed('selected', BrunelD3.isSelected(data))\n", 3845 | " .filter(BrunelD3.isSelected(data)).raise();\n", 3846 | " updateState(BrunelD3.transition(merged, transitionMillis));\n", 3847 | " label(merged, transitionMillis);\n", 3848 | "\n", 3849 | " BrunelD3.transition(selection.exit(), transitionMillis/3)\n", 3850 | " .style('opacity', 0.5).each( function() {\n", 3851 | " this.remove(); BrunelD3.removeLabels(this); \n", 3852 | " });\n", 3853 | " }\n", 3854 | "\n", 3855 | " return {\n", 3856 | " data: function() { return processed },\n", 3857 | " original: function() { return original },\n", 3858 | " internal: function() { return data },\n", 3859 | " selection: function() { return merged },\n", 3860 | " makeData: makeData,\n", 3861 | " build: build,\n", 3862 | " chart: function() { return charts[0] },\n", 3863 | " group: function() { return elementGroup },\n", 3864 | " fields: {\n", 3865 | " x: ['longitude'],\n", 3866 | " y: ['latitude'],\n", 3867 | " key: ['#row'],\n", 3868 | " color: ['diff2017_days']\n", 3869 | " }\n", 3870 | " };\n", 3871 | " }();\n", 3872 | "\n", 3873 | " function build(time, noData) {\n", 3874 | " var first = elements[0].data() == null;\n", 3875 | " if (first) time = 0; // no transition for first call\n", 3876 | " if ((first || time > -1) && !noData) {\n", 3877 | " elements[0].makeData();\n", 3878 | " elements[1].makeData();\n", 3879 | " BrunelD3.addLegend(legends, legends._legend);\n", 3880 | " }\n", 3881 | " elements[0].build(time);\n", 3882 | " elements[1].build(time);\n", 3883 | " }\n", 3884 | "\n", 3885 | " // Expose the following components of the chart\n", 3886 | " return {\n", 3887 | " elements : elements,\n", 3888 | " interior : interior,\n", 3889 | " zoom: function(params, time) {\n", 3890 | " if (params) zoom.on('zoom').call(zoomNode, params, time);\n", 3891 | " return d3.zoomTransform(zoomNode);\n", 3892 | " },\n", 3893 | " build : build\n", 3894 | " };\n", 3895 | " }();\n", 3896 | "\n", 3897 | " function setData(rowData, i) { datasets[i||0] = BrunelD3.makeData(rowData) }\n", 3898 | " function updateAll(time) { charts.forEach(function(x) {x.build(time || 0)}) }\n", 3899 | " function buildAll() {\n", 3900 | " for (var i=0;i" 3992 | ] 3993 | }, 3994 | "execution_count": 17, 3995 | "metadata": {}, 3996 | "output_type": "execute_result" 3997 | } 3998 | ], 3999 | "source": [ 4000 | "# セル4-6. 地図上での誤差の表示\n", 4001 | "\n", 4002 | "# 予測の誤差と観測所の位置情報を結合する\n", 4003 | "prediction_summary = pd.concat([prediction_summary, df_station], axis=1)\n", 4004 | "prediction_summary.head()\n", 4005 | "\n", 4006 | "# 地図上に予測の誤差を表示する\n", 4007 | "%brunel map('japan') + data('prediction_summary') x(longitude) y(latitude) \\\n", 4008 | "tooltip(location_name,label2017,prediction2017) color(diff2017_days) \\\n", 4009 | "label(diff2017_days) ::width=1200, height=600 " 4010 | ] 4011 | }, 4012 | { 4013 | "cell_type": "code", 4014 | "execution_count": null, 4015 | "metadata": {}, 4016 | "outputs": [], 4017 | "source": [] 4018 | } 4019 | ], 4020 | "metadata": { 4021 | "kernelspec": { 4022 | "display_name": "Python 3.5", 4023 | "language": "python", 4024 | "name": "python3" 4025 | }, 4026 | "language_info": { 4027 | "codemirror_mode": { 4028 | "name": "ipython", 4029 | "version": 3 4030 | }, 4031 | "file_extension": ".py", 4032 | "mimetype": "text/x-python", 4033 | "name": "python", 4034 | "nbconvert_exporter": "python", 4035 | "pygments_lexer": "ipython3", 4036 | "version": "3.5.4" 4037 | } 4038 | }, 4039 | "nbformat": 4, 4040 | "nbformat_minor": 1 4041 | } 4042 | --------------------------------------------------------------------------------