Python时间序列LSTM预测系列教程(5)-单变量

来源:互联网 发布:触摸屏查询软件 编辑:程序博客网 时间:2024/06/05 20:24

单变量LSTM预测模型(5)


教程原文连接


前置教程:

Python时间序列LSTM预测系列教程(1)-单变量

Python时间序列LSTM预测系列教程(2)-单变量

Python时间序列LSTM预测系列教程(3)-单变量

Python时间序列LSTM预测系列教程(4)-单变量


更健壮的LSTM案例


基于Python时间序列LSTM预测系列教程(4)描述的案例,进行改进

Keras每次会默认随机初始化LSTM

为了评估模型的性能,可以多次试验,求RMSE的均值


代码解析


#重复实验   repeats = 30error_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_scoresprint(results.describe())results.boxplot()pyplot.show()

实验结果




原创粉丝点击