In [58]:
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)
In [59]:
elec.head()
Out[59]:
datetime tky_dmnd 原子力 thp hyd 地熱 バイオマス slr 太陽光出力制御量 風力発電実績 風力出力制御量 揚水 連系線 合計
0 2016-04-01 00:00:00 2555 0 2258 92 0 2 0 0 2 0 0 201 2555
1 2016-04-01 01:00:00 2433 0 2151 92 0 2 0 0 2 0 0 186 2433
2 2016-04-01 02:00:00 2393 0 2117 92 0 2 0 0 2 0 0 180 2393
3 2016-04-01 03:00:00 2375 0 2102 93 0 2 0 0 1 0 0 178 2375
4 2016-04-01 04:00:00 2390 0 2126 93 0 2 0 0 1 0 0 169 2390
In [60]:
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)
In [61]:
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)
In [62]:
#気象庁のデータの読み込みを行う
wh = pd.read_csv('[path to data]', parse_dates={'datetime': ['date', 'time']})
wh.rename(columns=columns, inplace=True)
wh.head()
Out[62]:
datetime temp rain solor_rad wind_speed wind_direction sunshine_hours atom humidity
0 2016-04-01 01:00:00 13.3 0.0 0.0 1.4 南西 0.0 1014.8 76.0
1 2016-04-01 02:00:00 13.8 0.0 0.0 2.6 北西 0.0 1014.6 72.0
2 2016-04-01 03:00:00 13.0 0.0 0.0 3.1 北西 0.0 1014.7 63.0
3 2016-04-01 04:00:00 12.2 0.0 0.0 2.4 北北西 0.0 1014.9 67.0
4 2016-04-01 05:00:00 11.2 0.0 0.0 1.5 北北西 0.0 1015.4 69.0
In [63]:
#気温と電力需要の相関を作成するのに必要なカラムを抽出する。結合条件は日時
dat = pd.merge(elec, wh, how="inner")
dat = dat.loc[:,["datetime","tky_dmnd","slr","temp","solor_rad"]]
dat.head()
Out[63]:
datetime tky_dmnd slr temp solor_rad
0 2016-04-01 01:00:00 2433 0 13.3 0.0
1 2016-04-01 02:00:00 2393 0 13.8 0.0
2 2016-04-01 03:00:00 2375 0 13.0 0.0
3 2016-04-01 04:00:00 2390 0 12.2 0.0
4 2016-04-01 05:00:00 2467 0 11.2 0.0
In [64]:
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)
In [65]:
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)
In [68]:
#東京の電力消費データと気温のデータの結合を行う
tokyo = elec.join(wh["temp"]).dropna().as_matrix()
tokyo
Out[68]:
array([[ 2433. ,    13.3],
       [ 2393. ,    13.8],
       [ 2375. ,    13. ],
       ..., 
       [ 2994. ,     5.4],
       [ 2946. ,     5.5],
       [ 2783. ,     5.4]])
In [69]:
tokyo_elec = tokyo[:, 0:1]
tokyo_wthr = tokyo[:, 1:]
In [70]:
tokyo_wthr
Out[70]:
array([[ 13.3],
       [ 13.8],
       [ 13. ],
       ..., 
       [  5.4],
       [  5.5],
       [  5.4]])
In [71]:
# SVMを用いるために必要なライブラリーの呼び出し
import sklearn.cross_validation
import sklearn.svm

data_count = len(tokyo_elec)
In [72]:
# 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)))
SVR: Training Score = 0.141059, Testing(Validate) Score = -0.470640
SVR: Training Score = 0.117703, Testing(Validate) Score = 0.121252
SVR: Training Score = 0.073607, Testing(Validate) Score = -0.129368
SVR: Training Score = 0.134667, Testing(Validate) Score = 0.024817
SVR: Training Score = 0.166388, Testing(Validate) Score = -0.293765
In [73]:
#予測モデルの作成
model = sklearn.svm.SVR()
y_train = y_train.flatten()
y_test = y_test.flatten()

model.fit(x_train, y_train)
Out[73]:
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
  kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)
In [74]:
#最小値、最大値は、それぞれ気温の最高・最低を参照する。刻み込み幅は0.01
import numpy as np
px = np.arange(tokyo_wthr.min(), tokyo_wthr.max(), 0.01)[:, np.newaxis]
py = model.predict(px)
In [75]:
ppy = pd.DataFrame(px, py)
ppy.rename(columns = {0:'temp'}, inplace=True)
In [76]:
#気温と電力需要のグラフに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)
In [77]:
#気温と電力需要のグラフに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)
In [78]:
#当てはまりがわるいので、カーネル密度推計を行い、分布を立体的に確認する
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
Out[78]:
array([-6.78122567, -7.41382117, -7.28689816, ..., -7.41382617,
       -6.45580608, -7.41382617])
In [79]:
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)
In [80]:
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)
In [ ]:
#東電と気象庁の結合済みデータを読み出す。
tep = pd.read_csv("[path to data]")
In [88]:
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")]
In [86]:
#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)
This is the format of your plot grid:
[ (1,1) x1,y1 ]    [ (1,2) x2,y2 ]    [ (1,3) x3,y3 ]  
[ (2,1) x4,y4 ]    [ (2,2) x5,y5 ]    [ (2,3) x6,y6 ]  
[ (3,1) x7,y7 ]    [ (3,2) x8,y8 ]    [ (3,3) x9,y9 ]  
[ (4,1) x10,y10 ]  [ (4,2) x11,y11 ]  [ (4,3) x12,y12 ]

In [89]:
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)
This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y1 ]  [ (1,3) x3,y1 ]
[ (2,1) x1,y2 ]  [ (2,2) x2,y2 ]  [ (2,3) x3,y2 ]
[ (3,1) x1,y3 ]  [ (3,2) x2,y3 ]  [ (3,3) x3,y3 ]
[ (4,1) x1,y4 ]  [ (4,2) x2,y4 ]  [ (4,3) x3,y4 ]

In [90]:
#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()
In [91]:
prc = json.loads(prc)
prc["values"][0:10]
Out[91]:
[{'lat': 36.1, 'lng': 139.2, 'prc': 0.0},
 {'lat': 36.1, 'lng': 139.3, 'prc': 0.0},
 {'lat': 36.1, 'lng': 139.4, 'prc': 0.0},
 {'lat': 36.1, 'lng': 139.5, 'prc': 0.0},
 {'lat': 36.1, 'lng': 139.6, 'prc': 0.0},
 {'lat': 36.1, 'lng': 139.7, 'prc': 0.0},
 {'lat': 36.1, 'lng': 139.8, 'prc': 8.9},
 {'lat': 36.1, 'lng': 139.9, 'prc': 11.0},
 {'lat': 36.1, 'lng': 140.0, 'prc': 0.0},
 {'lat': 36.1, 'lng': 140.1, 'prc': 0.0}]
In [ ]: