-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLSTM_WazirX_Train.py
167 lines (113 loc) · 3.64 KB
/
LSTM_WazirX_Train.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#import packages
import pandas as pd
import numpy as np
#to plot within notebook
import matplotlib.pyplot as plt
#importing required libraries
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from keras.models import model_from_json
#setting figure size
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,200
#for normalizing data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
#read the file
df = pd.read_csv('Stockvol.csv')
#print the head
#setting index as date
df["['date'"] = [str(x).split("'")[1] for x in df["['date'"]]
# print(df[" 'BTC/USDT'"])
df["['date'"] = pd.to_datetime(df["['date'"])#,format='%Y-%m-%d %H:%M')
df.index = df["['date'"]
df=df.rename(columns={"['date'":"Date"})
df=df.rename(columns={" 'BTC/USDT'":"Close"})
#creating dataframe
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Close'])
last=0
for i in range(0,len(data)):
new_data['Date'][i] = data['Date'][i]
new_data['Close'][i] = data['Close'][i]
last = data['Close'][i]
ld = data['Date'][i]
# new_data=new_data[20000:]
# print(new_data.head())
# plt.plot(new_data[['Close']])
# plt.show()
# print(ld)
sss= (pd.date_range(ld, periods=91, freq="min"))
ld=[]
for xx in sss:
ld.append(str(xx))
# print(new_data['Date'][i-1])
# ld=ld[1:]
# print(ld)
for x in range(1,90):
new_data.loc[len(new_data.index)+x] = [ld[x],last]
print(new_data[-13:])
#setting index
new_data.index = new_data.Date
new_data.drop('Date', axis=1, inplace=True)
#creating train and test sets
dataset = new_data.values
ll=len(dataset)-90
vl=ll//9
tl=ll-vl
# print(type(new_data))
# for x in range(90):
# dataset= np.append(dataset,[[last,]],axis=0)
# new_data.loc[len(new_data.index)+x] = [last]
train = dataset[0:tl,:]
valid = dataset[tl:,:]
# print(valid)
# converting dataset into x_train and y_train
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)
# print(scaled_data)
x_train, y_train = [], []
for i in range(200,len(train)):
x_train.append(scaled_data[i-200:i,0])
y_train.append(scaled_data[i,0])
x_train, y_train = np.array(x_train), np.array(y_train)
# print(x_train)
x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))
# print(x_train)
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(units=400, return_sequences=True, input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=400))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=1, batch_size=1, verbose=2)
#predicting 246 values, using past 200 from the train data
inputs = new_data[len(new_data) - len(valid) - 200:].values
inputs = inputs.reshape(-1,1)
inputs = scaler.transform(inputs)
X_test = []
for i in range(200,inputs.shape[0]):
X_test.append(inputs[i-200:i,0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))
closing_price = model.predict(X_test)
closing_price = scaler.inverse_transform(closing_price)
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
# rms=np.sqrt(np.mean(np.power((valid-closing_price),2)))
# print(rms)
#for plotting
train = new_data[:tl]
valid = new_data[tl:]
print(len(closing_price))
print(len(valid))
valid['Predictions'] = closing_price
plt.plot(train['Close'][tl-200:])
plt.plot(valid[['Close','Predictions']])
plt.show()