-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdata_helper.py
57 lines (42 loc) · 1.62 KB
/
data_helper.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
import numpy as np
import pandas as pd
def load_timeseries(filename, params):
"""Load time series dataset"""
series = pd.read_csv(filename, sep=',', header=0, index_col=0, squeeze=True)
data = series.values
adjusted_window = params['window_size']+ 1
# Split data into windows
raw = []
for index in range(len(data) - adjusted_window):
raw.append(data[index: index + adjusted_window])
# Normalize data
result = normalize_windows(raw)
raw = np.array(raw)
result = np.array(result)
# Split the input dataset into train and test
split_ratio = round(params['train_test_split'] * result.shape[0])
train = result[:int(split_ratio), :]
np.random.shuffle(train)
# x_train and y_train, for training
x_train = train[:, :-1]
y_train = train[:, -1]
# x_test and y_test, for testing
x_test = result[int(split_ratio):, :-1]
y_test = result[int(split_ratio):, -1]
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
x_test_raw = raw[int(split_ratio):, :-1]
y_test_raw = raw[int(split_ratio):, -1]
# Last window, for next time stamp prediction
last_raw = [data[-params['window_size']:]]
last = normalize_windows(last_raw)
last = np.array(last)
last = np.reshape(last, (last.shape[0], last.shape[1], 1))
return [x_train, y_train, x_test, y_test, x_test_raw, y_test_raw, last_raw, last]
def normalize_windows(window_data):
"""Normalize data"""
normalized_data = []
for window in window_data:
normalized_window = [((float(p) / float(window[0])) - 1) for p in window]
normalized_data.append(normalized_window)
return normalized_data