Spaces:
Runtime error
Runtime error
masa729406
commited on
Commit
·
1cbad75
1
Parent(s):
d0ab8cf
Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,8 @@ from sklearn.linear_model import LinearRegression
|
|
16 |
from sklearn.metrics import log_loss
|
17 |
from sklearn.preprocessing import StandardScaler
|
18 |
|
|
|
|
|
19 |
import requests
|
20 |
from bs4 import BeautifulSoup as bs
|
21 |
from requests_html import AsyncHTMLSession
|
@@ -32,6 +34,7 @@ from typing import Generator, Tuple
|
|
32 |
import numpy as np
|
33 |
import pandas as pd
|
34 |
|
|
|
35 |
def date_range(
|
36 |
start: date, stop: date, step: timedelta = timedelta(1)
|
37 |
) -> Generator[date, None, None]:
|
@@ -199,26 +202,20 @@ model = linear_regression.fit(X,y)
|
|
199 |
|
200 |
d_today = datetime.date.today()
|
201 |
d_tom = datetime.date.today() + datetime.timedelta(days = 1)
|
202 |
-
|
203 |
-
# 予測日前日の魚データを取得
|
204 |
d_y = datetime.date.today() + datetime.timedelta(days = -1)
|
|
|
|
|
205 |
if __name__ == "__main__":
|
206 |
start_date = d_y
|
207 |
end_date = d_today
|
208 |
df_aji_pre = get_fish_price_data(start_date=start_date, end_date=end_date)
|
209 |
df_aji_pre['date'] = df_aji_pre['date'].astype(int)
|
210 |
|
211 |
-
# if __name__ == "__main__":
|
212 |
-
# start_date = d_today
|
213 |
-
# end_date = d_tom
|
214 |
-
# df_aji_pre = get_fish_price_data(start_date=start_date, end_date=end_date)
|
215 |
-
# df_aji_pre['date'] = df_aji_pre['date'].astype(int)
|
216 |
-
|
217 |
url23 = 'https://www.football-lab.jp/ka-f/match/'
|
218 |
dfs23 = pd.read_html(url23)
|
219 |
|
220 |
#シーズン毎に分類
|
221 |
-
res23 = pd.DataFrame([['S2023']]*len(dfs23[0])).join(dfs23
|
222 |
|
223 |
df = res23
|
224 |
|
@@ -243,61 +240,34 @@ df["date_before"] = df["date_before"]
|
|
243 |
df = df[['audience', 'date_ymd', 'date_before']]
|
244 |
df['last_audience'] = df['audience'].shift(1)
|
245 |
|
246 |
-
# df_pre = pd.merge(df, df_aji_pre, left_on='date_before', right_on='date', how='left')
|
247 |
-
|
248 |
-
# df_pre = df_pre.drop(['date_before', 'date_ymd'], axis=1)
|
249 |
-
# df_pre["audience"] = df_pre["audience"].str.replace(",", "").astype(int)
|
250 |
-
# df_pre["last_audience"] = df_pre["last_audience"].str.replace(",", "").astype(int)
|
251 |
-
|
252 |
-
# start_date = int(start_date)
|
253 |
-
# df_pre = df.query('date <= start_date')
|
254 |
-
|
255 |
-
|
256 |
df_pre = df.tail(1).reset_index()
|
257 |
df_pre = df_pre.drop('index', axis=1)
|
258 |
df_aji_ft_pre = pd.concat([df_pre, df_aji_pre], axis=1)
|
259 |
-
df_aji_ft_pre = df_aji_ft_pre[['
|
260 |
-
df_aji_ft_pre = df_aji_ft_pre.rename(columns={'audience': 'last_audience', 0: 'year', '開催日': '
|
|
|
|
|
|
|
|
|
|
|
261 |
|
262 |
def outbreak(date):
|
263 |
-
if date:
|
264 |
-
|
265 |
-
# if __name__ == "__main__":
|
266 |
-
# import datetime
|
267 |
-
# d_today = datetime.date.today()
|
268 |
-
# d_tom = datetime.date.today() + datetime.timedelta(days = 1)
|
269 |
-
# start_date = d_today
|
270 |
-
# end_date = d_tom
|
271 |
-
# df_aji_pre = get_fish_price_data(start_date=start_date, end_date=end_date)
|
272 |
-
# # df_aji_pre.to_csv("fish_price_pre.csv", index=False)
|
273 |
-
|
274 |
-
df_pre = df.tail(1).reset_index()
|
275 |
-
df_pre = df_pre.drop('index', axis=1)
|
276 |
-
df_aji_ft_pre = pd.concat([df_pre, df_aji_pre], axis=1)
|
277 |
-
df_aji_ft_pre = df_aji_ft_pre[['audience', 'date', 'low_price', 'center_price', 'high_price', 'quantity']]
|
278 |
-
df_aji_ft_pre = df_aji_ft_pre.rename(columns={'audience': 'last_audience', 0: 'year', '開催日': 'date', '観客数': 'audience'})
|
279 |
-
|
280 |
-
X = df_train.drop('audience', axis=1)
|
281 |
-
y = df_train['audience']
|
282 |
-
|
283 |
-
pred = linear_regression.predict(df_aji_ft_pre)
|
284 |
-
df_aji_ft_pre['audience_pred'] = pred
|
285 |
-
df_aji_ft_pre['date'] = df_aji_ft_pre['date'].astype(int)
|
286 |
|
287 |
fig = plt.figure()
|
288 |
-
plt.plot(df_train['
|
289 |
-
plt.plot(df_aji_ft_pre['
|
290 |
plt.title("prediction of audince")
|
291 |
plt.ylabel("audience")
|
292 |
-
plt.xlabel("
|
293 |
plt.legend()
|
294 |
return fig
|
295 |
|
296 |
with gr.Blocks() as demo:
|
297 |
gr.Markdown(
|
298 |
"""
|
299 |
-
#
|
300 |
-
|
301 |
## 使用データ
|
302 |
* 東京卸売市場日報
|
303 |
* Football Lab
|
@@ -305,15 +275,11 @@ with gr.Blocks() as demo:
|
|
305 |
観客動員数は雨天か否かで左右されると考えられる。そこで雨天の可能性をあじの価格を利用し表した。
|
306 |
一般的に雨天の場合、低気圧の影響で海面が上昇し漁に出ることが難しくなる。
|
307 |
そのため漁獲量が減少し、あじの価格が上昇すると考えられる。
|
308 |
-
## 特徴量
|
309 |
-
予測日前日のあじの高値、予測日前日のあじの中値、予測日前日のあじの安値、予測日前日のあじの卸売数量、等々力競技場で行われた前回の試合の観客動員数
|
310 |
-
##注意点
|
311 |
-
予測日前日の東京卸売市場のデータがない場合はErrorとなります。
|
312 |
"""
|
313 |
)
|
314 |
with gr.Row():
|
315 |
with gr.Column():
|
316 |
-
date_input = gr.Checkbox(label='
|
317 |
prediction_btn = gr.Button(value="predict")
|
318 |
with gr.Column():
|
319 |
prediction = gr.Plot(label = "時系列プロット")
|
|
|
16 |
from sklearn.metrics import log_loss
|
17 |
from sklearn.preprocessing import StandardScaler
|
18 |
|
19 |
+
!pip install beautifulsoup4
|
20 |
+
!pip install requests_html
|
21 |
import requests
|
22 |
from bs4 import BeautifulSoup as bs
|
23 |
from requests_html import AsyncHTMLSession
|
|
|
34 |
import numpy as np
|
35 |
import pandas as pd
|
36 |
|
37 |
+
|
38 |
def date_range(
|
39 |
start: date, stop: date, step: timedelta = timedelta(1)
|
40 |
) -> Generator[date, None, None]:
|
|
|
202 |
|
203 |
d_today = datetime.date.today()
|
204 |
d_tom = datetime.date.today() + datetime.timedelta(days = 1)
|
|
|
|
|
205 |
d_y = datetime.date.today() + datetime.timedelta(days = -1)
|
206 |
+
|
207 |
+
# 動作確認
|
208 |
if __name__ == "__main__":
|
209 |
start_date = d_y
|
210 |
end_date = d_today
|
211 |
df_aji_pre = get_fish_price_data(start_date=start_date, end_date=end_date)
|
212 |
df_aji_pre['date'] = df_aji_pre['date'].astype(int)
|
213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
url23 = 'https://www.football-lab.jp/ka-f/match/'
|
215 |
dfs23 = pd.read_html(url23)
|
216 |
|
217 |
#シーズン毎に分類
|
218 |
+
res23 = pd.DataFrame([['S2023']]*len(dfs23[0])).join(dfs23, lsuffix='0')
|
219 |
|
220 |
df = res23
|
221 |
|
|
|
240 |
df = df[['audience', 'date_ymd', 'date_before']]
|
241 |
df['last_audience'] = df['audience'].shift(1)
|
242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
df_pre = df.tail(1).reset_index()
|
244 |
df_pre = df_pre.drop('index', axis=1)
|
245 |
df_aji_ft_pre = pd.concat([df_pre, df_aji_pre], axis=1)
|
246 |
+
df_aji_ft_pre = df_aji_ft_pre[['date_ymd', 'audience', 'low_price', 'center_price', 'high_price', 'quantity']]
|
247 |
+
df_aji_ft_pre = df_aji_ft_pre.rename(columns={'audience': 'last_audience', 0: 'year', '開催日': 'date_ymd', '観客数': 'audience'})
|
248 |
+
df_aji_ft_pre ['last_audience'] = df_aji_ft_pre ['last_audience'].astype(int)
|
249 |
+
|
250 |
+
pred = linear_regression.predict(df_aji_ft_pre)
|
251 |
+
df_aji_ft_pre['audience_pred'] = pred
|
252 |
+
df_aji_ft_pre['date_ymd'] = df_aji_ft_pre['date_ymd'].astype(int)
|
253 |
|
254 |
def outbreak(date):
|
255 |
+
if date:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 |
|
257 |
fig = plt.figure()
|
258 |
+
plt.plot(df_train['date_ymd'], df_train['audience'], label='original')
|
259 |
+
plt.plot(df_aji_ft_pre['date_ymd'], df_aji_ft_pre['audience_pred'], '*', label='predict')
|
260 |
plt.title("prediction of audince")
|
261 |
plt.ylabel("audience")
|
262 |
+
plt.xlabel("Days")
|
263 |
plt.legend()
|
264 |
return fig
|
265 |
|
266 |
with gr.Blocks() as demo:
|
267 |
gr.Markdown(
|
268 |
"""
|
269 |
+
# 川崎フロンターレの観客動員数の予測
|
270 |
+
川崎フロンターレの等々力陸上競技場での試合の観客数を「あじ」の価格をもとに予測する。
|
271 |
## 使用データ
|
272 |
* 東京卸売市場日報
|
273 |
* Football Lab
|
|
|
275 |
観客動員数は雨天か否かで左右されると考えられる。そこで雨天の可能性をあじの価格を利用し表した。
|
276 |
一般的に雨天の場合、低気圧の影響で海面が上昇し漁に出ることが難しくなる。
|
277 |
そのため漁獲量が減少し、あじの価格が上昇すると考えられる。
|
|
|
|
|
|
|
|
|
278 |
"""
|
279 |
)
|
280 |
with gr.Row():
|
281 |
with gr.Column():
|
282 |
+
date_input = gr.Checkbox(label='please input date')
|
283 |
prediction_btn = gr.Button(value="predict")
|
284 |
with gr.Column():
|
285 |
prediction = gr.Plot(label = "時系列プロット")
|