prophet源码阅读

1.项目简介

1.1.prophet简介

prophet是由facebook开发的一款时间序列预测的工具,他具有以下的优缺点:

  • 优点:
    1.易用性高: Prophet 设计简单,易于上手,无需对时间序列预测领域有深入的专业知识,使得非专业人士也能够快速使用。

    2.灵活性强: Prophet 提供了许多参数和选项,可以根据不同的数据特点和预测需求进行调整,灵活适应各种场景。

    3.自动化处理: Prophet 可以自动处理数据中的缺失值和异常值,自动检测并适应节假日效应,减轻了用户的预处理负担。
    4,可解释性强: Prophet 的预测模型基于可解释性强的分解方式,能够直观地展示出趋势、季节性和假日效应等,便于用户理解和解释预测结果。

  • 缺点:
    1.局限性: Prophet 的预测模型相对简单,无法处理一些复杂的时间序列模式和特征,例如非线性趋势、多重季节性等。

    2.性能问题: 对于大规模的时间序列数据,Prophet 可能会面临性能问题,模型拟合和预测的时间较长,不适用于实时或高频率的预测任务。

    3.不适用于所有场景: 由于其简单的模型结构和特性限制,Prophet 不适用于所有的时间序列预测问题,尤其是一些复杂的、高度非线性的场景。

2.核心代码

2.1.数据处理

  • 数据读取主要是使用pandas库来读取

    1
    2
    3
    import pandas as pd
    pd.read_csv(path)

  • 读取时序数据后,Prophet 在 forecaster.py 中的 _make_future_dataframe 函数中对输入数据进行了处理。

2.2.模型实例化模块

在forecaster.py模块中的Prophet类
实例化只需要调用

1
2
form prophet import Prophet
m = Prophet()

以上两行代码就将prophet对象实例化给了m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def __init__(
self,
growth='linear',
changepoints=None,
n_changepoints=25,
changepoint_range=0.8,
yearly_seasonality='auto',
weekly_seasonality='auto',
daily_seasonality='auto',
holidays=None,
seasonality_mode='additive',
seasonality_prior_scale=10.0,
holidays_prior_scale=10.0,
changepoint_prior_scale=0.05,
mcmc_samples=0,
interval_width=0.80,
uncertainty_samples=1000,
stan_backend=None,
scaling: str = 'absmax',
holidays_mode=None,
):

由上方的Prophet类__init__()参数可以看到由很多可选参数

以下分析一些较为常见的参数设置:

  • growth:默认为linear,可选logistic和flat
  • changepoints:可指定历史数据中哪天为changepoint
  • n_changepoints:设置潜在变化点,默认为25
  • changepoint_rangem:推断历史数据的前多少百分比,默认情况为0.8
  • changepoint_prior_scale:设置趋势的灵活性,如果趋势变化是过拟合(灵活性过高)或拟合欠(灵活性不足),则可以使用输入参数调整稀疏先验的强度。默认情况下,此参数设置为 0.05
  • yearly_seasonlity:默认为‘auto’,表示当历史数据大于一年时自动分析yearly的周期性变化
  • weakly_seasonlity与daily_seasonlity同yearly_seasonlity
  • holidays:属于外部影响因素,为Dataframe形式,可以为python内定的节假日也可以是自己指定的节假日
  • seasonality_mode:表示预测结果是trend+seasonlity还是trend*seasonlity,大部分工业应用为乘法模式,prophet默认为加法模式

Prophet类中一些常用的函数:

  • def add_country_holidays(self, country_name):
    这是prophet中加入内置节假日的函数,使用方法如下:

    1
    2
    3
    4
    m = Prophet(holidays=holidays)
    m.add_country_holidays(country_name='US')
    m.fit(df)
    forecast = m.predict(future
  • def add_seasonality(self, name, period, fourier_order, prior_scale=None,

                      mode=None, condition_name=None):
    

自定义周期函数:
不同与一个月,一周,一年的周期,可以自定义多久时间来分析周期变化

  • def add_regressor(self, name, prior_scale=None, standardize=’auto’,
                    mode=None):  
    
    添加自定义外部影响的函数

2.3.模型训练

prophet训练模型类似于sklearn,forester.py中fit函数进行模型的训练:

m.fit(ds) #训练模型

2.4.模型预测

prophet的预测也相似于sklearn,依靠forester.py中的predic函数

1
2
3
4
5
6
7
8
9
    forecast = m.predict(future)
```



### 2.5.可视化模块
prophet的可视化也很方便包,在Prophet类中有plot、plot_components函数,用于绘制预测结果图和各个组成部分的图:
```commandline
m.plot(forecast)

通过以上代码可以显示出预测结果的可视化,如下所示:

plot

通过以下代码可得预测各部分(趋势和周期性)的可视化:

1
m.plot_compoents(forecast)

compoens

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2022-2024 Yutouegg
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信