from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import GRU
import numpy as np
# convert series to supervised learning
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = DataFrame(data)
cols, names = list(), list()
# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
else:
names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
# put it all together
agg = concat(cols, axis=1)
agg.columns = names
# drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
return agg
# load dataset
dataset = read_csv('京哈高速(1).csv', header=0, index_col=0)
values = dataset.values
# integer encode direction
#encoder = LabelEncoder()
#values[:,4] = encoder.fit_transform(values[:,4])
# ensure all data is float
values = values.astype('float32')
# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
# frame as supervised learning
reframed = series_to_supervised(scaled, 1, 1)
# drop columns we don't want to predict
reframed.drop(reframed.columns[[4,5]], axis=1, inplace=True) #改为[3,4]可预测速度
print(reframed.head())
# split into train and test sets
values = reframed.values
n_train_hours = 400 #设置训练样本量
train = values[:n_train_hours, :]
test = values[n_train_hours:, :]
# split into input and outputs
train_X, train_y = train[:, :-1], train[4:, -1]
train_X_1 = train_X[:-4, :]
train_X_2 = train_X[1:-3, :]
train_X_3 = train_X[2:-2, :]
train_X_4 = train_X[3:-1, :]
train_X_5 = train_X[4:, :]
com = np.array([train_X_1, train_X_2, train_X_3, train_X_4, train_X_5])
train_X = com.transpose((1,0,2))
#print(arr5)
print(train_X)
test_X, test_y = test[:, :-1], test[4:, -1]
test_X_1 = test_X[:-4, :]
test_X_2 = test_X[1:-3, :]
test_X_3 = test_X[2:-2, :]
test_X_4 = test_X[3:-1, :]
test_X_5 = test_X[4:, :]
com1 = np.array([test_X_1, test_X_2, test_X_3, test_X_4, test_X_5])
test_X = com1.transpose((1,0,2))
# reshape input to be 3D [samples, timesteps, features]
#trainxshape = int (train_X.shape[0]/10)
#testxshape=int(test_X.shape[0]/10)
#train_X = train_X.reshape((trainxshape, 10, train_X.shape[1]))
#test_X = test_X.reshape((testxshape, 10, test_X.shape[1]))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
# design network
model = Sequential()
model.add(GRU(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# fit network
history = model.fit(train_X, train_y, epochs=50, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)
# make a prediction
yhat = model.predict(test_X)
#test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))
# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X_2[:, 1:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X_2[:, 1:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:,0]
# calculate RMSE
rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
print('Test RMSE: %.3f' % rmse)
#np.savetxt('new.csv', inv_yhat, delimiter=',')#存储预测结果
#np.savetxt('true.csv', inv_y, delimiter=',')#存储真实数据
- 1
- 2
- 3
前往页