-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.py
executable file
·275 lines (235 loc) · 10.8 KB
/
load_data.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from datetime import datetime
import numpy as np
import os
import pandas as pd
# return data in form (batch size, num stocks, time_steps, feature_size)
def load_cla_data(data_path, tra_date, val_date, tes_date, seq=2,
date_format='%Y-%m-%d'):
fnames = [fname for fname in os.listdir(data_path) if
os.path.isfile(os.path.join(data_path, fname))]
print(len(fnames), ' tickers selected')
ticker_num = len(fnames)
fnames.remove('SNP500.csv')
fnames.insert(0, 'SNP500.csv')
print(fnames)
data_EOD = []
for index, fname in enumerate(fnames):
# print(fname)
single_EOD = np.genfromtxt(
os.path.join(data_path, fname), dtype=float, delimiter=',',
skip_header=False
)
# print('data shape:', single_EOD.shape)
data_EOD.append(single_EOD)
fea_dim = data_EOD[0].shape[1] - 2
trading_dates = np.genfromtxt(
os.path.join(data_path, '..', 'trading_dates.csv'), dtype=str,
delimiter=',', skip_header=False
)
print(len(trading_dates), 'trading dates')
# transform the trading dates into a dictionary with index, at the same
# time, transform the indices into a dictionary with weekdays
dates_index = {}
# indices_weekday = {}
data_wd = np.zeros([len(trading_dates), 5], dtype=float)
wd_encodings = np.identity(5, dtype=float)
for index, date in enumerate(trading_dates):
dates_index[date] = index
# indices_weekday[index] = datetime.strptime(date, date_format).weekday()
data_wd[index] = wd_encodings[datetime.strptime(date, date_format).weekday()]
tra_ind = dates_index[tra_date]
val_ind = dates_index[val_date]
tes_ind = dates_index[tes_date]
print('tra, val, tes index: ', tra_ind, val_ind, tes_ind)
# count training, validation, and testing instances
def count_instances(start_ind, end_ind):
num = 0
for date_ind in range(start_ind, end_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
# for tic_ind in range(len(fnames)):
if data_EOD[0][date_ind - seq: date_ind, :].min() > -123320:
num += 1
return num
tra_num = count_instances(tra_ind, val_ind)
val_num = count_instances(val_ind, tes_ind)
tes_num = count_instances(tes_ind, len(trading_dates))
print(tra_num, ' training instances')
print(val_num, ' validaing instances')
print(tes_num, ' testing instances')
# generate training, validation, and testing instances
# training
tra_pv = np.zeros([tra_num, ticker_num, seq, fea_dim], dtype=float)
tra_wd = np.zeros([tra_num, seq, 5], dtype=float)
tra_gt = np.zeros([tra_num, ticker_num - 1], dtype=float)
ins_ind = 0
for date_ind in range(tra_ind, val_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
tra_pv[ins_ind, tic_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, : -2]
tra_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
if tic_ind != 0:
tra_gt[ins_ind, tic_ind-1] = (data_EOD[tic_ind][date_ind][-2] + 1) // 2
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
ins_ind += 1
# validation
val_pv = np.zeros([val_num, ticker_num, seq, fea_dim], dtype=float)
val_wd = np.zeros([val_num, seq, 5], dtype=float)
val_gt = np.zeros([val_num, ticker_num-1], dtype=float)
ins_ind = 0
for date_ind in range(val_ind, tes_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
val_pv[ins_ind][tic_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, :-2]
val_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
if tic_ind != 0:
val_gt[ins_ind, tic_ind-1] = (data_EOD[tic_ind][date_ind][-2] + 1) // 2
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
ins_ind += 1
# testing
tes_pv = np.zeros([tes_num, ticker_num, seq, fea_dim], dtype=float)
tes_wd = np.zeros([tes_num, seq, 5], dtype=float)
tes_gt = np.zeros([tes_num, ticker_num-1], dtype=float)
ins_ind = 0
for date_ind in range(tes_ind, len(trading_dates)):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
tes_pv[ins_ind][tic_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, :-2]
# # for the momentum indicator
# tes_pv[ins_ind, -1, -1] = data_EOD[tic_ind][date_ind - 1, -1] - data_EOD[tic_ind][date_ind - 11, -1]
tes_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
if tic_ind != 0:
tes_gt[ins_ind, tic_ind-1] = (data_EOD[tic_ind][date_ind][-2] + 1) // 2
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
ins_ind += 1
print(tra_pv)
print(tra_gt)
# pd.DataFrame(np.squeeze(tra_gt)).to_csv("./output/prediction.csv")
return tra_pv, tra_wd, tra_gt, val_pv, val_wd, val_gt, tes_pv, tes_wd, tes_gt
if __name__ == '__main__':
# TEST
tra_pv, _, tra_gt, _, _, val_gt, _, _, tes_gt = load_cla_data(
'./data/kdd17/ourpped/',
'2007-01-03', '2015-01-02', '2016-01-04'
)
print('tra_pv', tra_pv.shape)
print('tra_gt', tra_gt.shape)
print(np.unique(tra_gt))
print(tes_gt.shape)
print(np.sum(tra_gt))
print(np.sum(val_gt))
print(np.sum(tes_gt))
print(np.sum(tes_gt) / 3720)
# return data in form (batch size, time_steps, feature_size)
def load_cla_data2(data_path, tra_date, val_date, tes_date, seq=2,
date_format='%Y-%m-%d'):
fnames = [fname for fname in os.listdir(data_path) if
os.path.isfile(os.path.join(data_path, fname))]
print(len(fnames), ' tickers selected')
fnames.remove('SNP500.csv')
fnames.insert(0, 'SNP500.csv')
data_EOD = []
for index, fname in enumerate(fnames):
# print(fname)
single_EOD = np.genfromtxt(
os.path.join(data_path, fname), dtype=float, delimiter=',',
skip_header=False
)
# print('data shape:', single_EOD.shape)
data_EOD.append(single_EOD)
fea_dim = data_EOD[0].shape[1] - 2
trading_dates = np.genfromtxt(
os.path.join(data_path, '..', 'trading_dates.csv'), dtype=str,
delimiter=',', skip_header=False
)
print(len(trading_dates), 'trading dates:')
# transform the trading dates into a dictionary with index, at the same
# time, transform the indices into a dictionary with weekdays
dates_index = {}
# indices_weekday = {}
data_wd = np.zeros([len(trading_dates), 5], dtype=float)
wd_encodings = np.identity(5, dtype=float)
for index, date in enumerate(trading_dates):
dates_index[date] = index
# indices_weekday[index] = datetime.strptime(date, date_format).weekday()
data_wd[index] = wd_encodings[datetime.strptime(date, date_format).weekday()]
tra_ind = dates_index[tra_date]
val_ind = dates_index[val_date]
tes_ind = dates_index[tes_date]
print(tra_ind, val_ind, tes_ind)
# count training, validation, and testing instances
def count_instances(start_ind, end_ind):
num = 0
for date_ind in range(start_ind, end_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
num += 1
return num
tra_num = count_instances(tra_ind, val_ind)
val_num = count_instances(val_ind, tes_ind)
tes_num = count_instances(tes_ind, len(trading_dates))
print(tra_num, ' training instances')
print(val_num, ' validaing instances')
print(tes_num, ' testing instances')
# generate training, validation, and testing instances
# training
tra_pv = np.zeros([tra_num, seq, fea_dim], dtype=float)
tra_wd = np.zeros([tra_num, seq, 5], dtype=float)
tra_gt = np.zeros([tra_num, 1], dtype=float)
ins_ind = 0
for date_ind in range(tra_ind, val_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
tra_pv[ins_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, : -2]
tra_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
tra_gt[ins_ind, 0] = (data_EOD[tic_ind][date_ind][-2] + 1) / 2
ins_ind += 1
# validation
val_pv = np.zeros([val_num, seq, fea_dim], dtype=float)
val_wd = np.zeros([val_num, seq, 5], dtype=float)
val_gt = np.zeros([val_num, 1], dtype=float)
ins_ind = 0
for date_ind in range(val_ind, tes_ind):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
val_pv[ins_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, :-2]
val_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
val_gt[ins_ind, 0] = (data_EOD[tic_ind][date_ind][-2] + 1) / 2
ins_ind += 1
# testing
tes_pv = np.zeros([tes_num, seq, fea_dim], dtype=float)
tes_wd = np.zeros([tes_num, seq, 5], dtype=float)
tes_gt = np.zeros([tes_num, 1], dtype=float)
ins_ind = 0
for date_ind in range(tes_ind, len(trading_dates)):
# filter out instances without length enough history
if date_ind < seq:
continue
for tic_ind in range(len(fnames)):
if data_EOD[tic_ind][date_ind - seq: date_ind, :].min() > -123320:
tes_pv[ins_ind] = data_EOD[tic_ind][date_ind - seq: date_ind, :-2]
# # for the momentum indicator
# tes_pv[ins_ind, -1, -1] = data_EOD[tic_ind][date_ind - 1, -1] - data_EOD[tic_ind][date_ind - 11, -1]
tes_wd[ins_ind] = data_wd[date_ind - seq: date_ind, :]
tes_gt[ins_ind, 0] = (data_EOD[tic_ind][date_ind][-2] + 1) / 2
ins_ind += 1
return tra_pv, tra_wd, tra_gt, val_pv, val_wd, val_gt, tes_pv, tes_wd, tes_gt