lstm 预测

 数据加载

#coding=utf-8
'''
Created on 2018年12月21日

@author: Administrator
'''
# LSTM for problem with window regression framing

import numpy as np
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error

# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back):
        a = dataset[i:(i+look_back), 0]
        dataX.append(a)
        dataY.append(dataset[i + look_back, 0])
    return numpy.array(dataX), numpy.array(dataY)

# fix random seed for reproducibility
numpy.random.seed(7)
look_back = 14

# load the dataset: 原始数据格式: 小时   小时内发生值
dataframe = read_csv('data/src.csv', usecols=[1], engine='python', skiprows=0, skipfooter=0)
data = dataframe.values
data = data.astype('float32')
print('dataset shape: ',data.shape)


# normalize the dataset
'''
from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(data)
'''
"""
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
dataset = scaler.fit_transform(data)
"""

# '''
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(data)
# '''
print('dataset shape: ',dataset.shape)

# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size-look_back:len(dataset),:]
print('test shape: ',test.shape)

# reshape into X=t and Y=t+1

trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
print('testX shape: ',testX.shape)
print('testY shape: ',testY.shape)

训练

trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
print('testX shape: ',testX.shape)
print('testY shape: ',testY.shape)

# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
print('testX shape: ',testX.shape)

# create and fit the LSTM network

from keras.callbacks import ReduceLROnPlateau
model = Sequential()
model.add(LSTM(16, input_shape=(look_back, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')


reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')


model.fit(trainX, trainY, epochs=1200, batch_size=16, verbose=2,callbacks=[reduce_lr])

预测,计算RMSE

trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
print('testX shape: ',testX.shape)
print('testY shape: ',testY.shape)

# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
print('testPredict shape:',testPredict.shape)

# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
print('testPredict shape: ',testPredict.shape)
print('testY shape: ',testY.shape)

# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))

绘图:对每一个点单独预测,提供的特征是已经存在的真实值

# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict

# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back):len(dataset), :] = testPredict

# plot baseline and predictions
plt.figure(figsize=(12,4),dpi=180)
plt.plot(scaler.inverse_transform(dataset), color="red")
plt.plot(trainPredictPlot, color="blue")
plt.plot(testPredictPlot, color="green")
plt.show()

用前一个预测的结果继续预测下一个

import numpy as np
recent_time_data = dataset[train_size-look_back:train_size, 0]

testX = recent_time_data
testX = testX.reshape(-1,look_back)
hours_long = 198  #预测的单位时间长度

'''将预测的结果加入已知数据中,作为下次预测的特征'''
def predict_24h(dataset_X,hours_long=124):

    predict_X = dataset_X
    predict = []
    for i in range(hours_long):
        predict_X = np.reshape(predict_X, (1, look_back, 1))
        predict_point = model.predict(predict_X)
        predict_X = np.delete(predict_X[0],0,axis = 0)
        predict_X = np.vstack((predict_X,predict_point))
        predict.append(predict_point[0])
        
    return predict

predict = predict_24h(testX,hours_long)

predict_dataset = scaler.inverse_transform(predict)

predictPlot24 = numpy.empty_like(dataset)
predictPlot24[:, :] = numpy.nan
predictPlot24[len(trainPredict)+look_back:len(trainPredict)+look_back+hours_long, :] = predict_dataset


display_range = 402 #显示此时间点数值后面的图像
plt.figure(figsize=(12,4),dpi=180)
plt.plot(scaler.inverse_transform(dataset)[display_range:], color="red")
# plt.plot(trainPredictPlot[display_range:], color="blue")
plt.plot(predictPlot24[display_range:], color="green")
plt.legend(['ture','predict'],loc='upper right')
plt.show()

模型保存

filepath = '/model.h5'

model.save_weights(filepath)

模型加载

model.load_weights(filepath, by_name=False)

用已经训练好的模型预测

输入最近14(由look_back定)个点的数据

his_data =[ 388.,  5895., 26135., 47642., 51058., 49131., 47858., 45013.,
       42801., 51359., 49741., 51892., 59602., 65449.]

his_data = np.array(his_data).reshape(look_back,-1)

his_dataset = scaler.transform(his_data)

recent_time_data = his_dataset


testX = recent_time_data
testX = testX.reshape(-1,look_back)
hours_long = 198 #预测的单位时间长度
def predict_24h(dataset_X,hours_long=124):

    predict_X = dataset_X
    predict = []
    for i in range(hours_long):
        predict_X = np.reshape(predict_X, (1, look_back, 1))
        predict_point = model.predict(predict_X)
        predict_X = np.delete(predict_X[0],0,axis = 0)
        predict_X = np.vstack((predict_X,predict_point))
        predict.append(predict_point[0])
        
    return predict

predict = predict_24h(testX,hours_long)

predict_dataset = scaler.inverse_transform(predict)

predictPlot24 = numpy.empty_like(dataset)
predictPlot24[:, :] = numpy.nan
predictPlot24[len(trainPredict)+look_back:len(trainPredict)+look_back+hours_long, :] = predict_dataset


display_range = 402 #显示此时间点数值后面的图像
plt.figure(figsize=(12,4),dpi=180)
plt.plot(scaler.inverse_transform(dataset)[display_range:], color="red")
plt.plot(trainPredictPlot[display_range:], color="blue")
plt.plot(predictPlot24[display_range:], color="green")
plt.show()

猜你喜欢

转载自blog.csdn.net/lucky_kai/article/details/85251306