参考官方文档:趋势变化点
prophet 是由 Facebook 开发的针对时间序列数据进行快速预测和分析的开源 Python 库。它适用于具有季节性、节假日效应等特征的时间序列数据。
pip install prophet
prophet-1.1.5-py3-none-win_amd64.whl (13.3 MB)
pip install plotly
plotly-5.22.0-py3-none-any.whl
1. 趋势检验案例
根据官方示例改编的: test_prophet.py
# -*- coding: utf-8 -*-
""" 1. 趋势检验案例 依赖 plotly """
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from prophet import Prophet
from datetime import datetime
# 构造数据集
start = datetime(2021, 10, 1)
end = datetime(2022, 10, 1)
rng = pd.date_range(start, end, freq='W')
data = pd.Series(np.random.randn(len(rng)), index=rng)
data = data.reset_index()
data.columns = ['ds','y']# model
m = Prophet(changepoint_prior_scale=5,yearly_seasonality=False)
# 兼顾异常值,0.8,兼顾80%的异常值;越大兼顾的越多,波动性越大
# 越小越平滑
# Proportion of history in which trend changepoints will be estimated
m.fit(data)
forecast = m.predict(data)# Python
from prophet.plot import add_changepoints_to_plot
fig = m.plot(forecast)
a = add_changepoints_to_plot(fig.gca(), m, forecast)# 找到突变时间线
threshold = 0.01
signif_changepoints = m.changepoints[np.abs(np.nanmean(m.params['delta'], axis=0)) >= threshold] if len(m.changepoints) > 0 else []print(signif_changepoints)
运行 python test_prophet.py