├── 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 | " location_name | \n",
45 | " prec_id | \n",
46 | " block_id | \n",
47 | " latitude | \n",
48 | " longitude | \n",
49 | " height | \n",
50 | "
\n",
51 | " \n",
52 | " \n",
53 | " \n",
54 | " | 0 | \n",
55 | " 稚内 | \n",
56 | " 11 | \n",
57 | " 47401 | \n",
58 | " 45.4150 | \n",
59 | " 141.6783 | \n",
60 | " 2.8 | \n",
61 | "
\n",
62 | " \n",
63 | " | 1 | \n",
64 | " 旭川 | \n",
65 | " 12 | \n",
66 | " 47407 | \n",
67 | " 43.7567 | \n",
68 | " 142.3717 | \n",
69 | " 120.0 | \n",
70 | "
\n",
71 | " \n",
72 | " | 2 | \n",
73 | " 札幌 | \n",
74 | " 14 | \n",
75 | " 47412 | \n",
76 | " 43.0600 | \n",
77 | " 141.3283 | \n",
78 | " 17.2 | \n",
79 | "
\n",
80 | " \n",
81 | " | 3 | \n",
82 | " 網走 | \n",
83 | " 17 | \n",
84 | " 47409 | \n",
85 | " 44.0167 | \n",
86 | " 144.2783 | \n",
87 | " 37.6 | \n",
88 | "
\n",
89 | " \n",
90 | " | 4 | \n",
91 | " 釧路 | \n",
92 | " 19 | \n",
93 | " 47418 | \n",
94 | " 42.9850 | \n",
95 | " 144.3767 | \n",
96 | " 4.5 | \n",
97 | "
\n",
98 | " \n",
99 | "
\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 | " location_name | \n",
170 | " y2007 | \n",
171 | " y2008 | \n",
172 | " y2009 | \n",
173 | " y2010 | \n",
174 | " y2011 | \n",
175 | " y2012 | \n",
176 | " y2013 | \n",
177 | " y2014 | \n",
178 | " y2015 | \n",
179 | " y2016 | \n",
180 | " y2017 | \n",
181 | " prec_id | \n",
182 | " block_id | \n",
183 | "
\n",
184 | " \n",
185 | " \n",
186 | " \n",
187 | " | 0 | \n",
188 | " 旭川 | \n",
189 | " 2007-05-06 | \n",
190 | " 2008-04-23 | \n",
191 | " 2009-05-03 | \n",
192 | " 2010-05-11 | \n",
193 | " 2011-05-09 | \n",
194 | " 2012-05-02 | \n",
195 | " 2013-05-18 | \n",
196 | " 2014-05-02 | \n",
197 | " 2015-04-27 | \n",
198 | " 2016-05-03 | \n",
199 | " 2017-05-03 | \n",
200 | " 12 | \n",
201 | " 47407 | \n",
202 | "
\n",
203 | " \n",
204 | " | 1 | \n",
205 | " 宇都宮 | \n",
206 | " 2007-03-26 | \n",
207 | " 2008-03-28 | \n",
208 | " 2009-03-26 | \n",
209 | " 2010-04-02 | \n",
210 | " 2011-04-06 | \n",
211 | " 2012-04-08 | \n",
212 | " 2013-03-21 | \n",
213 | " 2014-03-29 | \n",
214 | " 2015-03-30 | \n",
215 | " 2016-03-28 | \n",
216 | " 2017-04-03 | \n",
217 | " 41 | \n",
218 | " 47615 | \n",
219 | "
\n",
220 | " \n",
221 | " | 2 | \n",
222 | " 横浜 | \n",
223 | " 2007-03-23 | \n",
224 | " 2008-03-23 | \n",
225 | " 2009-03-22 | \n",
226 | " 2010-03-22 | \n",
227 | " 2011-03-30 | \n",
228 | " 2012-04-02 | \n",
229 | " 2013-03-18 | \n",
230 | " 2014-03-25 | \n",
231 | " 2015-03-23 | \n",
232 | " 2016-03-23 | \n",
233 | " 2017-03-25 | \n",
234 | " 46 | \n",
235 | " 47670 | \n",
236 | "
\n",
237 | " \n",
238 | " | 3 | \n",
239 | " 岡山 | \n",
240 | " 2007-03-26 | \n",
241 | " 2008-03-26 | \n",
242 | " 2009-03-21 | \n",
243 | " 2010-03-22 | \n",
244 | " 2011-03-31 | \n",
245 | " 2012-04-03 | \n",
246 | " 2013-03-24 | \n",
247 | " 2014-03-28 | \n",
248 | " 2015-03-28 | \n",
249 | " 2016-03-26 | \n",
250 | " 2017-04-01 | \n",
251 | " 66 | \n",
252 | " 47768 | \n",
253 | "
\n",
254 | " \n",
255 | " | 4 | \n",
256 | " 下関 | \n",
257 | " 2007-03-23 | \n",
258 | " 2008-03-26 | \n",
259 | " 2009-03-18 | \n",
260 | " 2010-03-20 | \n",
261 | " 2011-03-30 | \n",
262 | " 2012-03-30 | \n",
263 | " 2013-03-19 | \n",
264 | " 2014-03-25 | \n",
265 | " 2015-03-25 | \n",
266 | " 2016-03-28 | \n",
267 | " 2017-03-29 | \n",
268 | " 81 | \n",
269 | " 47762 | \n",
270 | "
\n",
271 | " \n",
272 | "
\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 | " location_name | \n",
342 | " prec_id | \n",
343 | " block_id | \n",
344 | " date | \n",
345 | " atmospheric_pressure | \n",
346 | " precipitation | \n",
347 | " temperature_avg | \n",
348 | " temperature_highest | \n",
349 | " temperature_lowest | \n",
350 | " humidity | \n",
351 | " daylight_hours | \n",
352 | " blooming | \n",
353 | "
\n",
354 | " \n",
355 | " \n",
356 | " \n",
357 | " | 0 | \n",
358 | " 旭川 | \n",
359 | " 12 | \n",
360 | " 47407 | \n",
361 | " 2007-01-01 | \n",
362 | " 1004.1 | \n",
363 | " 3.5 | \n",
364 | " 0.5 | \n",
365 | " 4.1 | \n",
366 | " -5.5 | \n",
367 | " 74.0 | \n",
368 | " 4.1 | \n",
369 | " 0 | \n",
370 | "
\n",
371 | " \n",
372 | " | 1 | \n",
373 | " 旭川 | \n",
374 | " 12 | \n",
375 | " 47407 | \n",
376 | " 2007-01-02 | \n",
377 | " 1001.5 | \n",
378 | " 1.0 | \n",
379 | " -2.2 | \n",
380 | " 0.8 | \n",
381 | " -9.6 | \n",
382 | " 83.0 | \n",
383 | " 1.6 | \n",
384 | " 0 | \n",
385 | "
\n",
386 | " \n",
387 | " | 2 | \n",
388 | " 旭川 | \n",
389 | " 12 | \n",
390 | " 47407 | \n",
391 | " 2007-01-03 | \n",
392 | " 1005.0 | \n",
393 | " 0.0 | \n",
394 | " -5.5 | \n",
395 | " -0.7 | \n",
396 | " -11.1 | \n",
397 | " 79.0 | \n",
398 | " 3.1 | \n",
399 | " 0 | \n",
400 | "
\n",
401 | " \n",
402 | " | 3 | \n",
403 | " 旭川 | \n",
404 | " 12 | \n",
405 | " 47407 | \n",
406 | " 2007-01-04 | \n",
407 | " 1004.5 | \n",
408 | " 0.0 | \n",
409 | " -3.1 | \n",
410 | " 0.5 | \n",
411 | " -9.8 | \n",
412 | " 79.0 | \n",
413 | " 3.6 | \n",
414 | " 0 | \n",
415 | "
\n",
416 | " \n",
417 | " | 4 | \n",
418 | " 旭川 | \n",
419 | " 12 | \n",
420 | " 47407 | \n",
421 | " 2007-01-05 | \n",
422 | " 1009.0 | \n",
423 | " 0.0 | \n",
424 | " -6.2 | \n",
425 | " 0.0 | \n",
426 | " -10.5 | \n",
427 | " 82.0 | \n",
428 | " 1.9 | \n",
429 | " 0 | \n",
430 | "
\n",
431 | " \n",
432 | "
\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 | " location_name | \n",
510 | " y2007 | \n",
511 | " y2008 | \n",
512 | " y2009 | \n",
513 | " y2010 | \n",
514 | " y2011 | \n",
515 | " y2012 | \n",
516 | " y2013 | \n",
517 | " y2014 | \n",
518 | " y2015 | \n",
519 | " y2016 | \n",
520 | " y2017 | \n",
521 | " prec_id | \n",
522 | " block_id | \n",
523 | "
\n",
524 | " \n",
525 | " \n",
526 | " \n",
527 | " | 旭川 | \n",
528 | " 旭川 | \n",
529 | " 2007-05-06 | \n",
530 | " 2008-04-23 | \n",
531 | " 2009-05-03 | \n",
532 | " 2010-05-11 | \n",
533 | " 2011-05-09 | \n",
534 | " 2012-05-02 | \n",
535 | " 2013-05-18 | \n",
536 | " 2014-05-02 | \n",
537 | " 2015-04-27 | \n",
538 | " 2016-05-03 | \n",
539 | " 2017-05-03 | \n",
540 | " 12 | \n",
541 | " 47407 | \n",
542 | "
\n",
543 | " \n",
544 | " | 宇都宮 | \n",
545 | " 宇都宮 | \n",
546 | " 2007-03-26 | \n",
547 | " 2008-03-28 | \n",
548 | " 2009-03-26 | \n",
549 | " 2010-04-02 | \n",
550 | " 2011-04-06 | \n",
551 | " 2012-04-08 | \n",
552 | " 2013-03-21 | \n",
553 | " 2014-03-29 | \n",
554 | " 2015-03-30 | \n",
555 | " 2016-03-28 | \n",
556 | " 2017-04-03 | \n",
557 | " 41 | \n",
558 | " 47615 | \n",
559 | "
\n",
560 | " \n",
561 | " | 横浜 | \n",
562 | " 横浜 | \n",
563 | " 2007-03-23 | \n",
564 | " 2008-03-23 | \n",
565 | " 2009-03-22 | \n",
566 | " 2010-03-22 | \n",
567 | " 2011-03-30 | \n",
568 | " 2012-04-02 | \n",
569 | " 2013-03-18 | \n",
570 | " 2014-03-25 | \n",
571 | " 2015-03-23 | \n",
572 | " 2016-03-23 | \n",
573 | " 2017-03-25 | \n",
574 | " 46 | \n",
575 | " 47670 | \n",
576 | "
\n",
577 | " \n",
578 | " | 岡山 | \n",
579 | " 岡山 | \n",
580 | " 2007-03-26 | \n",
581 | " 2008-03-26 | \n",
582 | " 2009-03-21 | \n",
583 | " 2010-03-22 | \n",
584 | " 2011-03-31 | \n",
585 | " 2012-04-03 | \n",
586 | " 2013-03-24 | \n",
587 | " 2014-03-28 | \n",
588 | " 2015-03-28 | \n",
589 | " 2016-03-26 | \n",
590 | " 2017-04-01 | \n",
591 | " 66 | \n",
592 | " 47768 | \n",
593 | "
\n",
594 | " \n",
595 | " | 下関 | \n",
596 | " 下関 | \n",
597 | " 2007-03-23 | \n",
598 | " 2008-03-26 | \n",
599 | " 2009-03-18 | \n",
600 | " 2010-03-20 | \n",
601 | " 2011-03-30 | \n",
602 | " 2012-03-30 | \n",
603 | " 2013-03-19 | \n",
604 | " 2014-03-25 | \n",
605 | " 2015-03-25 | \n",
606 | " 2016-03-28 | \n",
607 | " 2017-03-29 | \n",
608 | " 81 | \n",
609 | " 47762 | \n",
610 | "
\n",
611 | " \n",
612 | "
\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 | " location_name | \n",
1355 | " prec_id | \n",
1356 | " block_id | \n",
1357 | " latitude | \n",
1358 | " longitude | \n",
1359 | " height | \n",
1360 | " y2007 | \n",
1361 | " y2008 | \n",
1362 | " y2009 | \n",
1363 | " y2010 | \n",
1364 | " y2011 | \n",
1365 | " y2012 | \n",
1366 | " y2013 | \n",
1367 | " y2014 | \n",
1368 | " y2015 | \n",
1369 | " y2016 | \n",
1370 | " y2017 | \n",
1371 | "
\n",
1372 | " \n",
1373 | " \n",
1374 | " \n",
1375 | " | 下関 | \n",
1376 | " 下関 | \n",
1377 | " 81 | \n",
1378 | " 47762 | \n",
1379 | " 33.9483 | \n",
1380 | " 130.9250 | \n",
1381 | " 3.3 | \n",
1382 | " 2007-03-23 | \n",
1383 | " 2008-03-26 | \n",
1384 | " 2009-03-18 | \n",
1385 | " 2010-03-20 | \n",
1386 | " 2011-03-30 | \n",
1387 | " 2012-03-30 | \n",
1388 | " 2013-03-19 | \n",
1389 | " 2014-03-25 | \n",
1390 | " 2015-03-25 | \n",
1391 | " 2016-03-28 | \n",
1392 | " 2017-03-29 | \n",
1393 | "
\n",
1394 | " \n",
1395 | " | 京都 | \n",
1396 | " 京都 | \n",
1397 | " 61 | \n",
1398 | " 47759 | \n",
1399 | " 35.0150 | \n",
1400 | " 135.7317 | \n",
1401 | " 41.4 | \n",
1402 | " 2007-03-25 | \n",
1403 | " 2008-03-24 | \n",
1404 | " 2009-03-19 | \n",
1405 | " 2010-03-19 | \n",
1406 | " 2011-03-28 | \n",
1407 | " 2012-04-03 | \n",
1408 | " 2013-03-22 | \n",
1409 | " 2014-03-27 | \n",
1410 | " 2015-03-27 | \n",
1411 | " 2016-03-23 | \n",
1412 | " 2017-03-31 | \n",
1413 | "
\n",
1414 | " \n",
1415 | " | 仙台 | \n",
1416 | " 仙台 | \n",
1417 | " 34 | \n",
1418 | " 47590 | \n",
1419 | " 38.2617 | \n",
1420 | " 140.8967 | \n",
1421 | " 38.9 | \n",
1422 | " 2007-04-06 | \n",
1423 | " 2008-04-05 | \n",
1424 | " 2009-04-07 | \n",
1425 | " 2010-04-13 | \n",
1426 | " 2011-04-12 | \n",
1427 | " 2012-04-18 | \n",
1428 | " 2013-04-09 | \n",
1429 | " 2014-04-07 | \n",
1430 | " 2015-04-03 | \n",
1431 | " 2016-04-01 | \n",
1432 | " 2017-04-07 | \n",
1433 | "
\n",
1434 | " \n",
1435 | " | 佐賀 | \n",
1436 | " 佐賀 | \n",
1437 | " 85 | \n",
1438 | " 47813 | \n",
1439 | " 33.2650 | \n",
1440 | " 130.3050 | \n",
1441 | " 5.5 | \n",
1442 | " 2007-03-24 | \n",
1443 | " 2008-03-24 | \n",
1444 | " 2009-03-19 | \n",
1445 | " 2010-03-21 | \n",
1446 | " 2011-03-22 | \n",
1447 | " 2012-03-28 | \n",
1448 | " 2013-03-18 | \n",
1449 | " 2014-03-19 | \n",
1450 | " 2015-03-22 | \n",
1451 | " 2016-03-23 | \n",
1452 | " 2017-03-30 | \n",
1453 | "
\n",
1454 | " \n",
1455 | " | 函館 | \n",
1456 | " 函館 | \n",
1457 | " 23 | \n",
1458 | " 47430 | \n",
1459 | " 41.8167 | \n",
1460 | " 140.7533 | \n",
1461 | " 35.0 | \n",
1462 | " 2007-04-30 | \n",
1463 | " 2008-04-22 | \n",
1464 | " 2009-04-25 | \n",
1465 | " 2010-05-05 | \n",
1466 | " 2011-05-02 | \n",
1467 | " 2012-05-02 | \n",
1468 | " 2013-05-08 | \n",
1469 | " 2014-04-28 | \n",
1470 | " 2015-04-21 | \n",
1471 | " 2016-04-24 | \n",
1472 | " 2017-04-27 | \n",
1473 | "
\n",
1474 | " \n",
1475 | "
\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 | " prec_id | \n",
2129 | " block_id | \n",
2130 | " atmospheric_pressure | \n",
2131 | " precipitation | \n",
2132 | " temperature_avg | \n",
2133 | " temperature_highest | \n",
2134 | " temperature_lowest | \n",
2135 | " humidity | \n",
2136 | " daylight_hours | \n",
2137 | " blooming | \n",
2138 | " year | \n",
2139 | " label | \n",
2140 | " th_mean7 | \n",
2141 | " ta_cumsum2 | \n",
2142 | " ta_cumsum3 | \n",
2143 | " ta_mean1 | \n",
2144 | "
\n",
2145 | " \n",
2146 | " \n",
2147 | " \n",
2148 | " | count | \n",
2149 | " 105682.000000 | \n",
2150 | " 105682.000000 | \n",
2151 | " 105682.000000 | \n",
2152 | " 105682.000000 | \n",
2153 | " 105682.000000 | \n",
2154 | " 105682.000000 | \n",
2155 | " 105682.000000 | \n",
2156 | " 105682.000000 | \n",
2157 | " 105682.000000 | \n",
2158 | " 105682.000000 | \n",
2159 | " 105682.000000 | \n",
2160 | " 105682.000000 | \n",
2161 | " 105682.000000 | \n",
2162 | " 105682.000000 | \n",
2163 | " 105682.000000 | \n",
2164 | " 105682.000000 | \n",
2165 | "
\n",
2166 | " \n",
2167 | " | mean | \n",
2168 | " 52.358491 | \n",
2169 | " 47665.188679 | \n",
2170 | " 1007.262643 | \n",
2171 | " 3.804390 | \n",
2172 | " 11.547651 | \n",
2173 | " 15.723993 | \n",
2174 | " 6.867456 | \n",
2175 | " 66.946188 | \n",
2176 | " 5.443948 | \n",
2177 | " 0.486336 | \n",
2178 | " 2012.000000 | \n",
2179 | " 0.502886 | \n",
2180 | " 15.233320 | \n",
2181 | " 576.027336 | \n",
2182 | " 477.347698 | \n",
2183 | " 0.822720 | \n",
2184 | "
\n",
2185 | " \n",
2186 | " | std | \n",
2187 | " 21.470656 | \n",
2188 | " 140.433158 | \n",
2189 | " 30.965654 | \n",
2190 | " 10.629459 | \n",
2191 | " 7.093331 | \n",
2192 | " 8.180168 | \n",
2193 | " 8.105448 | \n",
2194 | " 14.122655 | \n",
2195 | " 4.194424 | \n",
2196 | " 0.499816 | \n",
2197 | " 3.162451 | \n",
2198 | " 0.499994 | \n",
2199 | " 7.943180 | \n",
2200 | " 628.167811 | \n",
2201 | " 578.244244 | \n",
2202 | " 0.278733 | \n",
2203 | "
\n",
2204 | " \n",
2205 | " | min | \n",
2206 | " 11.000000 | \n",
2207 | " 47401.000000 | \n",
2208 | " 0.000000 | \n",
2209 | " 0.000000 | \n",
2210 | " 3.000000 | \n",
2211 | " 3.000000 | \n",
2212 | " -25.700000 | \n",
2213 | " 0.000000 | \n",
2214 | " 0.000000 | \n",
2215 | " 0.000000 | \n",
2216 | " 2007.000000 | \n",
2217 | " 0.000000 | \n",
2218 | " 0.000000 | \n",
2219 | " 0.000000 | \n",
2220 | " 0.000000 | \n",
2221 | " 0.510989 | \n",
2222 | "
\n",
2223 | " \n",
2224 | " | 25% | \n",
2225 | " 36.000000 | \n",
2226 | " 47595.000000 | \n",
2227 | " 1003.200000 | \n",
2228 | " 0.000000 | \n",
2229 | " 4.800000 | \n",
2230 | " 9.100000 | \n",
2231 | " 0.800000 | \n",
2232 | " 57.000000 | \n",
2233 | " 1.100000 | \n",
2234 | " 0.000000 | \n",
2235 | " 2009.000000 | \n",
2236 | " 0.000000 | \n",
2237 | " 9.185714 | \n",
2238 | " 25.800000 | \n",
2239 | " 0.000000 | \n",
2240 | " 0.533702 | \n",
2241 | "
\n",
2242 | " \n",
2243 | " | 50% | \n",
2244 | " 53.000000 | \n",
2245 | " 47648.000000 | \n",
2246 | " 1009.500000 | \n",
2247 | " 0.000000 | \n",
2248 | " 10.300000 | \n",
2249 | " 15.200000 | \n",
2250 | " 6.000000 | \n",
2251 | " 67.000000 | \n",
2252 | " 5.400000 | \n",
2253 | " 0.000000 | \n",
2254 | " 2012.000000 | \n",
2255 | " 1.000000 | \n",
2256 | " 14.685714 | \n",
2257 | " 337.800000 | \n",
2258 | " 208.700000 | \n",
2259 | " 0.773077 | \n",
2260 | "
\n",
2261 | " \n",
2262 | " | 75% | \n",
2263 | " 68.000000 | \n",
2264 | " 47772.000000 | \n",
2265 | " 1015.300000 | \n",
2266 | " 2.000000 | \n",
2267 | " 17.900000 | \n",
2268 | " 22.800000 | \n",
2269 | " 13.500000 | \n",
2270 | " 77.000000 | \n",
2271 | " 9.100000 | \n",
2272 | " 1.000000 | \n",
2273 | " 2015.000000 | \n",
2274 | " 1.000000 | \n",
2275 | " 22.328571 | \n",
2276 | " 973.375000 | \n",
2277 | " 836.700000 | \n",
2278 | " 1.054696 | \n",
2279 | "
\n",
2280 | " \n",
2281 | " | max | \n",
2282 | " 88.000000 | \n",
2283 | " 47895.000000 | \n",
2284 | " 1034.200000 | \n",
2285 | " 247.500000 | \n",
2286 | " 31.300000 | \n",
2287 | " 39.800000 | \n",
2288 | " 28.500000 | \n",
2289 | " 100.000000 | \n",
2290 | " 15.000000 | \n",
2291 | " 1.000000 | \n",
2292 | " 2017.000000 | \n",
2293 | " 1.000000 | \n",
2294 | " 34.985714 | \n",
2295 | " 2657.500000 | \n",
2296 | " 2379.600000 | \n",
2297 | " 1.690110 | \n",
2298 | "
\n",
2299 | " \n",
2300 | "
\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 | " date | \n",
2926 | " location_name | \n",
2927 | " label | \n",
2928 | " prediction | \n",
2929 | "
\n",
2930 | " \n",
2931 | " \n",
2932 | " \n",
2933 | " | 2017-03-28 | \n",
2934 | " 2017-03-28 | \n",
2935 | " 奈良 | \n",
2936 | " 0.0 | \n",
2937 | " 0.0 | \n",
2938 | "
\n",
2939 | " \n",
2940 | " | 2017-03-29 | \n",
2941 | " 2017-03-29 | \n",
2942 | " 奈良 | \n",
2943 | " 0.0 | \n",
2944 | " 0.0 | \n",
2945 | "
\n",
2946 | " \n",
2947 | " | 2017-03-30 | \n",
2948 | " 2017-03-30 | \n",
2949 | " 奈良 | \n",
2950 | " 0.0 | \n",
2951 | " 1.0 | \n",
2952 | "
\n",
2953 | " \n",
2954 | " | 2017-03-31 | \n",
2955 | " 2017-03-31 | \n",
2956 | " 奈良 | \n",
2957 | " 1.0 | \n",
2958 | " 1.0 | \n",
2959 | "
\n",
2960 | " \n",
2961 | " | 2017-04-01 | \n",
2962 | " 2017-04-01 | \n",
2963 | " 奈良 | \n",
2964 | " 1.0 | \n",
2965 | " 1.0 | \n",
2966 | "
\n",
2967 | " \n",
2968 | "
\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 | " label2017 | \n",
3019 | " prediction2017 | \n",
3020 | " diff2017 | \n",
3021 | " diff2017_days | \n",
3022 | "
\n",
3023 | " \n",
3024 | " | location_name | \n",
3025 | " | \n",
3026 | " | \n",
3027 | " | \n",
3028 | " | \n",
3029 | "
\n",
3030 | " \n",
3031 | " \n",
3032 | " \n",
3033 | " | 下関 | \n",
3034 | " 2017-03-26 | \n",
3035 | " 2017-03-23 | \n",
3036 | " 3 days | \n",
3037 | " 3 | \n",
3038 | "
\n",
3039 | " \n",
3040 | " | 京都 | \n",
3041 | " 2017-03-28 | \n",
3042 | " 2017-03-28 | \n",
3043 | " 0 days | \n",
3044 | " 0 | \n",
3045 | "
\n",
3046 | " \n",
3047 | " | 仙台 | \n",
3048 | " 2017-04-04 | \n",
3049 | " 2017-04-06 | \n",
3050 | " -2 days | \n",
3051 | " -2 | \n",
3052 | "
\n",
3053 | " \n",
3054 | " | 佐賀 | \n",
3055 | " 2017-03-27 | \n",
3056 | " 2017-03-22 | \n",
3057 | " 5 days | \n",
3058 | " 5 | \n",
3059 | "
\n",
3060 | " \n",
3061 | " | 函館 | \n",
3062 | " 2017-04-24 | \n",
3063 | " 2017-04-21 | \n",
3064 | " 3 days | \n",
3065 | " 3 | \n",
3066 | "
\n",
3067 | " \n",
3068 | " | 前橋 | \n",
3069 | " 2017-03-30 | \n",
3070 | " 2017-03-30 | \n",
3071 | " 0 days | \n",
3072 | " 0 | \n",
3073 | "
\n",
3074 | " \n",
3075 | " | 名古屋 | \n",
3076 | " 2017-03-25 | \n",
3077 | " 2017-03-25 | \n",
3078 | " 0 days | \n",
3079 | " 0 | \n",
3080 | "
\n",
3081 | " \n",
3082 | " | 和歌山 | \n",
3083 | " 2017-03-27 | \n",
3084 | " 2017-03-25 | \n",
3085 | " 2 days | \n",
3086 | " 2 | \n",
3087 | "
\n",
3088 | " \n",
3089 | " | 大分 | \n",
3090 | " 2017-04-01 | \n",
3091 | " 2017-03-24 | \n",
3092 | " 8 days | \n",
3093 | " 8 | \n",
3094 | "
\n",
3095 | " \n",
3096 | " | 大阪 | \n",
3097 | " 2017-03-27 | \n",
3098 | " 2017-03-24 | \n",
3099 | " 3 days | \n",
3100 | " 3 | \n",
3101 | "
\n",
3102 | " \n",
3103 | " | 奈良 | \n",
3104 | " 2017-03-31 | \n",
3105 | " 2017-03-30 | \n",
3106 | " 1 days | \n",
3107 | " 1 | \n",
3108 | "
\n",
3109 | " \n",
3110 | " | 宇都宮 | \n",
3111 | " 2017-03-31 | \n",
3112 | " 2017-04-01 | \n",
3113 | " -1 days | \n",
3114 | " -1 | \n",
3115 | "
\n",
3116 | " \n",
3117 | " | 室蘭 | \n",
3118 | " 2017-04-29 | \n",
3119 | " 2017-04-29 | \n",
3120 | " 0 days | \n",
3121 | " 0 | \n",
3122 | "
\n",
3123 | " \n",
3124 | " | 宮崎 | \n",
3125 | " 2017-03-30 | \n",
3126 | " 2017-03-21 | \n",
3127 | " 9 days | \n",
3128 | " 9 | \n",
3129 | "
\n",
3130 | " \n",
3131 | " | 富山 | \n",
3132 | " 2017-04-02 | \n",
3133 | " 2017-04-02 | \n",
3134 | " 0 days | \n",
3135 | " 0 | \n",
3136 | "
\n",
3137 | " \n",
3138 | " | 山形 | \n",
3139 | " 2017-04-11 | \n",
3140 | " 2017-04-10 | \n",
3141 | " 1 days | \n",
3142 | " 1 | \n",
3143 | "
\n",
3144 | " \n",
3145 | " | 岐阜 | \n",
3146 | " 2017-03-25 | \n",
3147 | " 2017-03-26 | \n",
3148 | " -1 days | \n",
3149 | " -1 | \n",
3150 | "
\n",
3151 | " \n",
3152 | " | 岡山 | \n",
3153 | " 2017-03-29 | \n",
3154 | " 2017-03-27 | \n",
3155 | " 2 days | \n",
3156 | " 2 | \n",
3157 | "
\n",
3158 | " \n",
3159 | " | 帯広 | \n",
3160 | " 2017-04-28 | \n",
3161 | " 2017-04-30 | \n",
3162 | " -2 days | \n",
3163 | " -2 | \n",
3164 | "
\n",
3165 | " \n",
3166 | " | 広島 | \n",
3167 | " 2017-03-24 | \n",
3168 | " 2017-03-24 | \n",
3169 | " 0 days | \n",
3170 | " 0 | \n",
3171 | "
\n",
3172 | " \n",
3173 | " | 彦根 | \n",
3174 | " 2017-04-02 | \n",
3175 | " 2017-04-01 | \n",
3176 | " 1 days | \n",
3177 | " 1 | \n",
3178 | "
\n",
3179 | " \n",
3180 | " | 徳島 | \n",
3181 | " 2017-03-31 | \n",
3182 | " 2017-03-24 | \n",
3183 | " 7 days | \n",
3184 | " 7 | \n",
3185 | "
\n",
3186 | " \n",
3187 | " | 新潟 | \n",
3188 | " 2017-04-05 | \n",
3189 | " 2017-04-05 | \n",
3190 | " 0 days | \n",
3191 | " 0 | \n",
3192 | "
\n",
3193 | " \n",
3194 | " | 旭川 | \n",
3195 | " 2017-04-30 | \n",
3196 | " 2017-05-03 | \n",
3197 | " -3 days | \n",
3198 | " -3 | \n",
3199 | "
\n",
3200 | " \n",
3201 | " | 札幌 | \n",
3202 | " 2017-04-25 | \n",
3203 | " 2017-04-26 | \n",
3204 | " -1 days | \n",
3205 | " -1 | \n",
3206 | "
\n",
3207 | " \n",
3208 | " | 東京 | \n",
3209 | " 2017-03-18 | \n",
3210 | " 2017-03-22 | \n",
3211 | " -4 days | \n",
3212 | " -4 | \n",
3213 | "
\n",
3214 | " \n",
3215 | " | 松山 | \n",
3216 | " 2017-03-27 | \n",
3217 | " 2017-03-23 | \n",
3218 | " 4 days | \n",
3219 | " 4 | \n",
3220 | "
\n",
3221 | " \n",
3222 | " | 松江 | \n",
3223 | " 2017-03-29 | \n",
3224 | " 2017-03-29 | \n",
3225 | " 0 days | \n",
3226 | " 0 | \n",
3227 | "
\n",
3228 | " \n",
3229 | " | 横浜 | \n",
3230 | " 2017-03-22 | \n",
3231 | " 2017-03-23 | \n",
3232 | " -1 days | \n",
3233 | " -1 | \n",
3234 | "
\n",
3235 | " \n",
3236 | " | 水戸 | \n",
3237 | " 2017-03-31 | \n",
3238 | " 2017-04-01 | \n",
3239 | " -1 days | \n",
3240 | " -1 | \n",
3241 | "
\n",
3242 | " \n",
3243 | " | 津 | \n",
3244 | " 2017-03-31 | \n",
3245 | " 2017-03-28 | \n",
3246 | " 3 days | \n",
3247 | " 3 | \n",
3248 | "
\n",
3249 | " \n",
3250 | " | 熊本 | \n",
3251 | " 2017-03-29 | \n",
3252 | " 2017-03-21 | \n",
3253 | " 8 days | \n",
3254 | " 8 | \n",
3255 | "
\n",
3256 | " \n",
3257 | " | 熊谷 | \n",
3258 | " 2017-03-25 | \n",
3259 | " 2017-03-25 | \n",
3260 | " 0 days | \n",
3261 | " 0 | \n",
3262 | "
\n",
3263 | " \n",
3264 | " | 甲府 | \n",
3265 | " 2017-03-27 | \n",
3266 | " 2017-03-25 | \n",
3267 | " 2 days | \n",
3268 | " 2 | \n",
3269 | "
\n",
3270 | " \n",
3271 | " | 盛岡 | \n",
3272 | " 2017-04-14 | \n",
3273 | " 2017-04-17 | \n",
3274 | " -3 days | \n",
3275 | " -3 | \n",
3276 | "
\n",
3277 | " \n",
3278 | " | 神戸 | \n",
3279 | " 2017-03-31 | \n",
3280 | " 2017-03-25 | \n",
3281 | " 6 days | \n",
3282 | " 6 | \n",
3283 | "
\n",
3284 | " \n",
3285 | " | 福井 | \n",
3286 | " 2017-04-02 | \n",
3287 | " 2017-04-01 | \n",
3288 | " 1 days | \n",
3289 | " 1 | \n",
3290 | "
\n",
3291 | " \n",
3292 | " | 福岡 | \n",
3293 | " 2017-03-22 | \n",
3294 | " 2017-03-21 | \n",
3295 | " 1 days | \n",
3296 | " 1 | \n",
3297 | "
\n",
3298 | " \n",
3299 | " | 福島 | \n",
3300 | " 2017-04-05 | \n",
3301 | " 2017-04-05 | \n",
3302 | " 0 days | \n",
3303 | " 0 | \n",
3304 | "
\n",
3305 | " \n",
3306 | " | 秋田 | \n",
3307 | " 2017-04-13 | \n",
3308 | " 2017-04-10 | \n",
3309 | " 3 days | \n",
3310 | " 3 | \n",
3311 | "
\n",
3312 | " \n",
3313 | " | 稚内 | \n",
3314 | " 2017-05-06 | \n",
3315 | " 2017-05-06 | \n",
3316 | " 0 days | \n",
3317 | " 0 | \n",
3318 | "
\n",
3319 | " \n",
3320 | " | 網走 | \n",
3321 | " 2017-05-02 | \n",
3322 | " 2017-05-05 | \n",
3323 | " -3 days | \n",
3324 | " -3 | \n",
3325 | "
\n",
3326 | " \n",
3327 | " | 金沢 | \n",
3328 | " 2017-04-01 | \n",
3329 | " 2017-03-31 | \n",
3330 | " 1 days | \n",
3331 | " 1 | \n",
3332 | "
\n",
3333 | " \n",
3334 | " | 釧路 | \n",
3335 | " 2017-05-11 | \n",
3336 | " 2017-05-11 | \n",
3337 | " 0 days | \n",
3338 | " 0 | \n",
3339 | "
\n",
3340 | " \n",
3341 | " | 銚子 | \n",
3342 | " 2017-03-29 | \n",
3343 | " 2017-03-26 | \n",
3344 | " 3 days | \n",
3345 | " 3 | \n",
3346 | "
\n",
3347 | " \n",
3348 | " | 長崎 | \n",
3349 | " 2017-03-27 | \n",
3350 | " 2017-03-22 | \n",
3351 | " 5 days | \n",
3352 | " 5 | \n",
3353 | "
\n",
3354 | " \n",
3355 | " | 長野 | \n",
3356 | " 2017-04-11 | \n",
3357 | " 2017-04-09 | \n",
3358 | " 2 days | \n",
3359 | " 2 | \n",
3360 | "
\n",
3361 | " \n",
3362 | " | 青森 | \n",
3363 | " 2017-04-14 | \n",
3364 | " 2017-04-16 | \n",
3365 | " -2 days | \n",
3366 | " -2 | \n",
3367 | "
\n",
3368 | " \n",
3369 | " | 静岡 | \n",
3370 | " 2017-03-30 | \n",
3371 | " 2017-03-23 | \n",
3372 | " 7 days | \n",
3373 | " 7 | \n",
3374 | "
\n",
3375 | " \n",
3376 | " | 高松 | \n",
3377 | " 2017-03-30 | \n",
3378 | " 2017-03-25 | \n",
3379 | " 5 days | \n",
3380 | " 5 | \n",
3381 | "
\n",
3382 | " \n",
3383 | " | 高知 | \n",
3384 | " 2017-03-26 | \n",
3385 | " 2017-03-21 | \n",
3386 | " 5 days | \n",
3387 | " 5 | \n",
3388 | "
\n",
3389 | " \n",
3390 | " | 鳥取 | \n",
3391 | " 2017-03-30 | \n",
3392 | " 2017-03-30 | \n",
3393 | " 0 days | \n",
3394 | " 0 | \n",
3395 | "
\n",
3396 | " \n",
3397 | " | 鹿児島 | \n",
3398 | " 2017-04-02 | \n",
3399 | " 2017-03-20 | \n",
3400 | " 13 days | \n",
3401 | " 13 | \n",
3402 | "
\n",
3403 | " \n",
3404 | "
\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 |
--------------------------------------------------------------------------------