Recently, I tried using Prophet for easy time series prediction. Since it's a big deal, I decided to share everything from introduction to operation, so I wrote an article.
Prophet is a time-series forecast package released by facebook as OSS (MIT license). The detailed theory is not touched upon here, but predictions are made using a generalized additive model.
python
#prophet_A virtual environment called env in python3.Created with 7 series(3.8 series fbprophet 0.Not compatible with 6)
conda create -n prophet_env python=3.7
#prophet created from base environment_Switch to env virtual environment
conda activate prophet_env
First, install the Visual Stuido Community.
Then install the following libraries
python
#Prophet introduced because it depends on PyStan
conda install plotly -y
#Introduced fbprophet(2020/11/Version at 1 is 0.7.It was 1, but I got an error, so 0.6)
conda install -c conda-forge fbprophet==0.6
#Because the environment is miniconda ...(I didn't put numpy and pandas individually because they happened to be introduced together in ↑)
conda install matplotlib
If no error occurs with the following command, installation is successful
python
from fbprophet import Prophet
This time, we will use a simple data set called "Air Passengers" as a sample data set. The Month column contains the time-series part, and the Passengers column contains the changes in the number of passengers. Dataset link
Month | #Passengers |
---|---|
1949/1/1 | 112 |
1949/2/1 | 118 |
1949/3/1 | 132 |
python
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from fbprophet import Prophet
#Read csv of AirPassengers
df = pd.read_csv('AirPassengers.csv')
python
#Prophet needs to change the column names to ds and y, so change them
df.columns = ['ds', 'y']
#This time, learn from the back of the data frame to the 10th in Prophet(fit)Let
m = Prophet()
m.fit(df[:-10])
python
'''
periods=Predict 10:10 squares
freq='M': Unit is "Month"
→ In other words, it means to predict for 10 months
'''
future = m.make_future_dataframe(periods=10, freq='M')
forecast = m.predict(future)
#Illustrate the result of forecast
fig = m.plot(forecast)
It should be displayed as ↓.
How was it? The program itself works easily, so it may be more difficult to install. Different versions may cause an error somewhere, and in fact I stumbled there at first. (PyStan, libpython, etc. were introduced together with fbprophet == 0.6, so they were not introduced individually in the end) It's easy, so please give it a try.
Recommended Posts