1

我需要以“滚动”的方式与先知进行预测。只是为了给你一个想法,考虑到形状的 df (2400,2),我想执行类似的操作:

def forecast(dataframe):

    m = Prophet()
    m.fit(dataframe)
    future = m.make_future_dataframe(
             periods=24, freq="H")
    forecast = m.predict(future)

    return forecast['yhat'][-1]

df['yhat'] = df[['ds','y']].rolling(240).apply(forecast)

有没有聪明的方法来做到这一点?

4

1 回答 1

0

以这种方式完成,也许还有更好的方法:

def forecast(data, prds):

    data.name='y'
    data.reset_index(drop=True, inplace=True)

    df = pd.DataFrame()
    df['ds'] = pd.date_range(start='1/1/2020', periods=len(data), freq="H")

    df=df.merge(data, left_index=True, right_index=True)

    m = Prophet()
    m.fit(df)
    future = m.make_future_dataframe(periods=prds, freq="H")
    forec = m.predict(future)

    prediction = forec['yhat'].iloc[-1]

    return prediction

dataframe['yhat'] = dataframe['y'].rolling(240).apply(forecast, args=(24,))

于 2021-04-13T16:08:48.997 回答