-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdata_preprocessing.py
151 lines (119 loc) · 5.59 KB
/
data_preprocessing.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
import numpy as np
from sklearn.preprocessing import MinMaxScaler
class WindowGenerator():
def __init__(self, input_width, label_width, shift, df, label_columns=None):
# Work out the label column indices.
self.label_columns = label_columns
if label_columns is not None:
self.label_columns_indices = {name: i for i, name in
enumerate(label_columns)}
self.column_indices = {name: i for i, name in enumerate(df.columns)}
# Work out the window parameters.
self.input_width = input_width
self.label_width = label_width
self.shift = shift
self.total_window_size = input_width + shift
self.input_slice = slice(0, input_width)
self.input_indices = np.arange(self.total_window_size)[self.input_slice]
self.label_start = self.total_window_size - self.label_width
self.labels_slice = slice(self.label_start, None)
self.label_indices = np.arange(self.total_window_size)[self.labels_slice]
def split_window(self, features):
inputs = features[:, self.input_slice, :]
labels = features[:, self.labels_slice, :]
if self.label_columns is not None:
labels = np.stack(
[labels[:, :, self.column_indices[name]] for name in self.label_columns],
axis=-1)
return inputs, labels
def data_windowing(df, B, time_steps_in, time_steps_out, shift=None, label_columns=None, train_len=.8, val_len=.1, val_data=None, test_data=None):
if shift is None:
shift=time_steps_out
win = WindowGenerator(
input_width=time_steps_in,
label_width=time_steps_out,
shift=shift,
df=df,
label_columns=label_columns)
# Split the data, if the val and test set are not given
if val_data is None and test_data is None:
assert(train_len+val_len < 1)
N = df.shape[0]
train_data = np.array(df[:int(N*train_len)].values).astype(np.float32)
val_data = np.array(df[int(N*train_len):int(N*(train_len+val_len))].values).astype(np.float32)
test_data = np.array(df[int(N*(train_len+val_len)):].values).astype(np.float32)
else:
train_data = np.array(df.values).astype(np.float32)
val_data = np.array(val_data.values).astype(np.float32)
test_data = np.array(test_data.values).astype(np.float32)
# Initialize scaler
scaler = xy_scaler()
y_index = [df.columns.get_loc(col) for col in label_columns]
scaler.fit(train_data, y_index)
# Make windows
train_window = np.stack([ train_data[i:i+win.total_window_size] for i in range(0, train_data.shape[0] - win.total_window_size, time_steps_out)])
train_x, train_y = win.split_window(train_window)
train_y = train_y[:,:,0]
val_window = np.stack([ val_data[i:i+win.total_window_size] for i in range(0, val_data.shape[0] - win.total_window_size, time_steps_out)])
val_x, val_y = win.split_window(val_window)
val_y = val_y[:,:,0]
test_window = np.stack([ test_data[i:i+win.total_window_size] for i in range(0, test_data.shape[0] - win.total_window_size, time_steps_out)])
test_x, test_y = win.split_window(test_window)
test_y = test_y[:,:,0]
# Rescale data
train_x = scaler.transform_x(train_x)
train_y = scaler.transform_y(train_y)
val_x = scaler.transform_x(val_x)
val_y = scaler.transform_y(val_y)
test_x = scaler.transform_x(test_x)
test_y = scaler.transform_y(test_y)
# Make training batches
batch_len = int(np.floor(train_x.shape[0]/B))
to_del = time_steps_in//time_steps_out # make sure there are no overlapping windows across batches
train_data = []
for b in range(B):
train_data.append([train_x[b*batch_len:(b+1)*batch_len-to_del], train_y[b*batch_len:(b+1)*batch_len-to_del]])
return train_data, val_x, val_y, test_x, test_y, scaler
class xy_scaler:
"""
Transform X and Y data
"""
def __init__(self, Scaler=MinMaxScaler):
self.x_scaler = Scaler()
self.y_scaler = Scaler()
def fit(self, data, y_index):
"""
Parameters
----------
data : must have shape [time_steps, variables]
y_index : list specifying the target variables
"""
assert(len(data.shape) == 2)
assert(isinstance(y_index, list))
self.x_scaler.fit(data)
data_y = data[:, y_index]
self.y_scaler.fit(data_y)
def transform_x(self, data):
if len(data.shape) == 2:
data = data[..., None]
data_r = data.reshape(data.shape[0]*data.shape[1], -1)
data_r = self.x_scaler.transform(data_r)
data = data_r.reshape(data.shape[0], data.shape[1], -1)
return data
def transform_y(self, data):
data_r = data.reshape(data.shape[0]*data.shape[1], 1)
data_r = self.y_scaler.transform(data_r)
data = data_r.reshape(data.shape[0], data.shape[1])
return data
def inverse_transform_x(self, data):
if len(data.shape) == 2:
data = data[..., None]
data_r = data.reshape(data.shape[0]*data.shape[1], -1)
data_r = self.x_scaler.inverse_transform(data_r)
data = data_r.reshape(data.shape[0], data.shape[1], -1)
return data
def inverse_transform_y(self, data):
data_r = data.reshape(data.shape[0]*data.shape[1], 1)
data_r = self.y_scaler.inverse_transform(data_r)
data = data_r.reshape(data.shape[0], data.shape[1])
return data