Prophet is a tool developed by Facebook to predict time series data. It seems that it is fast and fully automatic. Let's use it for a moment.
Uses the daily Dow Jones Industrial Average since 1948.
pip install fbprophet
import fbprophet
fbprophet.__version__
#'0.6'
The format must be columns = ["ds", "y"]
.
ds | y | |
---|---|---|
18356 | 2020-04-23 | 23515.26 |
18357 | 2020-04-24 | 23775.27 |
18358 | 2020-04-27 | 24133.78 |
18359 | 2020-04-28 | 24101.55 |
18360 | 2020-04-29 | 24633.86 |
18361 | 2020-04-30 | 24345.72 |
18362 | 2020-05-01 | 23723.69 |
18363 | 2020-05-04 | 23749.76 |
18364 | 2020-05-05 | 23883.09 |
18365 | 2020-05-06 | 23664.64 |
18366 | 2020-05-07 | 23875.89 |
18367 | 2020-05-08 | 24331.32 |
m = Prophet(daily_seasonality=True)
m.fit(df)
If the frequency of time series data is not daily but hourly, set daily_seasonality = True
.
future = m.make_future_dataframe(periods=365)
future.tail()
ds | |
---|---|
18728 | 2021-05-04 |
18729 | 2021-05-05 |
18730 | 2021-05-06 |
18731 | 2021-05-07 |
18732 | 2021-05-08 |
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
ds | yhat | yhat_lower | yhat_upper | |
---|---|---|---|---|
18728 | 2021-05-04 | 25240.993067 | 23775.034765 | 26676.954454 |
18729 | 2021-05-05 | 25241.812462 | 23873.631394 | 26743.879477 |
18730 | 2021-05-06 | 25248.372948 | 23662.176440 | 26658.218006 |
18731 | 2021-05-07 | 25251.123010 | 23590.352159 | 26721.447848 |
18732 | 2021-05-08 | 25258.034603 | 23780.066094 | 26742.194673 |
Here, I will explain the variables -** yhat : Predicted value - yhat_lower : Lower limit of predicted error - yhat_upper **: Upper limit of predicted error
fig1 = m.plot(forecast)
It is generally within the margin of error.
fig2 = m.plot_components(forecast)
Sure, the quote says "Sell in May and go away, and come on back on St. Leger's Day." (Sell stocks in May and don't come back until St. Leger's Day (mid-September)) There is a saying. This seems to be something (laughs)
This is the basic usage, please see the document for more details.
Recommended Posts