-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnn.py
115 lines (86 loc) · 3.78 KB
/
rnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
####### Data Preprocessing ####################################################
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
# Importing Data
dataset_train = pd.read_csv('Manulife_Stock_Price_Train.csv')
training_set = dataset_train.iloc[:, 1:2].values
# Scale features between 0-1
scaling = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = scaling.fit_transform(training_set)
# Data structure
# Remember the last 2 months
xtrain = []
ytrain = []
# Using ~70% of Dataset to train (1600/2300)
for i in range(60, 1600):
xtrain.append(training_set_scaled[i-60:i, 0])
ytrain.append(training_set_scaled[i, 0])
xtrain, ytrain = np.array(xtrain), np.array(ytrain)
xtrain = np.reshape(xtrain, (xtrain.shape[0], xtrain.shape[1], 1))
################################################################################
####### Recurrent Neural Network ###############################################
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.models import Sequential
# Starting RNN
rnn = Sequential()
# First LSTM layer w/ dropout
rnn.add(LSTM(units = 50, return_sequences = True, input_shape = (xtrain.shape[1], 1)))
rnn.add(Dropout(0.2))
# Second LSTM layer w/ dropout
rnn.add(LSTM(units = 50, return_sequences = True))
rnn.add(Dropout(0.2))
# Third LSTM layer w/ dropout
rnn.add(LSTM(units = 50))
rnn.add(Dropout(0.2))
# Output layer
rnn.add(Dense(units = 1))
# Compiling the RNN
# Adam optimization is a stochastic gradient descent method
# It is based on adaptive estimation of first-order and second-order moments.
rnn.compile(optimizer = 'adam', loss = 'mean_squared_error')
# Train the model by fitting the RNN for the training set
history = rnn.fit(xtrain, ytrain, epochs = 100, batch_size = 32)
####################################################################################
####### Run On Test Set ############################################################
# Getting real stock price from csv
dataset_test = pd.read_csv('Manulife_Stock_Price_Test.csv')
real_stock_price = dataset_test.iloc[:, 1:2].values
# Getting prediction stock price
dataset_total = pd.concat((dataset_train['Open'], dataset_test['Open']), axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values
inputs = inputs.reshape(-1,1)
inputs = scaling.transform(inputs)
xtest = []
for i in range(60, 760):
xtest.append(inputs[i-60:i, 0])
xtest = np.array(xtest)
xtest = np.reshape(xtest, (xtest.shape[0], xtest.shape[1], 1))
predicted_stock_price = rnn.predict(xtest)
predicted_stock_price = scaling.inverse_transform(predicted_stock_price)
####################################################################################
####### Calculate RMSE #############################################################
import math
from sklearn.metrics import mean_squared_error
rmse = math.sqrt(mean_squared_error(real_stock_price, predicted_stock_price))
print(rmse)
####################################################################################
####### Training Loss Curve ########################################################
loss = history.history['loss']
epochs = range(1, len(loss)+1)
plt.plot(epochs, loss, 'ro', label='Training Loss')
plt.legend()
plt.show()
####################################################################################
####### Graph Results ##############################################################
plt.plot(real_stock_price, color = 'red', label = 'Real Stock Price')
plt.plot(predicted_stock_price, color = 'black', label = 'Predicted Stock Price')
plt.title('Stock Price Prediction')
plt.xlabel('Days Forward')
plt.ylabel('Stock Price')
plt.legend()
plt.show()
####################################################################################