前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >prophet Saturating Forecasts饱和预测例子

prophet Saturating Forecasts饱和预测例子

作者头像
lovelife110
发布2021-01-14 14:37:54
7680
发布2021-01-14 14:37:54
举报
文章被收录于专栏:爱生活爱编程爱生活爱编程

一、例子代码

demo代码:https://github.com/lilihongjava/prophet_demo/tree/master/saturating_forecasts

代码语言:javascript
复制
# encoding: utf-8
"""
@author: lee
@time: 2019/5/13 15:26
@file: main.py
@desc: 
"""
import pandas as pd
from fbprophet import Prophet
from pandas.plotting import register_matplotlib_converters


def main():
    df = pd.read_csv('./data/example_wp_log_R.csv')

    df['cap'] = 8.5

    m = Prophet(growth='logistic')
    m.fit(df)

    register_matplotlib_converters()
    future = m.make_future_dataframe(periods=1826)
    future['cap'] = 8.5
    fcst = m.predict(future)
    fig = m.plot(fcst)
    fig.show()

    df['y'] = 10 - df['y']
    df['cap'] = 6
    df['floor'] = 1.5
    future['cap'] = 6
    future['floor'] = 1.5
    m = Prophet(growth='logistic')
    m.fit(df)
    fcst = m.predict(future)
    fig = m.plot(fcst)
    fig.show()


if __name__ == "__main__":
    main()

二、逻辑增长模型(logistic growth)

默认情况下,Prophet 使用线性模型来预测,在预测增长时,通常会出现最大可达点,例如总市场规模,总人口数等。这被称为承载能力,预测应该在这一点上饱和。

Prophet允许使用具有指定承载能力的逻辑增长模型(logistic growth)进行预测。以下使用维基百科上R语言访问量(取对数)的数据。

df = pd.read_csv('../examples/example_wp_log_R.csv')

使用该模型,必须指定承载能力即:cap,必须在一个新的列上指明

df['cap'] = 8.5

需要注意的是,cap必须为dataframe的每一行指定,cap 列的值不一定是常数,因为如果市场规模在增长,那么cap 可能也会增长。

然后我们像以前一样拟合模型,传递一个额外的参数growth='logistic'来指定 logistic growth:

m = Prophet(growth='logistic') m.fit(df)

我们像之前的例子一样为预测构造一个dataframe ,但我们还必须指定cap。我们将cap设置上面相同的值,并预测未来3年:

future = m.make_future_dataframe(periods=1826) future['cap'] = 8.5 fcst = m.predict(future) fig = m.plot(fcst)

logistic函数的隐式最小值为0,并且在0处饱和,就像在cap处饱和一样。也可以指定不同的饱和最小值。

三、饱和最小值

逻辑增长模型模型还可以处理饱和最小值,使用floor与cap相同的方式来指定:

df['y'] = 10 - df['y'] df['cap'] = 6 df['floor'] = 1.5 future['cap'] = 6 future['floor'] = 1.5 m = Prophet(growth='logistic') m.fit(df) fcst = m.predict(future) fig = m.plot(fcst)

要使用具有饱和最小值的logistic growth trend,还必须指定最大cap。

参考资料:

https://facebook.github.io/prophet/docs/saturating_forecasts.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/05/13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、例子代码
  • 二、逻辑增长模型(logistic growth)
  • 三、饱和最小值
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档