Mathematical modeling artifact-Facebook time series prediction library Prophet

At the beginning, I will first explain the background: these days I have been busy doing the school graduate mathematical modeling contest, the topic is 2020 MathorCup College Mathematical Modeling Challenge B question, interested children can go and see Ha, mainly Research on the future development trend of the national pension service beds (there is not enough data in the topic itself, so you need to collect it yourself, which is daunting). I originally thought of gray forecasting or ARIMA time series models. As a result, I accidentally discovered a very powerful Facebook open source Prophet time series forecasting tool. The operation is simple, so I wrote this blog to summarize the principle, installation and operation of Prophet. use.

1. The principle of Prophet algorithm

Prophet is a process of forecasting time series data based on additional models. Prophet is used in many Facebook applications to generate reliable planning and goal setting forecasts. In most cases, its performance is better than any other method. You can get reasonable predictions about messy data without manual work. Prophet is robust to outliers, missing data, and sharp changes in time series. It can handle outliers well and provides users with many possibilities to adjust and adjust predictions. Prophet can be implemented in R and Python. .

The specific principle can be viewed in the paper published by Facebook, which can be obtained for free on github, titled "Forecasting at Scale", delivery address:

  • https://facebook.github.io/prophet/
  • https://github.com/facebook/prophet

2. Installation of fbprophet library (for python)

Prophet can currently be called through Python and R. Here we only introduce how to install and use it on python (in fact, bloggers are not very good at using R). We will install the fbprophet library in the Anaconda virtual environment under Win10. The installation and use of Anaconda will be specifically summarized in the following blog. If you are interested, please click and pay attention!

(1) Open the created virtual environment

activate ML(虚拟环境名)

(2) Install PyStan

conda install pystan

(2) Install fbprophet library

conda install -c conda-forge fbprophet

(3) Install plotly

conda install plotly -y 

(4) Import fbprophet library

from fbprophet import Prophet

Note: Remember not to install directly using pip!


Three, the use of Prophet library

from fbprophet import Prophet  # 导入Prophet
# 数据预处理
train_data = dataset[['年份', '60岁以上人口数(万人)']]
train_data.columns = ['ds', 'y'] 
# 模型建立和训练,interval_width参数表示置信度
model = Prophet(interval_width=0.95)
model.fit(train_data)
# 数据预测,periods表示预测点数,freq表示预测频率
future = model.make_future_dataframe(periods=3,freq='YS') # 预测未来
forecast = model.predict(future)
model.plot(forecast) # 绘制图形

Insert picture description here

Guess you like

Origin blog.csdn.net/xylbill97/article/details/107311051