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()

实验结果




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