-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRNN.py
More file actions
122 lines (67 loc) · 2.71 KB
/
RNN.py
File metadata and controls
122 lines (67 loc) · 2.71 KB
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import urllib.request
import requests
import re
"""
Companies = ["L-T","RY","TD","BNS","SLF","CRM","FB","MSFT"]
link = "http://www.finance.yahoo.com/quote/RY/history?period1=1410840000&period2=1505534400&interval=1d&filter=history&frequency=1d"
link = link.split("/")
link[4] = Companies[0]
link = "/".join(link)
openSite = urllib.request.urlopen(link).read()
soup = BeautifulSoup(openSite,"lxml")
#<a class="Fl(end) Mt(3px) Cur(p)">
for links in soup.find_all('a'):
print(links.get_all("href"))
"""
def nextDayForecast(trainingCsvFile):
dataset = pd.read_csv(trainingCsvFile)
training_data = dataset.iloc[0 : len(dataset) - 20,1:2].values
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler()
training_data = sc.fit_transform(training_data)
X_train = training_data[0:len(training_data) - 1]
y_train = training_data[1:len(training_data)]
X_train = np.reshape(X_train, (len(training_data) - 1,1,1))
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
regressor = Sequential()
regressor.add(LSTM(units = 4,activation = "sigmoid", input_shape = (None,1)))
regressor.add(Dense(units = 1))
regressor.compile(optimizer = "adam", loss ="mean_squared_error")
regressor.fit(X_train,y_train,batch_size = 32, epochs = 100)
real_stock_price = dataset.iloc[len(dataset) - 20:len(dataset), 1:2].values
inputs = real_stock_price
inputs = sc.fit_transform(inputs)
inputs = np.reshape(inputs,(len(real_stock_price),1,1))
predicted_stock_price = regressor.predict(inputs)
predicted_stock_price = sc.inverse_transform(predicted_stock_price)
return real_stock_price, predicted_stock_price
co = ["EFX.csv","SHOP.csv","GOOGL.csv","IBM.csv","TD.csv"]
#for i in range(len(co)):
actual, forecasted = nextDayForecast(co[0])
def plotResults(x,y):
plt.plot(actual,color="red",label="Red is the actual price")
plt.plot(forecasted, color= "blue",label="Blue is the forecasted price")
plt.xlabel("Time")
plt.ylabel("Stock Price")
plt.legend()
plt.show
"""
if i == 0:
plt.title("Equifax Stock")
elif i == 1:
plt.title("Shopify Stock")
elif i == 2:
plt.title("Google Stock")
elif i == 3:
plt.title("IBM Stock")
elif i == 4:
plt.title("TD Stock")
plt.show()
"""
plotResults(actual,forecasted)