-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutsf_data_utils.py
246 lines (164 loc) · 8.47 KB
/
utsf_data_utils.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
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
class DataUtil(object):
# Beijing PM2.5 and GefCom Electricity Price
# This class contains data specific information.
# It does the following:
# - Read data from file
# - Normalise it
# - Split it into train, dev (validation) and test
# - Create X and Y for each of the 3 sets (train, dev, test) according to the following:
# Every sample (x, y) shall be created as follows:
# - x --> window number of values
# - y --> one value that is at horizon in the future i.e. that is horizon away past the last value of x
# This way X and Y will have the following dimensions:
# - X [number of samples, window, number of un/multivariate time series]
# - Y [number of samples, number of un/multivariate time series]
def __init__(self, config):
self.config = config
self.train_rate = self.config.train_rate
self.valid_rate = self.config.valid_rate
self.w = self.config.window
self.h = self.config.horizon
self.rowdata = self.load_dataset()
self.n, self.m = self.rowdata.shape
self.data, self.scaler = self.normalize_data(self.rowdata)
def normalize_data(self, data, SACLED_FEATURE_RANGE=(0, 1)):
scaler = MinMaxScaler(feature_range=SACLED_FEATURE_RANGE)
data = scaler.fit_transform(data)
return data, scaler
def inverse_transform(self, y_scaled):
y_scaler = self.scaler
real_y = y_scaler.inverse_transform(y_scaled)
return real_y
def split_data(self):
train_range = range(self.w + self.h - 1, int(self.train_rate * self.n))
valid_range = range(int(self.train_rate * self.n), int((self.train_rate + self.valid_rate) * self.n))
test_range = range(int((self.train_rate + self.valid_rate) * self.n), self.n)
train = self.get_data(train_range)
valid = self.get_data(valid_range)
test = self.get_data(test_range)
return train, valid, test
def get_data(self, rng):
n = len(rng)
X = np.zeros((n, self.w, self.m))
Y = np.zeros((n, 1))
for i in range(n):
end = rng[i] - self.h + 1
start = end - self.w
X[i,:,:] = self.data[start : end, :]
Y[i,:] = self.data[rng[i], -1]
return [X, Y]
def get_dataset(self, ds_type='Train'):
if ds_type == 'Train':
ds = self.train
elif ds_type == 'Valid':
ds = self.valid
elif ds_type == 'Test':
ds = self.test
else:
raise RuntimeError("Unknown dataset type['Train', 'Valid', 'Test']:", ds_type)
return ds[0], ds[1]
class BJPMDataset(DataUtil):
def __init__(self, data_filename, config):
self.data_filename = data_filename
DataUtil.__init__(self, config)
print('Prepare DataSet - PJPM2_5 ...')
self.train, self.valid, self.test = self.split_data()
print('[Train Set] | X-shape: {} --- y-shape: {}'.format(self.train[0].shape, self.train[1].shape))
print('[Valid Set] | X-shape: {} --- y-shape: {}'.format(self.valid[0].shape, self.valid[1].shape))
print('[Test Set] | X-shape: {} --- y-shape: {}'.format(self.test[0].shape, self.test[1].shape))
def load_dataset(self):
df = pd.read_csv(self.data_filename, usecols=[5,6,7,8,9,10,11,12], skiprows=range(1, 25))
df.dropna(inplace=True)
df['cbwd'] = LabelEncoder().fit_transform(df['cbwd'])
pm2_5 = df['pm2.5']
df.drop(labels=['pm2.5'], axis=1, inplace=True)
df.insert(len(df.columns), 'pm2.5', pm2_5)
rowdata = df.values.astype('float32')
return rowdata
class GefComPriceDataset(DataUtil):
def __init__(self, data_filename, config):
self.data_filename = data_filename
DataUtil.__init__(self, config)
print('Prepare DataSet - GefCom2014_Price ...')
self.train, self.valid, self.test = self.split_data()
print('[Train Set] | X-shape: {} --- y-shape: {}'.format(self.train[0].shape, self.train[1].shape))
print('[Valid Set] | X-shape: {} --- y-shape: {}'.format(self.valid[0].shape, self.valid[1].shape))
print('[Test Set] | X-shape: {} --- y-shape: {}'.format(self.test[0].shape, self.test[1].shape))
def load_dataset(self):
df = pd.read_csv(self.data_filename)
df.dropna(inplace=True)
rowdata = df.values.astype('float32')
return rowdata
class PollutionDataset(DataUtil):
def __init__(self, data_filename, config):
self.data_filename = data_filename
DataUtil.__init__(self, config)
print('Prepare DataSet - Pollution ...')
self.train, self.valid, self.test = self.split_data()
print('[Train Set] | X-shape: {} --- y-shape: {}'.format(self.train[0].shape, self.train[1].shape))
print('[Valid Set] | X-shape: {} --- y-shape: {}'.format(self.valid[0].shape, self.valid[1].shape))
print('[Test Set] | X-shape: {} --- y-shape: {}'.format(self.test[0].shape, self.test[1].shape))
def load_dataset(self):
df = pd.read_csv(self.data_filename)
df.dropna(inplace=True)
rowdata = df.values.astype('float32')
return rowdata
class BikeDataset(DataUtil):
def __init__(self, data_filename, config):
self.data_filename = data_filename
DataUtil.__init__(self, config)
print('Prepare DataSet - Bike ...')
self.train, self.valid, self.test = self.split_data()
print('[Train Set] | X-shape: {} --- y-shape: {}'.format(self.train[0].shape, self.train[1].shape))
print('[Valid Set] | X-shape: {} --- y-shape: {}'.format(self.valid[0].shape, self.valid[1].shape))
print('[Test Set] | X-shape: {} --- y-shape: {}'.format(self.test[0].shape, self.test[1].shape))
def load_dataset(self):
df = pd.read_csv(self.data_filename)
df.dropna(inplace=True)
rowdata = df.values.astype('float32')
return rowdata
class NSW2013Dataset(DataUtil):
def __init__(self, data_filename, config):
self.data_filename = data_filename
DataUtil.__init__(self, config)
print('Prepare DataSet - NSW2013 ...')
self.train, self.valid, self.test = self.split_data()
print('[Train Set] | X-shape: {} --- y-shape: {}'.format(self.train[0].shape, self.train[1].shape))
print('[Valid Set] | X-shape: {} --- y-shape: {}'.format(self.valid[0].shape, self.valid[1].shape))
print('[Test Set] | X-shape: {} --- y-shape: {}'.format(self.test[0].shape, self.test[1].shape))
def load_dataset(self):
df = pd.read_csv(self.data_filename)
df.dropna(inplace=True)
rowdata = df.values.astype('float32')
return rowdata
class NSW2016Dataset(DataUtil):
def __init__(self, data_filename, config):
self.data_filename = data_filename
DataUtil.__init__(self, config)
print('Prepare DataSet - NSW2016 ...')
self.train, self.valid, self.test = self.split_data()
print('[Train Set] | X-shape: {} --- y-shape: {}'.format(self.train[0].shape, self.train[1].shape))
print('[Valid Set] | X-shape: {} --- y-shape: {}'.format(self.valid[0].shape, self.valid[1].shape))
print('[Test Set] | X-shape: {} --- y-shape: {}'.format(self.test[0].shape, self.test[1].shape))
def load_dataset(self):
df = pd.read_csv(self.data_filename)
df.dropna(inplace=True)
rowdata = df.values.astype('float32')
return rowdata
class TAS2016Dataset(DataUtil):
def __init__(self, data_filename, config):
self.data_filename = data_filename
DataUtil.__init__(self, config)
print('Prepare DataSet - TAS2016 ...')
self.train, self.valid, self.test = self.split_data()
print('[Train Set] | X-shape: {} --- y-shape: {}'.format(self.train[0].shape, self.train[1].shape))
print('[Valid Set] | X-shape: {} --- y-shape: {}'.format(self.valid[0].shape, self.valid[1].shape))
print('[Test Set] | X-shape: {} --- y-shape: {}'.format(self.test[0].shape, self.test[1].shape))
def load_dataset(self):
df = pd.read_csv(self.data_filename)
df.dropna(inplace=True)
rowdata = df.values.astype('float32')
return rowdata