【问题标题】:Predict beyond existing data using pyramid-arima使用pyramid-arima预测超越现有数据
【发布时间】:2019-11-28 03:05:47
【问题描述】:

我正在为美国每个州的房价创建 50 个不同的时间序列模型。我正在使用金字塔 ARIMA 来完成此任务。

数据来自包含 Date、State 和 Median_Listing_Price 列的 .csv。

我已经创建了模型并希望预测超出现有数据的值,但我不知道如何做到这一点。

我有一个看起来像这样的图表:

我想要一个看起来像这样的图表:

我还想将预测值输出到新的 .csv。

当前代码:

# Indexing and creating series
df = pd.read_csv(f'state_csvs/{state}.csv', parse_dates=['Date'], date_parser=dateparse, index_col=0, header=0)
data = df[['Median_Listing_Price']]

# Divide into train and validation set
train = data.loc['2013-11':'2017-01']
valid = data.loc['2017-02':]

# Building the model
model = auto_arima(train, start_p=1, start_q=1, max_p=3, max_q=3, m=12, start_P=0, seasonal=True, d=1, D=1,
                   trace=True, error_action='ignore', suppress_warnings=True)
model.fit(train)

forecast = model.predict(n_periods=len(valid))
forecast = pd.DataFrame(forecast, index=valid.index, columns=['Prediction'])

# Plot the predictions for validation set
plt.plot(train, label='Train')
plt.plot(valid, label='Valid')
plt.plot(forecast, label='Prediction')
plt.title(f'{state}')
plt.show()

【问题讨论】:

    标签: python arima pyramid-arima


    【解决方案1】:

    您可以使用以下代码,

    import matplotlib as plt
    import pandas as pd
    
    #you can continue your code with following code
    #But you need to first get final parameters from auto_arima
    
    model.summary()        #for getting final parameters
    
    #Look for output like >> Model: ARIMA(x, y, z)
    #Now use the following code after yours
    model_future = ARIMA(df,order=(x, y, z))
    results_future = model_future.fit()
    predictions_future = results_future.predict(len(df),len(df)+12,typ = 'levels')
    df.plot(legend=True , figsize = (12,8))
    predictions_future.plot(legend= True)
    
    #To add future predictions to csv you can use,
    predictions_future.to_csv(path_to_folder)
    

    在我的情况下输出: 我已经做了 12 个月的未来,你可以有自己的参数设置。

    【讨论】:

    • 是的!这行得通!非常感谢,我已经为此感到沮丧了好几个小时。
    • 很高兴能帮上忙。
    猜你喜欢
    • 2022-10-22
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多