import pandas as pd
#東京電力のオープンデータを読み込む
elec = pd.read_csv(
"[path to data]",
#日付と時間を結合したセルに変更する
parse_dates={'datetime': ['DATE', 'TIME']})
#使用する文字列をアルファベットに変更する
columns={
"東京エリア需要":"tky_dmnd",
"火力":"thp",
"水力":"hyd",
"太陽光発電実績":"slr"
}
#再度読み込みを行う
elec.rename(columns=columns, inplace=True)
elec.head()
import plotly
plotly.offline.init_notebook_mode(connected=False)
#ラベルオプションの設定を行う
data = [plotly.graph_objs.Scatter(x=elec["datetime"], y=elec["tky_dmnd"], name="東京エリア需要")]
layout = plotly.graph_objs.Layout(
title="東京エリア需要(1時間毎の時系列)",
legend={"x":0.8, "y":0.1},
xaxis={"title":"datetime"},
yaxis={"title":"東京エリア需要(万kw)"}
)
fig = plotly.graph_objs.Figure(data=data, layout=layout)
plotly.offline.iplot(fig, show_link=False)
thp = plotly.graph_objs.Scatter(x=elec["datetime"], y=elec["thp"], mode = "lines", name="火力", marker=dict(color="rgba(220,20,60,0.7)"))
hyd = plotly.graph_objs.Scatter(x=elec["datetime"], y=elec["hyd"], mode = "lines", name="水力", marker=dict(color="rgba(0,128,255,0.8)"))
slr = plotly.graph_objs.Scatter(x=elec["datetime"], y=elec["slr"], mode = "lines", name="太陽光", marker=dict(color="rgba(255,165,0,0.8)"))
#火力・水力・太陽光のデータの結合を行う
el = [thp, slr, hyd]
layout = plotly.graph_objs.Layout(
title="発電量割合(1時間毎の時系列)",
legend={"x":1.0, "y":1.0},
xaxis={"title":"date_time"},
yaxis={"title":"発電量(万kw)"}
)
fig = plotly.graph_objs.Figure(data=el, layout=layout)
plotly.offline.iplot(fig, show_link=False)
#気象庁のデータの読み込みを行う
wh = pd.read_csv('[path to data]', parse_dates={'datetime': ['date', 'time']})
wh.rename(columns=columns, inplace=True)
wh.head()
#気温と電力需要の相関を作成するのに必要なカラムを抽出する。結合条件は日時
dat = pd.merge(elec, wh, how="inner")
dat = dat.loc[:,["datetime","tky_dmnd","slr","temp","solor_rad"]]
dat.head()
cor_dmnd = [plotly.graph_objs.Scatter(x=dat["temp"], y=dat["tky_dmnd"], mode = "markers", marker=dict(color="rgba(64,64,64,0.6)"))]
layout = plotly.graph_objs.Layout(
title="電力需要と気温の関係",
legend={"x":0.8, "y":0.1},
xaxis={"title":"気温(℃)"},
yaxis={"title":"電力需要(万kw)"}
)
fig = plotly.graph_objs.Figure(data=cor_dmnd , layout=layout)
plotly.offline.iplot(fig, show_link=False)
import plotly as py
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=False)
trace1 = go.Scatter3d(x=dat["datetime"],
y=dat["temp"],
z=dat["tky_dmnd"],
mode='markers',
marker=dict(
size=2,
colorscale='Viridis',
opacity=0.8,
color="rgba(25,25,112,0.5)"
)
)
data = [trace1]
layout = plotly.graph_objs.Layout(
title="気温と電力需要の時系列3D"
)
fig = go.Figure(data=data, layout=layout)
py.offline.iplot(fig, show_link=False)
#東京の電力消費データと気温のデータの結合を行う
tokyo = elec.join(wh["temp"]).dropna().as_matrix()
tokyo
tokyo_elec = tokyo[:, 0:1]
tokyo_wthr = tokyo[:, 1:]
tokyo_wthr
# SVMを用いるために必要なライブラリーの呼び出し
import sklearn.cross_validation
import sklearn.svm
data_count = len(tokyo_elec)
# KFoldの呼び出しと定義
kf = sklearn.cross_validation.KFold(data_count, n_folds=5)
# 交差検定実施
for train, test in kf:
x_train = tokyo_wthr[train]
x_test = tokyo_wthr[test]
y_train = tokyo_elec[train]
y_test = tokyo_elec[test]
# -- SVR --
model = sklearn.svm.SVR()
y_train = y_train.flatten()
y_test = y_test.flatten()
model.fit(x_train, y_train)
print ("SVR: Training Score = %f, Testing(Validate) Score = %f" %
(model.score(x_train, y_train), model.score(x_test, y_test)))
#予測モデルの作成
model = sklearn.svm.SVR()
y_train = y_train.flatten()
y_test = y_test.flatten()
model.fit(x_train, y_train)
#最小値、最大値は、それぞれ気温の最高・最低を参照する。刻み込み幅は0.01
import numpy as np
px = np.arange(tokyo_wthr.min(), tokyo_wthr.max(), 0.01)[:, np.newaxis]
py = model.predict(px)
ppy = pd.DataFrame(px, py)
ppy.rename(columns = {0:'temp'}, inplace=True)
#気温と電力需要のグラフにSVMで作成された予測モデル
pred = [plotly.graph_objs.Scatter(x=ppy["temp"], y=ppy.index, mode = "lines", marker=dict(color="rgba(30,144,255, 0.8)"))]
layout = plotly.graph_objs.Layout(
title="SVMを用いた需要予測モデル",
legend={"x":0.8, "y":0.1},
xaxis={"title":"気温(℃)"},
yaxis={"title":"電力需要(万kw)"}
)
fig = plotly.graph_objs.Figure(data=pred, layout=layout)
plotly.offline.iplot(fig, show_link=False)
#気温と電力需要のグラフにSVMで作成された予測モデルを重ね合わせる
cor_dmnd = plotly.graph_objs.Scatter(x=dat["temp"], y=dat["tky_dmnd"], mode = "markers",marker=dict(color="rgba(64,64,64,0.6)"), name="実測値散布")
pred = plotly.graph_objs.Scatter(x=ppy["temp"], y=ppy.index, mode = "lines", marker=dict(color="rgba(30,144,255,0.5)"), name="需要予測")
el = [cor_dmnd, pred]
layout = plotly.graph_objs.Layout(
title="SVMを用いた需要予測モデル",
legend={"x":1.0, "y":1.0},
xaxis={"title":"気温(℃)"},
yaxis={"title":"電力需要(万kw)"}
)
fig = plotly.graph_objs.Figure(data=el, layout=layout)
plotly.offline.iplot(fig, show_link=False)
#当てはまりがわるいので、カーネル密度推計を行い、分布を立体的に確認する
from sklearn.neighbors.kde import KernelDensity
import numpy as np
dmnd_cor = tokyo[:, 0:]
#dmnd_cor
kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(dmnd_cor)
kde_score = kde.score_samples(dmnd_cor)
kde_score
import plotly as py
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=False)
#3D用の呼び出し、x=需要、y=気温、z=密度のグラフを書く
trace1 = go.Scatter3d(x=dat["tky_dmnd"],
y=dat["temp"],
z=kde_score,
mode='markers',
marker=dict(
size=2,
colorscale='Viridis',
opacity=0.8,
color="rgba(25,25,112,0.5)"
)
)
data = [trace1]
layout = plotly.graph_objs.Layout(
title="気温と電力需要の確率密度"
)
fig = go.Figure(data=data, layout=layout)
py.offline.iplot(fig, show_link=False)
import plotly
plotly.offline.init_notebook_mode(connected=False)
#ラベルオプションの設定を行う
slr_cor = [plotly.graph_objs.Scatter(x=dat["solor_rad"], y=dat["slr"], mode = "markers", name="全日")]
layout = plotly.graph_objs.Layout(
title="日射量と太陽光発電量の相関",
legend={"x":0.8, "y":0.1},
xaxis={"title":"日射量"},
yaxis={"title":"太陽光発電量"}
)
fig = plotly.graph_objs.Figure(data=slr_cor, layout=layout)
plotly.offline.iplot(fig, show_link=False)
#東電と気象庁の結合済みデータを読み出す。
tep = pd.read_csv("[path to data]")
apr = tep[tep.datetime < "2016-05-01"]
may = tep[(tep.datetime >= "2016-05-01") & (tep.datetime < "2016-06-01")]
jun = tep[(tep.datetime >= "2016-06-01") & (tep.datetime < "2016-07-01")]
jul = tep[(tep.datetime >= "2016-07-01") & (tep.datetime < "2016-08-01")]
aug = tep[(tep.datetime >= "2016-08-01") & (tep.datetime < "2016-09-01")]
sep = tep[(tep.datetime >= "2016-09-01") & (tep.datetime < "2016-10-01")]
oct = tep[(tep.datetime >= "2016-10-01") & (tep.datetime < "2016-11-01")]
nov = tep[(tep.datetime >= "2016-11-01") & (tep.datetime < "2016-12-01")]
dec = tep[(tep.datetime >= "2016-12-01") & (tep.datetime < "2017-01-01")]
jan = tep[(tep.datetime >= "2017-01-01") & (tep.datetime < "2017-02-01")]
feb = tep[(tep.datetime >= "2017-02-01") & (tep.datetime < "2017-03-01")]
mar = tep[(tep.datetime >= "2017-03-01") & (tep.datetime < "2017-04-01")]
#facet grid用の設定を行う
from plotly import tools
apr = plotly.graph_objs.Scatter(x=apr["time"], y=apr["solor_rad"], mode = "markers", name="apr")
may = plotly.graph_objs.Scatter(x=may["time"], y=may["solor_rad"], mode = "markers", name="may")
jun = plotly.graph_objs.Scatter(x=jun["time"], y=jun["solor_rad"], mode = "markers", name="jun")
jul = plotly.graph_objs.Scatter(x=jul["time"], y=jul["solor_rad"], mode = "markers", name="jul")
aug = plotly.graph_objs.Scatter(x=aug["time"], y=aug["solor_rad"], mode = "markers", name="aug")
sep = plotly.graph_objs.Scatter(x=sep["time"], y=sep["solor_rad"], mode = "markers", name="sep")
oct = plotly.graph_objs.Scatter(x=oct["time"], y=oct["solor_rad"], mode = "markers", name="oct")
nov = plotly.graph_objs.Scatter(x=nov["time"], y=nov["solor_rad"], mode = "markers", name="nov")
dec = plotly.graph_objs.Scatter(x=dec["time"], y=dec["solor_rad"], mode = "markers", name="dec")
jan = plotly.graph_objs.Scatter(x=jan["time"], y=jan["solor_rad"], mode = "markers", name="jan")
feb = plotly.graph_objs.Scatter(x=feb["time"], y=feb["solor_rad"], mode = "markers", name="feb")
mar = plotly.graph_objs.Scatter(x=mar["time"], y=mar["solor_rad"], mode = "markers", name="mar")
fig = tools.make_subplots(rows=4, cols=3, subplot_titles=('apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec', 'jan', 'feb', 'mar'))
fig.append_trace(apr, 1, 1)
fig.append_trace(may, 1, 2)
fig.append_trace(jun, 1, 3)
fig.append_trace(jul, 2, 1)
fig.append_trace(aug, 2, 2)
fig.append_trace(sep, 2, 3)
fig.append_trace(oct, 3, 1)
fig.append_trace(nov, 3, 2)
fig.append_trace(dec, 3, 3)
fig.append_trace(jan, 4, 1)
fig.append_trace(feb, 4, 2)
fig.append_trace(mar, 4, 3)
fig['layout'].update(height=800, width=900, title='月別日射量')
plotly.offline.iplot(fig, show_link=False)
from plotly import tools
apr = plotly.graph_objs.Scatter(x=apr["solor_rad"], y=apr["slr"], mode = "markers", name="apr")
may = plotly.graph_objs.Scatter(x=may["solor_rad"], y=may["slr"], mode = "markers", name="may")
jun = plotly.graph_objs.Scatter(x=jun["solor_rad"], y=jun["slr"], mode = "markers", name="jun")
jul = plotly.graph_objs.Scatter(x=jul["solor_rad"], y=jul["slr"], mode = "markers", name="jul")
aug = plotly.graph_objs.Scatter(x=aug["solor_rad"], y=aug["slr"], mode = "markers", name="aug")
sep = plotly.graph_objs.Scatter(x=sep["solor_rad"], y=sep["slr"], mode = "markers", name="sep")
oct = plotly.graph_objs.Scatter(x=oct["solor_rad"], y=oct["slr"], mode = "markers", name="oct")
nov = plotly.graph_objs.Scatter(x=nov["solor_rad"], y=nov["slr"], mode = "markers", name="nov")
dec = plotly.graph_objs.Scatter(x=dec["solor_rad"], y=dec["slr"], mode = "markers", name="dec")
jan = plotly.graph_objs.Scatter(x=jan["solor_rad"], y=jan["slr"], mode = "markers", name="jan")
feb = plotly.graph_objs.Scatter(x=feb["solor_rad"], y=feb["slr"], mode = "markers", name="feb")
mar = plotly.graph_objs.Scatter(x=mar["solor_rad"], y=mar["slr"], mode = "markers", name="mar")
fig = tools.make_subplots(rows=4, cols=3, shared_xaxes=True, shared_yaxes=True, subplot_titles=('apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec', 'jan', 'feb', 'mar'))
fig.append_trace(apr, 1, 1)
fig.append_trace(may, 1, 2)
fig.append_trace(jun, 1, 3)
fig.append_trace(jul, 2, 1)
fig.append_trace(aug, 2, 2)
fig.append_trace(sep, 2, 3)
fig.append_trace(oct, 3, 1)
fig.append_trace(nov, 3, 2)
fig.append_trace(dec, 3, 3)
fig.append_trace(jan, 4, 1)
fig.append_trace(feb, 4, 2)
fig.append_trace(mar, 4, 3)
fig['layout'].update(height=800, width=900, title='太陽光発電と日射量相関(月別)')
plotly.offline.iplot(fig, show_link=False)
#AERIALのデータを参照するsample code
import urllib.request
import json
with urllib.request.urlopen('http://api.hi-rezclimate.org/amsr2.py/prc?lat=35.689622&lng=139.761900&date=2016-08-20&range=0.5') as response:
prc = response.read()
prc = json.loads(prc)
prc["values"][0:10]