-
Notifications
You must be signed in to change notification settings - Fork 8
/
data_load.py
275 lines (212 loc) · 6.04 KB
/
data_load.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
import datetime
from random import randint
import pandas as pd
import numpy as np
from dateutil.relativedelta import relativedelta
from math import pi, sin, cos
def count_positive(v):
count = 0
for i in range(len(v)):
if v[i] > 0:
count += 1
return count
def get_keep_indexes(x):
indexes = []
for i in range(len(x)):
if count_positive(x[i, :]) < 10:
continue
if count_positive(x[i, :15]) < 1:
continue
if count_positive(x[i, -15:]) < 1:
continue
indexes.append(i)
return indexes
def get_x_z_at_i_t(s, v, datetime_offset: datetime.datetime, i: int, t: int, gran):
x = []
_, T = s.shape
x.append(s[i, t - 1] / v[i])
x.append(t / T)
d = datetime_offset
if gran == 'm':
d += relativedelta(months=t)
x.append(sin(2 * pi * ((d.month - 1) / 11)))
x.append(cos(2 * pi * ((d.month - 1) / 11)))
elif gran == 'h':
d += relativedelta(hours=t)
x.append(sin(2 * pi * (d.hour / 23)))
x.append(cos(2 * pi * (d.hour / 23)))
weekday = d.weekday()
x.append(sin(2 * pi * (weekday / 6)))
x.append(cos(2 * pi * (weekday / 6)))
else:
raise Exception('gran not supported')
z = s[i, t]
return x, z
def get_window_x_z_at_i_t(s, v, datetime_offset: datetime.datetime, i: int, t_window: int, window_len: int, gran):
X = []
Z = []
for t in range(t_window, t_window + window_len):
x, z = get_x_z_at_i_t(s, v, datetime_offset, i, t, gran)
X.append(x)
Z.append([z])
return X, Z
def get_x_z(s, v, datetime_offset: datetime.datetime, t_offset: int, length: int, window_length: int, gran='m'):
assert len(s) == len(v)
X = []
Z = []
V = []
N, _ = s.shape
for i in range(N):
for t in range(t_offset, t_offset + length - window_length + 1):
x, z = get_window_x_z_at_i_t(s, v, datetime_offset, i, t, window_length, gran)
X.append(x)
Z.append(z)
V.append([v[i]])
X = np.array(X)
Z = np.array(Z)
V = np.array(V)
return X, Z, V
def get_x_z_subsample(s, v, datetime_offset: datetime.datetime, t_offset: int, length: int, window_length: int, count: int, gran='m'):
assert len(s) == len(v)
X = []
Z = []
V = []
N, _ = s.shape
for c in range(count):
i = randint(0, len(s) - 1)
t = randint(t_offset, t_offset + length - window_length + 1)
x, z = get_window_x_z_at_i_t(s, v, datetime_offset, i, t, window_length, gran)
X.append(x)
Z.append(z)
V.append([v[i]])
if c % 1000 == 0:
print('sampling {}/{}'.format(c, count))
X = np.array(X)
Z = np.array(Z)
V = np.array(V)
return X, Z, V
def get_parts_series():
df = pd.read_csv('data/carparts.csv')
df = df.dropna()
s = df.values
# no item id
s = s[:, 1:]
indexes = get_keep_indexes(s)
s = s[indexes]
datetime_offset = datetime.datetime(1998, 1, 1)
return datetime_offset, s
def get_elec_series():
df = pd.read_csv('data/elec.csv')
datetime_offset = datetime.datetime(2000, 1, 1)
s = df.values.T
return datetime_offset, s
def load_elec():
datetime_offset, s = get_elec_series()
_, T = s.shape
enc_len = 168
dec_len = 24
train_len = T - dec_len - 1
# first t of the series
t1 = 1
# first t of prediction range
t0 = t1 + train_len
# first t of encoder (validation)
t_enc = t0 - enc_len
v = 1 + np.mean(s[:, t1:t0], axis=1)
gran = 'h'
x_train, z_train, v_train = get_x_z_subsample(
s,
v,
datetime_offset,
t_offset=t1,
length=train_len,
window_length=enc_len + dec_len,
count=100_000,
gran=gran
)
p = np.squeeze(v_train / np.sum(v_train))
v_train = np.expand_dims(v_train, axis=-1)
enc_x, enc_z, _ = get_x_z(
s,
v,
datetime_offset,
t_offset=t_enc,
length=enc_len,
window_length=enc_len,
gran=gran
)
dec_x, dec_z, _ = get_x_z(
s,
v,
datetime_offset,
t_offset=t0,
length=dec_len,
window_length=dec_len,
gran=gran
)
v = np.expand_dims(v, axis=-1)
v = np.expand_dims(v, axis=-1)
data = {
'x': x_train,
'z': z_train,
'v': v_train,
'p': p,
'enc_x': enc_x,
'enc_z': enc_z,
'dec_x': dec_x[:, :, 1:],
'dec_z': dec_z,
'dec_v': v,
}
return datetime_offset, data
def load_parts():
datetime_offset, s = get_parts_series()
enc_len = 8
dec_len = 8
train_len = 42
# first t of the series
t1 = 1
# first t of prediction range
t0 = t1 + train_len
# first t of encoder (validation)
t_enc = t0 - enc_len
v = 1 + np.mean(s[:, t1:t0], axis=1)
x_train, z_train, v_train = get_x_z(
s,
v,
datetime_offset,
t_offset=t1,
length=train_len,
window_length=8,
)
p = np.squeeze(v_train / np.sum(v_train))
v_train = np.expand_dims(v_train, axis=-1)
enc_x, enc_z, _ = get_x_z(
s,
v,
datetime_offset,
t_offset=t_enc,
length=enc_len,
window_length=enc_len,
)
dec_x, dec_z, _ = get_x_z(
s,
v,
datetime_offset,
t_offset=t0,
length=dec_len,
window_length=dec_len
)
v = np.expand_dims(v, axis=-1)
v = np.expand_dims(v, axis=-1)
data = {
'x': x_train,
'z': z_train,
'v': v_train,
'p': p,
'enc_x': enc_x,
'enc_z': enc_z,
'dec_x': dec_x[:, :, 1:],
'dec_z': dec_z,
'dec_v': v,
}
return datetime_offset, data