【问题标题】:Why adding new data in time series affects yhat (predictions) in the past in Prophet?为什么在时间序列中添加新数据会影响 Prophet 过去的(预测)?
【发布时间】:2022-12-17 21:55:23
【问题描述】:

我正在使用 Prophet 来预测时间序列。但是,如果我上个月放弃,yhat 在过去变化。我认为预期的结果是修改未来的预测,而不是过去。这种行为正确吗?

我怎样才能让一个月只取决于过去的数据?

import pandas as pd
from prophet import Prophet
pd.set_option('display.float_format', lambda x: '%.2f' % x)

# Read the data
json_data = '{"ds":{"0":"2017-10-01","1":"2017-11-01","2":"2017-12-01","3":"2018-01-01","4":"2018-02-01","5":"2018-03-01","6":"2018-04-01","7":"2018-05-01","8":"2018-06-01","9":"2018-07-01","10":"2018-08-01","11":"2018-09-01","12":"2018-10-01","13":"2018-11-01","14":"2018-12-01","15":"2019-01-01","16":"2019-02-01","17":"2019-03-01","18":"2019-04-01","19":"2019-05-01","20":"2019-06-01","21":"2019-07-01","22":"2019-08-01","23":"2019-09-01","24":"2019-10-01","25":"2019-11-01","26":"2019-12-01","27":"2020-01-01","28":"2020-02-01","29":"2020-03-01","30":"2020-04-01","31":"2020-05-01","32":"2020-06-01","33":"2020-07-01","34":"2020-08-01","35":"2020-09-01","36":"2020-10-01","37":"2020-11-01","38":"2020-12-01","39":"2021-01-01","40":"2021-02-01","41":"2021-03-01","42":"2021-04-01","43":"2021-05-01","44":"2021-06-01","45":"2021-07-01","46":"2021-08-01","47":"2021-09-01","48":"2021-10-01","49":"2021-11-01","50":"2021-12-01","51":"2022-01-01","52":"2022-02-01","53":"2022-03-01","54":"2022-04-01","55":"2022-05-01","56":"2022-06-01","57":"2022-07-01","58":"2022-08-01","59":"2022-09-01","60":"2022-10-01","61":"2022-11-01"},"y":{"0":3065,"1":3127,"2":8506,"3":2527,"4":2376,"5":2753,"6":2964,"7":3750,"8":4445,"9":3502,"10":3968,"11":3195,"12":3232,"13":3377,"14":7823,"15":2452,"16":2563,"17":2747,"18":2877,"19":3617,"20":3620,"21":4044,"22":3491,"23":2853,"24":3447,"25":3346,"26":7835,"27":2543,"28":2412,"29":1860,"30":759,"31":3630,"32":2216,"33":1247,"34":4455,"35":3178,"36":3502,"37":3475,"38":7311,"39":2296,"40":2136,"41":1717,"42":2200,"43":3764,"44":3697,"45":4007,"46":3566,"47":3043,"48":3457,"49":3256,"50":8564,"51":2218,"52":2815,"53":3389,"54":3816,"55":4853,"56":4406,"57":3859,"58":4152,"59":3421,"60":3965,"61":3590}}'
data = pd.read_json(json_data)
data['ds'] = pd.to_datetime(data['ds'])

# Predict helper function
def predict(df, periods=12):
    m = Prophet().fit(df)
    future = m.make_future_dataframe(periods=periods, freq='MS')
    forecast = m.predict(future)
    return forecast[['ds', 'yhat']].merge(df, on='ds', how='left')

# Predict the next 12 months
prediction = predict(data, 12)

# Drop last month from data and predict the next 12 months
prediction_without_last_month = predict(data[:-1], 12)

# Compare the predictions
comparison = prediction.merge(prediction_without_last_month, on='ds', suffixes=['', '_without_last_month'])

# Output the comparison
comparison[comparison['ds'].dt.month == 10]

PS:请注意,过去一年中的所有 10 月份都有不同的 yhat,仅排除 2022 年 11 月,即未来与这些月份相比。

【问题讨论】:

    标签: python pandas data-science arima prophet


    【解决方案1】:

    时间序列数据具有季节性、趋势和周期等核心组成部分。例如,冰淇淋的销售通常具有年度季节性——您可以根据今年的销售情况合理预测明年夏天的销售情况。同样,温度或空气质量测量值具有每日或每年的季节性。

    【讨论】:

    • 抱歉,我的问题没有得到回答。让我更清楚一点:我根据包含 2017-10 月至 2022-11 月的数据集做出了预测。做出相同的预测,但不包括 2022-11 月份,而且过去几个月的预测都发生了变化。为什么?
    猜你喜欢
    • 2018-08-13
    • 2018-10-16
    • 2011-03-24
    • 2017-06-27
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多