-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stock prediction(Creating the model).py
148 lines (71 loc) · 2.01 KB
/
Stock prediction(Creating the model).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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
# In[2]:
df=pd.read_csv('Google_Stock_Price_Train.csv',index_col='Date',parse_dates=True)
# In[3]:
df.head()
# In[4]:
df.shape
# In[5]:
df.isna().any()
# In[6]:
#Changing the datatype of close and volume t ofloat
df['Close']=df['Close'].str.replace(',','').astype(float)
df['Volume']=df['Volume'].str.replace(',','').astype(float)
# In[7]:
training_set=df['Open']
training_set=pd.DataFrame(training_set)
# In[8]:
from sklearn.preprocessing import MinMaxScaler
sc=MinMaxScaler(feature_range=(0,1))
training_set_scaled=sc.fit_transform(training_set)
# In[9]:
training_set
# In[10]:
training_set_scaled
# In[12]:
#creating a data structure with 100 time steps and 1 output
X_train=[]
y_train=[]
for i in range(100,1258):
X_train.append(training_set_scaled[i-100:i,0])
y_train.append(training_set_scaled[i,0])
X_train,y_train=np.array(X_train),np.array(y_train)
# In[14]:
#reshaping
X_train=np.reshape(X_train,(X_train.shape[0],X_train.shape[1],1))
# In[15]:
X_train.shape
# In[16]:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout
# In[18]:
#creating the model RNN
model = Sequential()
#adding the first LSTM layer
model.add(LSTM(units=50,return_sequences=True,input_shape=(X_train.shape[1],1)))
model.add(Dropout(0.2))
#adding the second LSTM layer
model.add(LSTM(units=50,return_sequences=True))
model.add(Dropout(0.2))
#adding the third LSTM layer
model.add(LSTM(units=50,return_sequences=True))
model.add(Dropout(0.2))
#adding the fourth LSTM layer
model.add(LSTM(units=50))
model.add(Dropout(0.2))
#adding the output layer
model.add(Dense(units=1))
# In[19]:
model.compile(optimizer='adam',loss=tf.keras.losses.mse)
# In[20]:
model.fit(X_train,y_train,epochs=100,batch_size=32)
# In[ ]: