NaokiOkamoto commited on
Commit
498b624
1 Parent(s): 0fa9403

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -19
app.py CHANGED
@@ -8,10 +8,11 @@ import japanize_matplotlib
8
  import matplotlib.dates as mdates
9
  from dateutil.relativedelta import relativedelta
10
  import datetime
 
11
  from function import get_fish_qty, get_estat, dr_prediction_deployment, prediction_func, train_modeling
12
 
13
  import yaml
14
- with open('config/config.yaml') as file:
15
  config = yaml.safe_load(file.read())
16
 
17
  def retrain():
@@ -34,48 +35,119 @@ def get_prediction_result(retrain = False):
34
  how='left')
35
  df.loc[df['forecast_point'].isnull(), 'forecast_point'] = df['target_date'].apply(lambda x:x-relativedelta(months=1))
36
  df = df.loc[~((df['target_date']<(today+relativedelta(months=1)))&(df['電気代'].isnull()))]
37
- return df[['forecast_point', 'target_date', '電気代']]
 
38
 
39
  def plot_prediction_result():
40
  update = gr.LinePlot.update(
41
  value=get_prediction_result(),
42
  x="target_date",
43
- y="電気代",
44
  title="昨日までの魚の卸売り量から予測された、来月の2人世帯の平均電気料金の推移",
45
  width=500,
46
- height=400,
47
  )
48
  return update
49
- # def plot_prediction_result():
50
- # df = get_prediction_result()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- # fig, ax1 = plt.subplots(1, 1, figsize=(30, 10))
53
- # fig.autofmt_xdate()
54
- # ax1.xaxis.set_major_formatter(mdates.DateFormatter("%Y/%m/%d"))
55
- # ax1.xaxis.set_major_locator(mdates.DayLocator(interval=1))
56
- # plt.rcParams["font.size"] = 14
57
- # ax1.grid(True)
58
- # ax1.set_xlim(df['target_date'].min(), df['target_date'].max())
59
- # ax1.set_xlabel('target_date')
60
- # ax1.set_ylabel('電気代(円)')
61
- # ax1.plot(df['target_date'], df['電気代'])
62
- # ax1.legend(loc="upper left", bbox_to_anchor=(1.0, 1.0))
63
 
64
- return fig
 
 
 
 
65
 
66
 
67
  with gr.Blocks() as electoric_ploting:
68
- gr.Markdown("# 📈 Real-Time Line Plot")
 
 
 
 
 
 
 
 
 
 
 
 
69
  with gr.Row():
70
  with gr.Column():
71
  plot = gr.LinePlot(show_label=False)
72
  # plot = gr.Plot(label="昨日までの魚の卸売り量から予測された、来月の2人世帯の平均電気料金の推移")
 
 
 
 
 
 
 
 
 
 
73
  with gr.Row():
74
  gr.DataFrame(get_prediction_result)
75
 
76
  with gr.Column():
 
 
 
 
 
77
  btn= gr.Button(value="再学習")
78
  btn.click(retrain, inputs=None, outputs=None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
 
81
  # demo.load(make_plot, inputs=[button], outputs=[plot])
 
8
  import matplotlib.dates as mdates
9
  from dateutil.relativedelta import relativedelta
10
  import datetime
11
+ import datarobot as dr
12
  from function import get_fish_qty, get_estat, dr_prediction_deployment, prediction_func, train_modeling
13
 
14
  import yaml
15
+ with open('config.yaml') as file:
16
  config = yaml.safe_load(file.read())
17
 
18
  def retrain():
 
35
  how='left')
36
  df.loc[df['forecast_point'].isnull(), 'forecast_point'] = df['target_date'].apply(lambda x:x-relativedelta(months=1))
37
  df = df.loc[~((df['target_date']<(today+relativedelta(months=1)))&(df['電気代'].isnull()))]
38
+ df = df.rename(columns={'電気代':'電気代_予測'})
39
+ return df[['forecast_point', 'target_date', '電気代_予測']]
40
 
41
  def plot_prediction_result():
42
  update = gr.LinePlot.update(
43
  value=get_prediction_result(),
44
  x="target_date",
45
+ y="電気代_予測",
46
  title="昨日までの魚の卸売り量から予測された、来月の2人世帯の平均電気料金の推移",
47
  width=500,
48
+ height=300,
49
  )
50
  return update
51
+
52
+ def get_train_newest_target_period():
53
+ df = pd.read_csv('data/train.csv')
54
+ train_max_yearmonth = pd.to_datetime(str(df['年月'].max()), format='%Y%m').strftime('%Y年%m月')
55
+ return train_max_yearmonth
56
+
57
+ def get_newest_target_period():
58
+ df = get_estat.get_household_survey()
59
+ expence_df = pd.DataFrame({'年月':df['時間軸(月次)'].unique()})
60
+ cate='3.1 電気代'
61
+ temp_df = df.loc[df['品目分類(2020年改定)'] == cate]
62
+ unit = temp_df['unit'].unique()[0]
63
+ temp_df = temp_df.rename(columns={'value':f'{cate}_({unit})'})
64
+ expence_df = pd.merge(expence_df,
65
+ temp_df[['時間軸(月次)', f'{cate}_({unit})']].rename(columns={'時間軸(月次)':'年月'}),
66
+ on='年月',
67
+ how='left')
68
+ expence_df = expence_df.rename(columns={'3.1 電気代_(円)':'電気代'})
69
+ expence_df['年月'] = pd.to_datetime(expence_df['年月'], format='%Y年%m月').astype(str).apply(lambda x:''.join(x.split('-'))[:6]).astype(int)
70
+ target_max_yearmonth = pd.to_datetime(str(expence_df['年月'].max()), format='%Y%m').strftime('%Y年%m月')
71
+
72
+ return target_max_yearmonth
73
 
74
+ def get_model_infomation():
75
+ token = 'NjQwMDVmNGI0ZDQzZDFhYzI2YThmZDJiOnVZejljTXFNTXNoUnlKMStoUFhXSFdYMEZRck9lY3dobnEvRFZ1aVBHbVE9'
76
+ ### デモ環境これっぽい
77
+ endpoint = 'https://app.datarobot.com/api/v2'
78
+ dr.Client(
79
+ endpoint=endpoint,
80
+ token=token
81
+ )
82
+ model_info = pd.read_csv('data/model_management.csv').iloc[-1, :]
83
+ model = dr.Model.get(project = dr.Project.get(model_info['model_url'].split('/')[4]), model_id = model_info['model_url'].split('/')[-1])
 
84
 
85
+ feature_impact = pd.DataFrame(model.get_or_request_feature_impact())
86
+ feature_impact = feature_impact.sort_values('impactNormalized', ascending=False).reset_index(drop=True)
87
+ feature_impact = feature_impact.iloc[:20, :]
88
+
89
+ return model_info, feature_impact
90
 
91
 
92
  with gr.Blocks() as electoric_ploting:
93
+ gr.Markdown(
94
+ """
95
+ # その日の魚の卸売り量から、来月の家計データ月別支出の電気代を予測するAI
96
+ 使用データ
97
+ * 東京卸売市場日報
98
+ * 家計調査の月別支出
99
+ * 原油価格データ
100
+ * 燃料調達価格データ
101
+ why
102
+ 電気代のtrendは原油価格などが大きく影響するが、細かい変化は気候に影響し、気候はある程度海水温に関連性があると考えられる。
103
+ また、魚の卸売量は水揚げ量に関係し、水揚げ量は海水温に関係するという考えからモデルを作成。
104
+ """
105
+ )
106
  with gr.Row():
107
  with gr.Column():
108
  plot = gr.LinePlot(show_label=False)
109
  # plot = gr.Plot(label="昨日までの魚の卸売り量から予測された、来月の2人世帯の平均電気料金の推移")
110
+ with gr.Column():
111
+ df = get_prediction_result()
112
+ gr.Textbox(df['電気代_予測'].max(),
113
+ label='現在までの予測値の最大値')
114
+ gr.Textbox(df['電気代_予測'].min(),
115
+ label='現在までの予測値の最小値')
116
+ gr.Textbox(df['電気代_予測'].mean(),
117
+ label='現在までの予測値の平均値')
118
+ gr.Textbox(df['電気代_予測'].median(),
119
+ label='現在までの予測値の中央値')
120
  with gr.Row():
121
  gr.DataFrame(get_prediction_result)
122
 
123
  with gr.Column():
124
+
125
+ gr.Textbox(get_train_newest_target_period,
126
+ label='現在の学習済みのターゲット値最新月')
127
+ gr.Textbox(get_newest_target_period,
128
+ label='現在の取得可能ターゲット値最新月')
129
  btn= gr.Button(value="再学習")
130
  btn.click(retrain, inputs=None, outputs=None)
131
+
132
+ with gr.Row():
133
+ model_info, feature_impact_df = get_model_infomation()
134
+ gr.Textbox(model_info['model_type'],
135
+ label='現在のモデル')
136
+ with gr.Row():
137
+ for i in range(len(feature_impact_df)):
138
+ feature_impact_df['featureName'][i] = str(i+1).zfill(2) + '_' + feature_impact_df['featureName'][i]
139
+ gr.BarPlot(value = feature_impact_df,
140
+ title = '特徴量インパクト上位20',
141
+ x = 'featureName',
142
+ y = 'impactNormalized',
143
+ tooltip=['impactNormalized'],
144
+ x_title = '特徴量名',
145
+ y_title = '特徴量インパクト_相対値',
146
+ vertical=False,
147
+ y_lim=[0, 1.2],
148
+ width=400,
149
+ height=300)
150
+
151
 
152
 
153
  # demo.load(make_plot, inputs=[button], outputs=[plot])