加载包
import numpy as np
import pandas as pd
import math
#Sequential多个网络层的线性堆叠;Dense隐含层
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import SimpleRNN
from keras.layers import GRU
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error
数据定义
def create_dataset(dataset, look_back):
dataX, dataY = [], []
for i in range(len(dataset)-look_back):
a = dataset[i:(i+look_back), 0]
#append向列表的尾部增加元素
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return np.array(dataX), np.array(dataY)
数据分割
# fix random seed for reproducibility
#seed用于指定随机数生成时所用算法开始的整数值
np.random.seed(7)
# load the dataset/下载数据集
#usecols获取数据的列,如果取前4列,则usecols=(0,1,2,3)
#print(XXXX)
dataframe = pd.read_csv(r'333.csv' \
, usecols=[0], engine='python')
dataset = dataframe.values
dataset = dataset.astype('float32')
# normalize the dataset/标准化数据集
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets/分割训练集与测试集
#train_size = int(len(dataset) * 0.84)
vsize = 200;
train_size = 800;
#print(train_size )
# test_size = len(dataset) - train_size
look_back =10
train, test = dataset[0:train_size-1,:], dataset[train_size-look_back-1:train_size+vsize-1,:]
# dataset detail/具体分割后数据集
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
#predX, predY = create_dataset(pred, look_back)
# reshape input to be [samples, feature_num, features]
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
建模预测
#predX = np.reshape(predX,(predX.shape[0], 1, predX.shape[1]))
#print(trainX ))
# start_cr_a_fit_net = time.time()
# create and fit the LSTM network/创建并拟合LSTM网络
model = Sequential()
model.add(GRU(15, input_shape=(None,look_back)))
#model.add(SimpleRNN(18, input_shape=(None,look_back)))
#model.add(GRU(XXXX[1], input_shape=(None,look_back)))
model.add(Dense(1))
model.summary()
#model.compile(loss='mean_squared_error', optimizer='adam')
model.compile(loss='mean_squared_error', optimizer='sgd')
model.fit(trainX, trainY, epochs=331, batch_size=1, verbose=0)
# end_cr_a_fit_net = time.time()-start_cr_a_fit_net
# print('Running time of creating and fitting the LSTM network: %.2f Seconds' % (end_cr_a_fit_net))
# make predictions/进行预测
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
#NpredY = model.predict(predX)
##### invert predictions start ##### / 将预测值转换为正常数值
# create empty table with 12 fields
trainPredict=scaler.inverse_transform(trainPredict)
trainY=scaler.inverse_transform([trainY])
testPredict=scaler.inverse_transform(testPredict)
testY=scaler.inverse_transform([testY])
#NpredY = scaler.inverse_transform(NpredY)
#predY=scaler.inverse_transform([predY])
# print(predY)
# print(NpredY)
# print('测试集预测值' )
# print(testPredict)
##### invert predictions end #####
# calculate root mean squared error/计算RMSE和误差率
# trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
# print('Train Score: %.8f RMSE' % (trainScore))
# testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
# print('Test Score: %.8f RMSE' % (testScore))
# df = pd.DataFrame(NpredY[:,0])
# aa=df.to_excel('12.xlsx',encoding='utf-8')
#df = pd.DataFrame(NpredY[:,0])
#aa=df.to_excel('12.xlsx',encoding='utf-8')
preRMSE = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test RMSE: %.8f RMSE' % (preRMSE))
preMSE = mean_squared_error(testY[0], testPredict[:,0])
print('Test MSE: %.8f MSE' % (preMSE))
trainMAE = mean_absolute_error(testY[0], testPredict[:,0])
print('Test MAE: %.8f MAE' % (trainMAE))
# testMAE = mean_absolute_error(testY[0], testPredict[:,0])
# print('Test MAE: %.8f MAE' % (testMAE))
#trainMAPE = np.mean(np.abs(trainY[0]-trainPredict[:,0])/trainY[0])
# print('Train MAPE: %.8f %% MAPE' % (trainMAPE*100))
#testMAPE = np.mean(np.abs(testY[0]-testPredict[:,0])/testY[0])
#print('Test MAPE: %.8f %% MAPE' % (testMAPE*100))
predMAPE = np.mean(np.abs(testY[0]-testPredict[:,0])/testY[0])
print('predY MAPE: %.8f %% MAPE' % (predMAPE*100))