Prophet time series forecasting algorithm

1. Background

Time series forecasting is a method of predicting future data. For the analysis of time series, we can use traditional statistical methods, such as ARIMA, Exponential Smoothing, etc. These methods analyze past data to build models to predict future trends, but A limitation of these methods is that certain assumptions must be met, such as data stability, handling of missing values, etc.

Therefore, some new time series forecasting methods have emerged in recent years, such as Facebook's open source Prophet , which is a highly flexible time series forecasting algorithm that can handle data instability, missing values, outliers, etc. , At the same time, it can automatically detect periodic trends and the impact of holidays, which can be applied to various industries and fields.


2. The basic principle of Prophet

The basic principle of Prophet is to decompose the time series into trend items, seasonal items and holiday items, and use the additive model to forecast on this basis. Specifically, assume that the time series y(t) consists of the following three parts:

  • Trend item g(t): describes the long-term trend of the time series, usually fitted by a piecewise linear model with automatic regression items and seasonal items.
  • Seasonal item s(t): describes the periodic change of the time series, usually fitted by Fourier series.
  • Holiday item h(t): Describes abnormal events in time series (such as holidays, promotions, etc.), usually represented by custom binary variables.
    Add the trend, seasonal, and holiday terms to get the forecast for the time series:

y(t) = g(t) + s(t) + h(t) + ε(t)

where ε(t) is the error term, usually assumed to be normally distributed.


3. How to use Prophet

1. Environment preparation

First, we need to install Prophet and the Python packages it depends on. It can be installed with the following command:

pip install fbprophet numpy pandas matplotlib

2. Data preparation

In order to make sales forecasts, we need to prepare some historical sales data first. Here we use a sample data set sales.csv, which contains two columns of date and sales. The dataset can be downloaded via the following link:
https://raw.githubusercontent.com/facebook/prophet/master/examples/example_retail_sales.csv

We can read the dataset using the Pandas library:

import pandas as pd

sales = pd.read_csv('sales.csv')

Next, we need to convert the dataset into the format required by Prophet.
Prophet requires the data set to contain at least two columns, ds and y , representing date and predicted value , respectively .
Here we set the ds column to be the date column and the y column to be the sales volume column.

sales = sales.rename(columns={
    
    'ds': 'date', 'y': 'sales'})
sales['date'] = pd.to_datetime(sales['date'])

Finally, we also need to split the dataset into training and testing sets. Here we use the last 12 months of data as the test set.

test_size = 12
train = sales.iloc[:-test_size]
test = sales.iloc[-test_size:]

3. Model training

The data is ready, and then we can start training the Prophet model. The usage process of Prophet is roughly as follows:

  1. Create a Prophet model object
  2. Fit the model to the training set data
  3. Use models to make predictions about future data
from fbprophet import Prophet

# 创建 Prophet 模型对象
model = Prophet()

# 拟合模型
model.fit(train)

# 构建未来时间序列
future = model.make_future_dataframe(periods=test_size, freq='M')

# 使用模型进行预测
forecast = model.predict(future)

In the above code, we create a Prophet model object model and fit it with the training set data.

Then, we use the make_future_dataframe method to construct a time series containing the next 12 months, and use the predict method to make predictions.

It should be noted that the prediction result of Prophet contains many columns, including: yhat is the predicted value, yhat_lower is the lower limit of the predicted value, and yhat_upper is the upper limit of the predicted value.

4. Evaluation optimization

After time series forecasting, we need to evaluate and optimize the forecast results.
Prophet provides many ways to evaluate the fit of the model, such as visualizing the prediction results, calculating the prediction error, etc.

1) Visualize the prediction results
import matplotlib.pyplot as plt

# 可视化预测结果
fig = model.plot(forecast)
plt.show()

Here, we use the plot method to visualize the predictions and then plot them using Matplotlib.

2) Calculate the prediction error
# 计算预测误差
from fbprophet.diagnostics import performance_metrics

df_perf = performance_metrics(forecast)

By evaluating the prediction results, we can determine the fitting degree of the model, and then optimize it, such as adjusting model parameters, adding more historical data, etc.

3) View the components of the forecast results
model.plot_components(forecast)

This will generate a visualization that includes trend, seasonality, and holiday effects.

The above are the basic steps of how to use Prophet for time series forecasting.

Of course, in practical applications, more data processing and adjustments (such as customizing seasonality, setting holidays, confidence intervals, etc.) may be required to ensure the best forecast results.


Four. Summary

Prophet is a powerful time series forecasting tool that is easy to use and has high forecasting accuracy.

If it is a scenario for predicting time series data, Prophet is recommended ! ! !


PS

For the description of Prophet parameters, please refer to the blog: https://www.cnblogs.com/miracle-luna/p/17368601.html


Guess you like

Origin blog.csdn.net/aikudexiaohai/article/details/130679275