/
util.py
66 lines (54 loc) · 1.55 KB
/
util.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
import numpy as np
import pickle
def create_batches(data, batch_length, normalized=True):
# split each stock data into windows of size batch_length
m, n = data.shape
data = data[:, :-(n % batch_length)]
# print(data.shape)
# print(m)
# print(n)
X = np.reshape(data, (m, n//batch_length, batch_length))
# print(X.shape)
# subtarct mean and divide by variance
y = X[:, 1:, -1]
mean = None
if normalized:
# subtract mean
mean = X.mean(axis=2, keepdims=True)
X -= mean
# print(X.shape)
# divide by standard dev
# std = X.std(axis=2, keepdims=True)
# std = np.repeat(std, batch_length, axis=2)
# print(std.shape)
# avoid divide by zero
# std[std == 0] = 1e-13
# X /= std
# print(X.shape)
# true label is the final price of the next batch
# mean = None
if normalized:
# subtract mean
# mean = X.mean(axis=2, keepdims=True)
# X -= mean
y -= mean[:,:-1,0]
# keep dimensions the same
y = np.reshape(y, (m, n//batch_length - 1, 1))
# last batch of X will not have a true label, remove it
X, y = X[:,:-1], y[:,:]
return X, y
def sliding_window(data, batch_length, overlap, plus=1):
m, n = data.shape
windows = n // (batch_length - overlap) - 2
X = np.ndarray((m, windows, batch_length))
y = np.ndarray((m, windows, 1))
for i in range(windows):
X[:,i,:] = data[:,i*batch_length-i*overlap:i*batch_length-i*overlap+batch_length]
y[:,i,0] = data[:,i*batch_length-i*overlap + batch_length + plus]
me = X[:,i,:].mean()
std = X[:,i,:].std()
X[:,i,:] -= me
y[:,i,0] -= me
X[:,i,:] /= std
y[:,i,0] /= std
return X, y