Python時間序列LSTM預測系列教程(5)-單變量

單變量LSTM預測模型(5)


教程原文連接


前置教程:

Python時間序列LSTM預測系列教程(1)-單變量

Python時間序列LSTM預測系列教程(2)-單變量

Python時間序列LSTM預測系列教程(3)-單變量

Python時間序列LSTM預測系列教程(4)-單變量


更健壯的LSTM案例


基於Python時間序列LSTM預測系列教程(4)描述的案例,進行改進

Keras每次會默認隨機初始化LSTM

爲了評估模型的性能,可以多次試驗,求RMSE的均值


代碼解析


#重複實驗   
repeats = 30
error_scores = list()
for r in range(repeats):
    #fit 模型
    lstm_model = fit_lstm(train_scaled, 1, 3000, 4)#訓練數據,batch_size,epoche次數, 神經元個數
    #預測   
    train_reshaped = train_scaled[:,0].reshape(len(train_scaled), 1, 1)
    lstm_model.predict(train_reshaped, batch_size=1)
    #測試數據的前向驗證
    predictions = list()
    for i in range(len(test_scaled)):
        #1步長預測
        X, y = test_scaled[i, 0:-1], test_scaled[i, -1]
        yhat = forcast_lstm(lstm_model, 1, X)
        #逆縮放
        yhat = invert_scale(scaler, X, yhat)
        #逆差分
        yhat = inverse_difference(raw_values, yhat, len(test_scaled)+1-i)
        predictions.append(yhat)
        expected = raw_values[len(train)+i+1]
        print('Moth=%d, Predicted=%f, Expected=%f'%(i+1, yhat, expected))
    #性能報告
    rmse = sqrt(mean_squared_error(raw_values[-12:], predictions))
    print('%d) Test RMSE:%.3f' %(r+1,rmse))
    error_scores.append(rmse)
            
#統計信息   
results = DataFrame()
results['rmse'] = error_scores
print(results.describe())
results.boxplot()
pyplot.show()

實驗結果




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章