-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras_lstm.py
118 lines (88 loc) · 3.34 KB
/
keras_lstm.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
# LSTM and CNN for sequence classification
import numpy as np
import math
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from keras.layers.convolutional import Conv1D, MaxPooling1D
from keras.preprocessing import sequence
import pandas as pd
# fix random seed for reproducibility
#numpy.random.seed(7)
def standardize_data(array):
#takes in 2d arrays
#relative scale
a = array.copy()
for i in range(a.shape[-1]):
mean = np.mean(a[:, i])
std = np.std(a[:, i])
a[:,i] = (a[:,i] - mean)/std
return a
# load the dataset
DIR = "../data/stock_train_data_20170910.csv"
COLUMNS = list(range(1,91)) #Read 88 features, weight, label
all_set = pd.read_csv(DIR, skipinitialspace=True, skiprows=0, usecols=COLUMNS).as_matrix()
np.random.shuffle(all_set)
TESTDIR="../data/stock_test_data_20170910.csv"
pred_col=list(range(1,89)) #1-88,Features
prediction_set=pd.read_csv(TESTDIR, skipinitialspace=True, skiprows=0, usecols=pred_col).as_matrix()
#fea_col = training_set[...,:88]
#label_col = [...,-1]
print('Datasets Loaded.')
cut = math.floor(all_set.shape[0]*0.95)
X_train=all_set[0:cut, :88]
y_train=all_set[0:cut, -1]
training_weight=all_set[0:cut,-2]
X_test = all_set[cut:, :88]
y_test = all_set[cut:, -1]
#standardize the data
X_train=standardize_data(X_train)
X_test=standardize_data(X_test)
print('Data prepared.')
# create the model
model = Sequential()
'''
model.add(Conv1D(input_shape=(None,88),filters=32, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
'''
#specially prepare data for lstm
X_train=np.expand_dims(X_train,axis=2)
X_test=np.expand_dims(X_test,axis=2)
X_predict=np.expand_dims(prediction_set,axis=2)
model.add(LSTM(40,input_shape=(88,1),return_sequences=True, recurrent_dropout=0.2,dropout=0.2))
#model.add(Dropout(0.2))
model.add(LSTM(20,recurrent_dropout=0.2,dropout=0.2))
#model.add(Dropout(0.2))
adam=keras.optimizers.Adam(lr=0.002)
'''
model.add(Dense(80,input_shape=(88,),activation='selu'))
#keras.layers.normalization.BatchNormalization()
model.add(Dropout(0.3))
model.add(Dense(40,activation='selu'))
#keras.layers.normalization.BatchNormalization()
model.add(Dropout(0.3))
'''
#Output layer
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, epochs=50, batch_size=3000, validation_data=(X_test,y_test), sample_weight=training_weight) #not sure whether validation_split uses weight
print('Training finished. Start predicting.')
'''
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
'''
predictions = model.predict(X_predict,verbose=1).flatten()
print('Max =' + str(np.max(predictions)))
print('Min =' + str(np.min(predictions)))
#predictions=np.clip(predictions,0.001,0.999)
#testdata: 321674 ~ 521619
indices = pd.read_csv(TESTDIR, skipinitialspace=True, skiprows=0, usecols=[0]).as_matrix().flatten()
df = pd.DataFrame(data={'id':indices, 'proba':predictions})
df.to_csv('result_lstm_3_2.csv',index=False)
print('Result saved.')
#model.save('test_1.h5') # creates a HDF5 file 'my_model.h5'
#del model # deletes the existing model
# returns a compiled model identical to the previous one
#model = keras.models.load_model('my_model.h5')