๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
fbprophet ์๊ณ์ด ๋ฐ์ดํฐ๋ก ์น ํธ๋ํฝ ๊ฐ์ง ๋ณธ๋ฌธ
fbprophet ์๊ณ์ด ๋ฐ์ดํฐ๋ก ์น ํธ๋ํฝ ๊ฐ์ง
์ง์ง์ํ์นด 2022. 9. 23. 16:49220923 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ HyeongWookKim ๋์ ๊นํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-) >
https://gist.github.com/HyeongWookKim/c8f31f30b233896bb8947622d7efaf82
[Ch 7. ์๊ณ์ด ๋ฐ์ดํฐ๋ฅผ ๋ค๋ค๋ณด์] from "ํ์ด์ฌ์ผ๋ก ๋ฐ์ดํฐ ์ฃผ๋ฌด๋ฅด๊ธฐ(๋ฏผํ๊ธฐ ์ง์)"
[Ch 7. ์๊ณ์ด ๋ฐ์ดํฐ๋ฅผ ๋ค๋ค๋ณด์] from "ํ์ด์ฌ์ผ๋ก ๋ฐ์ดํฐ ์ฃผ๋ฌด๋ฅด๊ธฐ(๋ฏผํ๊ธฐ ์ง์)" - Ch 7. ์๊ณ์ด ๋ฐ์ดํฐ๋ฅผ ๋ค๋ค๋ณด์.ipynb
gist.github.com
1๏ธโฃ libraries & data load
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import pandas_datareader.data as web
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from fbprophet import Prophet
from datetime import datetime
- ๋ ์ง(index), ๋ฐฉ๋ฌธ์(hit)
pinkwink_web = pd.read_csv("08. PinkWink Web Traffic.csv",
encoding = 'utf-8', thousands = ',',
names = ['date', 'hit'], index_col = 0)
pinkwink_web = pinkwink_web[pinkwink_web['hit'].notnull()]
pinkwink_web.head()
- 2016๋ 7์ 1์ผ๋ถํฐ 2017๋ 6์ 16์ผ๊น์ง์ ์ ์ ๋
pinkwink_web['hit'].plot(figsize = (12, 4), grid = True);
๐ค '์ ํ ํ๊ท ์ง์ ' ๋ฐ '๋คํญ ํ๊ท์'์ ํํ
time = np.arange(0, len(pinkwink_web)) # ์๊ฐ์ถ(time) ์์ฑ (0๋ถํฐ 365๊น์ง)
traffic = pinkwink_web['hit'].values # ์น ํธ๋ํฝ์ ์๋ฃ๋ฅผ traffic ๋ณ์์ ์ ์ฅ
fx = np.linspace(0, time[-1], 1000) # 0๋ถํฐ 364๊น์ง๋ฅผ 1000๊ฐ๋ก ๋ถํ
- RMSE(Root Mean Square Error)์ ๊ณ์ฐํด์ฃผ๋ error() ํจ์๋ฅผ ์์ฑ
def error(f, x, y):
return np.sqrt(np.mean((f(x) - y) ** 2)) # RMSE(Root Mean Square Error)
- ๊ฐ ์ฐจ์๋ณ ๋คํญ์์ 'RMSE(Root Mean Square Error)'์ ๊ณ์ฐํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ ์๊ฐํ
- 1์ฐจ, 2์ฐจ, 3์ฐจ ๋คํญ์์ RMSE(Root Mean Square Error)๋ ๊ฑฐ์ ๋น์ท
- 15์ฐจ ๋คํญ์์ RMSE(Root Mean Square Error)๋ ๋น๊ต์ ๋ฎ์
- => ๋ชจ๋ธ์ด ๋ณต์กํด์ง์๋ก ์ฃผ์ด์ง ๋ฐ์ดํฐ์ ๋ํ fitting ์ ํ๋๊ฐ ๋์์ง๊ธฐ ๋๋ฌธ
- => ๋ชจ๋ธ์ด ๋๋ฌด ๋ณต์กํ๋ฉด ๊ณผ์ ํฉ(overfitting) ๋ฌธ์ ๊ฐ ๋ฐ์
# 1์ฐจ ๋คํญ์
fp1 = np.polyfit(time, traffic, 1)
f1 = np.poly1d(fp1)
# 2์ฐจ ๋คํญ์
f2p = np.polyfit(time, traffic, 2)
f2 = np.poly1d(f2p)
# 3์ฐจ ๋คํญ์
f3p = np.polyfit(time, traffic, 3)
f3 = np.poly1d(f3p)
# 15์ฐจ ๋คํญ์
f15p = np.polyfit(time, traffic, 15)
f15 = np.poly1d(f15p)
# ๊ฐ ์ฐจ์๋ณ ๋คํญ์์ '์์ฐจ ์ ๊ณฑํฉ'์ ๊ณ์ฐ
print(error(f1, time, traffic))
print(error(f2, time, traffic))
print(error(f3, time, traffic))
print(error(f15, time, traffic))
plt.figure(figsize = (10, 6))
plt.scatter(time, traffic, s = 10)
plt.plot(fx, f1(fx), lw = 4, label = 'f1')
plt.plot(fx, f2(fx), lw = 4, label = 'f2')
plt.plot(fx, f3(fx), lw = 4, label = 'f3')
plt.plot(fx, f15(fx), lw = 4, label = 'f15')
plt.grid(True, linestyle = '-', color = '0.75')
plt.legend(loc = 2)
plt.show()
2๏ธโฃ Prophet ๋ชจ๋์ ์ด์ฉํ forecast ์์ธก
- pinkwink_web ๋ณ์์์ ๋ ์ง(index), ๋ฐฉ๋ฌธ์(hit)๋ง ๋ฐ๋ก ์ ์ฅ
- pandas์ to_datetime() ํจ์๋ฅผ ์ฌ์ฉํด์ 'ds' ๋ณ์๋ฅผ ๋ ์ง๋ก ์ ์ธ
- Prophet() ํจ์๋ฅผ ์ฌ์ฉํ ๋, ์ฃผ๊ธฐ์ฑ์ด ์ฐ๋จ์(yearly_seasonality) ๋ฐ ์ผ๋จ์(daily_seasonality)
df = pd.DataFrame({'ds': pinkwink_web.index, 'y': pinkwink_web['hit']})
df.reset_index(inplace = True)
df['ds'] = pd.to_datetime(df['ds'], format = "%y. %m. %d.") # "yyyy-mm-dd" ํํ๋ก ๋ณ๊ฒฝ
# 'ds' ๋ณ์๋ฅผ ์์ฑํด์คฌ์ผ๋ฏ๋ก, 'date' ๋ณ์๋ ์ญ์
del df['date']
m = Prophet(yearly_seasonality = True, daily_seasonality = True)
m.fit(df)
- make_future_dataframe()๋ก ์ง์ ๋ ๋ ์ง ์๋งํผ ๋ฏธ๋๋ก ํ์ฅํ๋ ์ ์ ํ ๋ฐ์ดํฐ ํ๋ ์ ์ป๊ธฐ
- 60์ผ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์์ธก
future = m.make_future_dataframe(periods = 60)
future.tail()
- predict()๋ฅผ ์ฌ์ฉํ์ฌ ์์ธกํ ๋ฐ์ดํฐ๋ฅผ forecast ๋ณ์์ ์ ์ฅ
forecast = m.predict(future)
forecast.head()
- ํ์ธํด๋ณด๊ณ ์ ํ๋ ๋ณ์๋ค๋ง ๋ฝ์๋ด์ ํ์ธ
- 'ds', 'yhat', 'yhat_lower', 'yhat_upper'
- ์๊ฐํ๋ฅผ ํตํด ์์ธก ๊ฒฐ๊ณผ๋ฅผ ํ์ธ
- 2017๋ 6์ ๋ง๊น์ง์ ๋ฐ์ดํฐ ์ดํ, ์ฝ 2๊ฐ์(60์ผ)์ ์์ธก ๊ฒฐ๊ณผ
- ๋จ์ํ ๋คํญ์์ผ๋ก ๊ฒฝํฅ์ ํ์ ํ๋ ๊ฒ๋ณด๋ค ๋ ํจ๊ณผ์
m.plot(forecast);
- plot_components()๋ฅผ ์ฌ์ฉํด์ ์ ํ ํ๊ท ๋ฐ ๊ณ์ ์ฑ ์ฑ๋ถ ๋ณ๋ก ๋ถํด
- = forecast component ์๊ฐํ (Trend, Holidays, Weakly, Yearly, Daily)
m.plot_components(forecast);
์ถ์ธ์ฑ (Trend) | ๋ฐ์ดํฐ์ ์ฅ๊ธฐ์ ๋ณ๋ |
์ํ์ฑ (Cycle) | ์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง์ง๋ง ์ผ์ ํ์ง ์๊ณ ๋ฐ๋ณต์ด ์๋ ๋ฐ์ดํฐ์ ๋ณ๋ |
๊ณ์ ์ฑ (Seasonality) | ์ผ์ ํ ์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง๊ณ ๋ฐ๋ณต๋๋ ๋ฐ์ดํฐ์ ๋ณ๋ |
๋ถ๊ท์น์ฑ (Noise) | ์ ์ ์๊ฑฐ๋ ๋๋ฐ์ ์ธ ์์ธ์ ์ํด ๋ฐ์ํ๋ ๋ฐ์ดํฐ์ ๋ณ๋ |
plot_components ๋ก ์ํฉ๋ณ ๊ทธ๋ํ ๋ถ์์ด ๊ฐ๋ฅํ๋ค๋..
์ ๊ธฐํ๋ค.... ํฌ..