-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpred2.py
More file actions
112 lines (68 loc) · 2.76 KB
/
pred2.py
File metadata and controls
112 lines (68 loc) · 2.76 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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pandas_datareader as web
import datetime as dt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.layers import Dense, Dropout, LSTM
from tensorflow.keras.models import Sequential
def predd2(crp, cur):
crypto_curr = crp
curr = cur
start = dt.datetime(2016,1,1)
end = dt.datetime.now()
data = web.DataReader(f'{crypto_curr}-{curr}', 'yahoo', start, end)
#Prepare data squeeze in -1 -- 0 -- 1
print(data.head())
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1,1))
pred_days = 90
x_train, y_train = [], []
for x in range(pred_days, len(scaled_data)):
x_train.append(scaled_data[x-pred_days:x, 0])
y_train.append(scaled_data[x, 0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
#NN
#numpy == 1.19.5
#model =
model = Sequential()
model.add(LSTM(units = 50, return_sequences = True, input_shape = (x_train.shape[1], 1)))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(units = 50))
model.add(Dropout(0.2))
model.add(Dense(units = 1))
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
#model.fit(x_train, y_train, epochs = 50, batch_size = 32)
#model.fit(x_train, y_train, epochs = 25, batch_size = 32)
model.fit(x_train, y_train, epochs = 2, batch_size = 32)
#Testing model
test_s = dt.datetime(2020,1,1)
test_e = dt.datetime.now()
test_data = web.DataReader(f'{crypto_curr}-{curr}', 'yahoo', test_s, test_e)
act_prices = test_data['Close'].values
total_dataset = pd.concat((data['Close'], test_data['Close']), axis = 0)
model_inputs = total_dataset[len(total_dataset) - len(test_data) - pred_days:].values
model_inputs = model_inputs.reshape(-1, 1)
model_inputs = scaler.fit_transform(model_inputs)
x_test = []
for x in range(pred_days, len(model_inputs)):
x_test.append(model_inputs[x-pred_days:x, 0])
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
pred_prices = model.predict(x_test)
pred_prices = scaler.inverse_transform(pred_prices)
plt.plot(act_prices, color = 'black', label = 'Actual prices')
plt.plot(pred_prices, color = 'green', label = 'Pred prices')
plt.title(crp)
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend(loc = 'upper left')
return str(pred_prices[-1])
#plt.show()
#input1 = input()
#if input1 == "y":
#model.save('dbot/moddel')
#predd2("RVN", "INR")