如何在fbprophet中手动绘制每周组件?

2024-06-17 00:57:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用fbprophet根据我的时间序列进行预测。除了最后一刻,一切都很顺利。在

m = Prophet(daily_seasonality = True, yearly_seasonality = False, weekly_seasonality = True,
                seasonality_mode = 'multiplicative', 
                interval_width = interval_width,
                changepoint_range = changepoint_range)
m = m.fit(dataframe)
forecast = m.predict(dataframe)
m.plot_components(forecast)

Results of plot_components function

我不需要使用标准的函数plot_组件,而是需要保存数据并独立地进行绘图(例如在AWS的QuickSight中)。我知道如何做趋势(它需要所有可能的数据和绘制图表),但我不明白它是如何与每周和每天的图表。在

有人知道如何为每周和每天的绘图保存数据吗?在


Tags: 数据true绘图dataframeplot图表时间components
1条回答
网友
1楼 · 发布于 2024-06-17 00:57:54

在先知内部挖掘: https://github.com/facebook/prophet/blob/master/python/fbprophet/plot.py

实现这一点的一种方法是编写自己的函数,重用plot_weekly()中的一些内部代码

def my_custom_plot_weekly(m, ax=None, uncertainty=True, weekly_start=0, figsize=(10, 6), name='weekly'):
    """Plot the weekly component of the forecast.
    Parameters
         
    m: Prophet model.
    ax: Optional matplotlib Axes to plot on. One will be created if this
        is not provided.
    uncertainty: Optional boolean to plot uncertainty intervals, which will
        only be done if m.uncertainty_samples > 0.
    weekly_start: Optional int specifying the start day of the weekly
        seasonality plot. 0 (default) starts the week on Sunday. 1 shifts
        by 1 day to Monday, and so on.
    figsize: Optional tuple width, height in inches.
    name: Name of seasonality component if changed from default 'weekly'.
    Returns
       -
    a list of matplotlib artists
    """
    artists = []
    if not ax:
        fig = plt.figure(facecolor='w', figsize=figsize)
        ax = fig.add_subplot(111)
    # Compute weekly seasonality for a Sun-Sat sequence of dates.
    days = (pd.date_range(start='2017-01-01', periods=7) +
            pd.Timedelta(days=weekly_start))
    # Import this function: seasonality_plot_df 
    df_w = seasonality_plot_df(m, days)
    seas = m.predict_seasonal_components(df_w)
    days = days.weekday_name
    # Return the data here, do not plot.
    return days, seas

在你的情况下,你会得到这样的结果:

^{pr2}$

相关问题 更多 >