新手只需4步入门 Prophet,轻松做 PV 预测

体验 Facebook 开源的时序预测模型 Prophet,做用户访问 PV 的预测,非常容易上手。

1. 安装 prophet

$ pip3 install pystan==2.19.1.1
$
$ pip3 install prophet
1
2
3

2. 准备样本

Peyton Manning 的维基百科页面open in new window 的每日页面浏览量作为样本,因为它说明了 Prophet 的一些特征,比如季节性、不断变化的增长率,以及对特殊日子进行建模的能力 (比如曼宁的季后赛和超级碗出场)。

样本 下载地址open in new window https://github.com/facebook/prophet/blob/master/examples/example_wp_log_peyton_manning.csv

Prophet 预测考虑了季节、节假日、星期等因素,该样本比较吻合,比如美国的超级碗会影响该页面的访问突增。类似咱们国家的双十一、618 等都是特殊日子。

使用 Jupyter Lab 体验训练的过程,将样本装载到 Dataframe 中。

import pandas as pd
from prophet import Prophet

## 装载样本到 dataframe 中
df = pd.read_csv('example_wp_log_peyton_manning.csv')
df.head()

## 绘图看下效果
df.plot(x='ds', y='y', title='pageviews', ylabel='y', figsize=(20,10) )
1
2
3
4
5
6
7
8
9

-w1514

3. 训练模型

## 实例化一个 Prophet() 对象来训练模型
m = Prophet()m.fit(df)
1
2

在实例化 Prophet()对象的时候,有很多参数可以选择 yearly_seasonality=True, daily_seasonality=True,还可以指定 法定节假日open in new window 等。 此外,除了常规的线性模型,也支持 逻辑模型open in new window ,比如市场规模存在饱和,通过参数 growth='logistic' 控制 -w320

4. 预测数据

# 创建一个 dataframe,包含预测的日期行,用于存储预测数据
future = m.make_future_dataframe(periods=365)
future.tail()

# 预测
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

# 绘图
fig1 = m.plot(forecast)

# 绘图 ,周期性趋势变化
fig2 = m.plot_components(forecast)
1
2
3
4
5
6
7
8
9
10
11
12
13

-w1518-w1519

5. FAQ

5.1 CentOS 7 无法安装最新版 1.0.1

以下方法在 CentOS 7.2、CentOS 7.6 下验证有效。

主要是因为 prophet==1.0.1 依赖 pystan==2.19.1.1 ,在 CentOS 7 中可能是因为 gcc 版本不匹配,导致无法安装成功。

解决办法,安装 pystan==2.18.0.0prophet==1.0prophet==1.0 只要求 pystan>=2.14

pip3 install pystan==2.18.0.0  -i https://mirrors.tencent.com/pypi/simple
pip3 install prophet==1.0
1
2

以下是报错内容

Installing collected packages: prophet
  Running setup.py install for prophet ... error
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-ummn4y60/prophet/setup.py';f=getattr(tokenize,'open', open)(__file__);code=f.read().replace('\r\n','\n');f.close();exec(compile(code, __file__,'exec'))" install --record /tmp/pip-t3dbp9js-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib
    creating build/lib/prophet
    creating build/lib/prophet/stan_model
    Importing plotly failed. Interactive plots will not work.
    DIAGNOSTIC(S) FROM PARSER:
    Warning: left-hand side variable (name=cp_idx) occurs on right-hand side of assignment, causing inefficient deep copy to avoid aliasing.
    Warning: left-hand side variable (name=m_pr) occurs on right-hand side of assignment, causing inefficient deep copy to avoid aliasing.

    INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_dfdaf2b8ece8a02eb11f050ec701c0ec NOW.
    /usr/local/lib64/python3.6/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /tmp/tmp8hr7e3l3/stanfit4anon_model_dfdaf2b8ece8a02eb11f050ec701c0ec_5458513074395718142.pyx
      tree = Parsing.p_module(s, pxd, full_module_name)
    error: command 'gcc' failed with exit status 4

    ----------------------------------------
Command "/usr/bin/python3 -u -c"import setuptools, tokenize;__file__='/tmp/pip-build-ummn4y60/prophet/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))"install --record /tmp/pip-t3dbp9js-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-ummn4y60/prophet/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

5.2 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any()or a.all()

m.fit() 报错,可将 pandas 版本调整为 pandas=1.1.5

5.3 prophet: NameError: name 'go' is not defined

from prophet.plot import plot_plotly, plot_components_plotly
plot_plotly(m, forecast)
1
2

执行代码有如下报错,原来是未安装 ipywidgets,使用 pip3 安装即可。

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-43-b429b8acb1e5> in <module>
      2 from prophet.plot import plot_plotly, plot_components_plotly
      3 
----> 4 plot_plotly(m, forecast)

/usr/local/lib/python3.9/site-packages/prophet/plot.py in plot_plotly(m, fcst, uncertainty, plot_cap, trend, changepoints, changepoints_threshold, xlabel, ylabel, figsize)
    588     data = []
    589     # Add actual
--> 590     data.append(go.Scatter(
    591         name='Actual',
    592         x=m.history['ds'],

NameError: name 'go' is not defined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

在 jupyter notebook 中重新执行代码,可以使用交互式插件控制预测效果。

jupyter lab 一直没成功,但 jupyter notebook 没问题。

5.4 在 jupyter-scipy 镜像中安装 prophet

在 kubeflow 内置的 notebookopen in new window 中直接安装 prophet,会有如下提示

Building wheels for collected packages: prophet
  Building wheel for prophet (setup.py) ... |^@^@^@^@^@^error
  ERROR: Command errored out with exit status 1:
   command: /opt/conda/bin/python3.8 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-iafmvjcd/prophet_5c45e8d96bf04ec493a240825e496607/setup.py'"'"'; __file__='"'"'/tmp/pip-install-iafmvjcd/prophet_5c45e8d96bf04ec493a240825e496607/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-rvhk55ih
       cwd: /tmp/pip-install-iafmvjcd/prophet_5c45e8d96bf04ec493a240825e496607/
  Complete output (10 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib
  creating build/lib/prophet
  creating build/lib/prophet/stan_model
  Importing plotly failed. Interactive plots will not work.
  INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_dfdaf2b8ece8a02eb11f050ec701c0ec NOW.
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for prophet
  Running setup.py clean for prophet
Failed to build prophet
Installing collected packages: prophet
    Running setup.py install for prophet ... error
    ERROR: Command errored out with exit status 1:
     command: /opt/conda/bin/python3.8 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-iafmvjcd/prophet_5c45e8d96bf04ec493a240825e496607/setup.py'"'"'; __file__='"'"'/tmp/pip-install-iafmvjcd/prophet_5c45e8d96bf04ec493a240825e496607/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-6a41s8ow/install-record.txt --single-version-externally-managed --compile --install-headers /opt/conda/include/python3.8/prophet
         cwd: /tmp/pip-install-iafmvjcd/prophet_5c45e8d96bf04ec493a240825e496607/
    Complete output (10 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib
    creating build/lib/prophet
    creating build/lib/prophet/stan_model
    Importing plotly failed. Interactive plots will not work.
    INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_dfdaf2b8ece8a02eb11f050ec701c0ec NOW.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /opt/conda/bin/python3.8 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-iafmvjcd/prophet_5c45e8d96bf04ec493a240825e496607/setup.py'"'"'; __file__='"'"'/tmp/pip-install-iafmvjcd/prophet_5c45e8d96bf04ec493a240825e496607/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-6a41s8ow/install-record.txt --single-version-externally-managed --compile --install-headers /opt/conda/include/python3.8/prophet Check the logs for full command output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

查看 jupyter-scipy:v1.4 类似镜像 scipy-notebookopen in new window基础镜像dockerfileopen in new window 中可以发现包含 conda。

所以既然 pip 安装不上,尝试下 cnoda

在 notebook 中执行以下命令,安装 prophet

!conda install -c conda-forge prophet -y
1

prophet-1.0.1 安装成功。

import prophet 验证是否安装成功。

Reference