-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train.py.txt
51 lines (41 loc) · 1.42 KB
/
Train.py.txt
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
import pandas as pd
import numpy as np
import json
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras.layers import Activation
data = pd.DataFrame(data=pd.read_csv("test1.csv"))
dt =pd.DataFrame(data=pd.read_csv("test1.csv"))
#print(data)
dt.dropna(inplace=True)
data.dropna(inplace=True)
#print(data.values)
x_train = data[["Open","High","Low","Close","Volume"]]
print(x_train)
y_train = data["Close"]
print(y_train)
#converting into an array
x_train, y_train = np.array(x_train), np.array(y_train)
#LSTM needs 3d array
x_train = np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))
dt1 =pd.DataFrame(data=pd.read_csv("test.csv"))
x_test = []
x_test = dt1[["Open","High","Low","Close","Volume"]]
x_test = np.array(x_test)
x_test = np.reshape(x_test,(x_test.shape[0],x_test.shape[1],1))
model = Sequential()
model.add(LSTM(units=50,return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50))
model.add(Dense(1))
model.add(Activation('relu'))
model.compile(loss='logcosh',optimizer='adam')
model.fit(x_train, y_train, epochs=50, batch_size=32 ,verbose=1)
closing_price = model.predict(x_test)
print(closing_price)
model_json = model.to_json()
with open('model_save.json', 'w') as json_file:
json_file.write(model_json)
model.save_weights('model_save.h5')
print('done.....')