我如何正确预测湿度值?

时间:2018-11-23 00:02:15

标签: python time-series statsmodels arima

我有以下输入内容:

startDate = "2013-01-01"
endDate = "2013-01-01"
knownTimestamps = ['2013-01-01 00:00','2013-01-01 01:00','2013-01-01     02:00','2013-01-01 03:00','2013-01-01 04:00',
           '2013-01-01 05:00','2013-01-01 06:00','2013-01-01 08:00','2013-01-01 10:00','2013-01-01 11:00',
           '2013-01-01 12:00','2013-01-01 13:00','2013-01-01 16:00','2013-01-01 17:00','2013-01-01 18:00',
           '2013-01-01 19:00','2013-01-01 20:00','2013-01-01 21:00','2013-01-01 23:00']
humidity = ['0.62','0.64','0.62','0.63','0.63','0.64','0.63','0.64','0.48','0.46','0.45','0.44','0.46','0.47','0.48','0.49','0.51','0.52','0.52']
timestamps = ['2013-01-01 07:00','2013-01-01 09:00','2013-01-01 14:00','2013-01-01 15:00','2013-01-01 22:00']

我正在使用以下函数通过python中的AR模型预测湿度值。

from statsmodels.tsa.arima_model import ARIMA
def predictMissingHumidity(startDate, endDate, knownTimestamps, humidity, timestamps):
data_prediction = pd.DataFrame({'knownTimestamps': knownTimestamps,'humidity': humidity})
print(data_prediction.head(10))
history = [float(x) for x in data_prediction.humidity]
predictions = []
test = timestamps
for t in range(len(test)):
    model = ARIMA(history, order=(2,2,0))
    model_fit = model.fit(disp=0)
    output = model_fit.forecast()
    yhat = output[0]
    predictions.append(float(yhat))
    obs = test[t]
    history.append(float(obs))
print(predictions)
return predictions

模型为时间戳列表中的值预测相同的湿度值。

res = predictMissingHumidity(startDate, endDate, knownTimestamps, humidity, timestamps) 
print(res)


output = [0.5287247355700563, 0.5287247355700563, 0.5287247355700563,
 0.5287247355700563, 0.5287247355700563] 

有人可以告诉我我哪里错了吗?

1 个答案:

答案 0 :(得分:0)

您没有更新历史记录。大概这是您大多数代码来自的站点

https://machinelearningmastery.com/arima-for-time-series-forecasting-with-python/

您可以在第23行上看到历史记录如何更新并用于测试集下一步的预测:

history.append(obs)