Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- time X1 X2 X3 X4 PREDICT(t+60)
- 0 min 63 0 0 1 69
- 60 min 63 0 2 1 104
- 120 min 0 10 0 1 pred next value
- x_test_n = np.flip(x_test_n, axis=0)
- curr_frame = x_test_n[0]
- predicted = []
- newaxis=[]
- for i in range(len(x_test_n)):
- predicted.append(model.predict(curr_frame[newaxis,:])[0,0])
- curr_frame = curr_frame[1:]
- curr_frame = np.insert(curr_frame, [num_time_steps-1], predicted[-1], axis=0)
- data= pd.DataFrame(data,columns=['X1','X2','X3','X4'])
- data.columns = ['X1', 'X2', 'X3','X4']
- pd.options.display.float_format = '{:,.0f}'.format
- data = data.dropna ()
- y=data['Y'].astype(int)
- cols=['X1', 'X2', 'X3','X4']
- x=data[cols].astype(int)
- data=data.values
- scaler_x = preprocessing.MinMaxScaler(feature_range =(0, 1))
- x = np.array(x).reshape ((len(x),4 ))
- x = scaler_x.fit_transform(x)
- scaler_y = preprocessing.MinMaxScaler(feature_range =(0, 1))
- y = np.array(y).reshape ((len(y), 1))
- y = scaler_y.fit_transform(y)
- train_end = 80
- x_train=x[0: train_end ,]
- x_test=x[train_end +1: ,]
- y_train=y[0: train_end]
- y_test=y[train_end +1:]
- x_train=x_train.reshape(x_train.shape +(1,))
- x_test=x_test.reshape(x_test.shape + (1,))
- num_time_steps=3
- model = Sequential()
- model.add(LSTM(4, return_sequences=True,input_shape=(num_time_steps,x_train_n.shape[2]))) # returns a sequence of vectors of dimension 32
- model.add(LSTM(8, return_sequences=True)) # returns a sequence of vectors of dimension 32
- model.add(LSTM(8)) # return a single vector of dimension 32
- model.add(Dense(1))
- batchsize = 1
- model.compile(loss="mean_squared_error",optimizer="adam")
- history = model.fit(x_train,y_train, batch_size = batchsize, nb_epoch=30,validation_data=(x_test, y_test),shuffle =True)
- model.reset_states()
- x_test_n = np.flip(x_test_n, axis=0)
- curr_frame = x_test_n[0]
- predicted = []
- newaxis=[]
- for i in range(len(x_test_n)):
- predicted.append(model.predict(curr_frame[newaxis,:])[0,0])
- curr_frame = curr_frame[1:]
- curr_frame = np.insert(curr_frame, [num_time_steps-1], predicted[-1], axis=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement