-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnn.py
81 lines (52 loc) · 2.05 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
# Recurrent Neural Network
# Part 1 - Data Preprocessing
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the training set
dataset_train = pd.read_csv('Google_Stock_Price_Train.csv')
training_set = dataset_train.iloc[: , 1:2].values
# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0,1))
training_set_scale = sc.fit_transform(training_set)
# Creating a data structure with 60 timesteps and 1 output
X_train=[]
y_train=[]
for i in range(60 , 1258):
X_train.append(training_set_scale[i-60:i , 0])
y_train.append(training_set_scale[i , 0])
X_train , y_train = np.array(X_train) , np.array(y_train)
# Reshaping
X_train = np.reshape(X_train , (X_train.shape[0] , X_train.shape[1] , 1))
# Part 2 - Building the RNN
# Importing the Keras libraries and package
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
# Initialising the RNN
classifier = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
classifier.add(LSTM(units=50 ,return_sequences= True , input_shape = (X_train.shape[1] ,1)))
classifier.add(Dropout(0.2))
# Adding a second LSTM layer and some Dropout regularisation
classifier.add(LSTM(units=50 ,return_sequences= True ))
classifier.add(Dropout(0.2))
# Adding a third LSTM layer and some Dropout regularisation
classifier.add(LSTM(units=50 ,return_sequences= True ))
classifier.add(Dropout(0.2))
# Adding a fourth LSTM layer and some Dropout regularisation
classifier.add(LSTM(units=50 ))
classifier.add(Dropout(0.2))
# Adding the output layer
classifier.add(Dense(units=1))
# Compiling the RNN
classifier.compile(optimizer='adam' , loss='mean_squared_error')
# Fitting the RNN to the Training set
classifier.fit(X_train , y_train , epochs = 100 , batch_size = 32)
# Part 3 - Making the predictions and visualising the results
# Getting the real stock price of 2017
# Getting the predicted stock price of 2017
# Visualising the results