Time series forecast based on fbprophet

Time series forecast based on fbprophet

Due to business needs, it is necessary to make sales forecasts for a period of time in the future. This may be a bit metaphysical in the past. With the development of data science (machine learning), such problems are classified as forecasts based on time series, and they also produce Various time series prediction algorithm models, such as ARMA/ARIMA, exponential smoothing Holt-winters, deep learning-based LSTM, etc., also include fbprophet, which is also the protagonist of this article. Compared with the previous several algorithm models, fbprophet is more It's easy to use, and the effect is obvious.
Time series forecasting belongs to supervised learning. In essence, it inputs historical data sets to the computer, and the computer fits a curve (a mathematical function) that is closer to the true value (not as close as possible) according to a certain algorithm, and based on the root This function makes predictions about future data.

Prophet model

Prophet is a time series forecasting algorithm open sourced by Facebook in 2017. Facebook claims that the default Prophet model can generate forecasts comparable to professional data analysts. Even if you are not an expert in time series analysis, you can understand the important parameters of the model. Prophet regards the predicted value as a function of time, and its model is as follows:

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

Among them:
g(t) represents the trend, influencing factors such as population (or number of users) growth, the maximum upper limit (ceiling) of industry development, etc. For this item, prophet provides two implementations: piecewise linear (linear) and piecewise logistic regression (logistic).
s(t) represents periodicity. It is used to fit the periodicity of the data, which can be yearly, By quarter, month, week, day, etc. For example, for offline stores, there will be more passenger flow on weekends, and their sales will be higher.
h(t) stands for holiday, which is used to fit holidays or the impact of promotional activities.
εt is the noise term, which represents some unexpected factors, used to fit the effects such as power outages, severe weather, and natural disasters.
Note: Each item above can be understood as a complex mathematical function. We will not expand it here. Those who are interested can refer to: https://blog.csdn.net/a358463121/article/details/70194279

Common parameter description

Commonly used parameters are mainly divided into take advantage type, period type, holiday, etc. The equal sign represents the default.

Trend related

growth='linear',#模型的趋势函数:分段线性:linear,逻辑回归:logistic
changepoints=None,#突变点集合
n_changepoints=25,#突变点个数
changepoint_range=0.8,#突变点所在的数据范围,默认为数据集的前80%
changepoint_prior_scale=0.05,#自动突变点选择的灵活性,值越大越容易出现changepoint

Cycle related

yearly_seasonality='auto',#年相关性
weekly_seasonality='auto',#周相关性
daily_seasonality='auto',#日相关性
seasonality_mode='additive',#周期性模型使用的拟合模型,默认使用加法模型,seasonality_mode='multiplicative'表示乘法模型
seasonality_prior_scale=10.0,#周期性相关度,值越大即呈现的周期性规率越明显

Holiday related

holidays=None,#指定节假日列表
holidays_prior_scale=10.0,#节假日相关度,值越大即表示节假日因素的影响越大

Simple entry

Take the time series data of daily visits to Peyton Manning's Wikipedia homepage (2007/12/10-2016/01/20) provided by official documents as an example to perform model fitting and parameter adjustment.

import pandas as pd
from fbprophet import Prophet
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
# 读入数据集
whole_df = pd.read_csv('example_wp_log_peyton_manning.csv')
whole_df

Insert picture description here
See what the data looks like

df = whole_df[whole_df['ds']>'2013-01-01']#为显示清晰,只取3年的数据
df.index=df['ds']
df.plot(figsize=(32,8))

Insert picture description here
Make simple data predictions

m = Prophet() #创建Prophet对象来拟合模型,这里先采用默认参数
m.fit(df) #代入数据集来拟合模型
fday = 30 #要预测的天数
future = m.make_future_dataframe(periods=fday)#将数据集扩充至未来的天数,同时包含历史数据
forecast = m.predict(future)#执行预测:对每一个日期的历史数据生成一个预测值(称为 yhat )
m.plot(forecast,figsize=(32, 8))#将预测的效果进行绘图

Insert picture description here
Interpretation: The discrete dots are the actual values, and the blue lines are the predicted values.
Until the forecast results have been released, if you look at the trend of the model, as well as the annual seasonality and weekly seasonality, you can use:

m.plot_components(forecast)

Insert picture description here

Error evaluation and tuning

The above few lines of code can achieve prediction, isn't it too simple? The prediction results produced by the model built with the default parameters may not be very ideal. We need to split the historical data set into a training set and a test set, and then continuously adjust the parameters and compare the prediction results with the test set to calculate the error , And finally solve the parameter with the smallest error, and use this parameter to predict the future data.

Insert picture description here
Iteratively solve the optimal parameters

#寻参:为简化for次数,这里只列出两个参数,实际需要迭代的会更多
def find_params(df,fdays):
    train_size = len(df) - fdays
    train_df,test_df=df[0:train_size], df[train_size:len(df)]
    seasonality_modes = ['additive', 'multiplicative']
    seasonality_prior_scales = [0.01,0.03, 0.1, 0.3, 1, 3, 10,30]
    #holidays_prior_scales = [0.01,0.05, 0.2, 1, 5, 10, 20] #事实上可以加入更多参数,这里略去
    min_err = -1
    best_params = None
    for mode in seasonality_modes:
        for sp_scale in seasonality_prior_scales:
            params = {
    
    
                            "seasonality_mode": mode,
                            "seasonality_prior_scale": sp_scale
                    }
            avg_err = predict_one(train_df,test_df,fdays,params)
            if(avg_err < min_err):
                min_err = avg_err
                best_params = params
            print(f'{params}:{avg_err}')
                
print(f'最小平均误差:{min_err},最优参数:{best_params}')
#根据参数进行一次预测,并算出预测的误差
def predict_one(train_df,test_df,fdays,params):
    m = Prophet(**params)
    m.fit(train_df)
    future = m.make_future_dataframe(periods=fdays)#将数据集扩充至未来的天数,同时包含历史数据
    forecast = m.predict(future).tail(fdays)
    forecast.index = forecast['ds'].map(lambda x:x.strftime('%Y-%m-%d'))
    return mean_forecast_err(test_df['y'],forecast['yhat'])
#计算平均误差
def mean_forecast_err(y, yhat):
    return y.sub(yhat).abs().mean()

结果如下:
{‘seasonality_mode’: ‘additive’, ‘seasonality_prior_scale’: 0.01}:0.46835404175700496
{‘seasonality_mode’: ‘additive’, ‘seasonality_prior_scale’: 0.03}:0.47134440199356015
{‘seasonality_mode’: ‘additive’, ‘seasonality_prior_scale’: 0.1}:0.47418518322082714
{‘seasonality_mode’: ‘additive’, ‘seasonality_prior_scale’: 0.3}:0.4748634038391658
{‘seasonality_mode’: ‘additive’, ‘seasonality_prior_scale’: 1}:0.47178295035945506
{‘seasonality_mode’: ‘additive’, ‘seasonality_prior_scale’: 3}:0.47382680395375665
{‘seasonality_mode’: ‘additive’, ‘seasonality_prior_scale’: 10}:0.4750603929631507
{‘seasonality_mode’: ‘additive’, ‘seasonality_prior_scale’: 30}:0.4710919693964274
{‘seasonality_mode’: ‘multiplicative’, ‘seasonality_prior_scale’: 0.01}:0.4699275213869728
{‘seasonality_mode’: ‘multiplicative’, ‘seasonality_prior_scale’: 0.03}:0.46863816555668125
{‘seasonality_mode’: ‘multiplicative’, ‘seasonality_prior_scale’: 0.1}:0.4709381698222656
{‘seasonality_mode’: ‘multiplicative’, ‘seasonality_prior_scale’: 0.3}:0.47118585242058947
{‘seasonality_mode’: ‘multiplicative’, ‘seasonality_prior_scale’: 1}:0.47044784615578944
{‘seasonality_mode’: ‘multiplicative’, ‘seasonality_prior_scale’: 3}:0.46960144311702073
{‘seasonality_mode’: ‘multiplicative’, ‘seasonality_prior_scale’: 10}:0.4688539147158322
{‘seasonality_mode’: ‘multiplicative’, ‘seasonality_prior_scale’: 30}:0.4677488199423377
Minimum average error: 0.4677488199423377, optimal parameters: {'seasonality_mode':'multiplicative','seasonality_prior_scale': 30}

Use the trained parameters to make the final prediction

At this time, you only need to substitute the parameters into the Prophet model to complete the prediction. The code is slightly
referenced
[1]: https://blog.csdn.net/anshuai_aw1/article/details/83412058
[2]: https://facebook.github. io/prophet/docs/quick_start.html#python-api

Guess you like

Origin blog.csdn.net/vipshop_fin_dev/article/details/110360819
Recommended