Easy time series prediction with Prophet

1.First of all

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.

2. What is Prophet?

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.

Official page github

3. Create a virtual environment for verification and switch to the virtual environment

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

4. Installation-operation check

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

5. Actually use

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

5-1: Until file reading

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')

5-2: Learn Prophet

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])

5-3: Predicted by Prophet

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 ↓.

6. Finally

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

Easy time series prediction with Prophet
I tried to implement time series prediction with GBDT
Forecasting time series data with Simplex Projection
Predict time series data with neural network
Play with Prophet
Time Series Decomposition
View details of time series data with Remotte
✨ Easy with Python ☆ Estimated elapsed time after death ✨
Python time series question
RNN_LSTM1 Time series analysis
Time series analysis 1 Basics
Time series data prediction by AutoML (automatic machine learning)
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Easy debugging with ipdb
Display TOPIX time series
Time series plot / Matplotlib
Easy TopView with OpenCV
Predict from various data in Python using Facebook Prophet, a time series prediction tool
I made a package to filter time series with python
Reformat the timeline of the pandas time series plot with matplotlib
Challenge to future sales forecast: ⑤ Time series analysis by Prophet
Easy tox environment with Jenkins
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Time series analysis related memo
Standard input with time limit
Easy folder synchronization with Python
Easy to make with syntax
Easy image classification with TensorFlow
Easy web scraping with Scrapy
Prediction of Nikkei 225 with Pytorch 2
Execution time measurement with Python With
Time series analysis part 4 VAR
Time series analysis Part 3 Forecast
Easy Python compilation with NUITKA-Utilities
[Python] Plot time series data
Easy HTTP server with Python
Easy proxy login with django-hijack
Prediction of Nikkei 225 with Pytorch
Time series analysis Part 1 Autocorrelation
Time synchronization (Windows) with Python
How to extract features of time series data with PySpark Basics
Comparison of time series data predictions between SARIMA and Prophet models