-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathPreprocessing.py
More file actions
99 lines (77 loc) · 2.36 KB
/
Copy pathPreprocessing.py
File metadata and controls
99 lines (77 loc) · 2.36 KB
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
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
def preprocessing(series):
'''
MinMax Scaling of the raw time series
Args:
series: the raw time series
Returns:
scaled_series and scaler object
'''
series = np.array(series)
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(series.reshape(-1,1))
scaled_series = scaled.reshape((len(series),))
return scaled_series, scaler
def inverse_transform(series,scaler):
'''
Inverse transform of scales series
Args:
series: scaled series
scaler: scaler object
Returns:
unscaled series
'''
return scaler.inverse_transform(series.reshape(-1,1))
def getSeries(data,p):
'''
Splits a given time series proportionally
for training and testing purposes
Args:
data: numpy array or pandas series
containing the time series.
p: float value that defines the
proportion of the series used
for training.
Returns:
series: time series for training
y_test: time series for testing
n_test: number of timesteps
in the test series
'''
n = data.shape[0]
n_train = int(n * p)
n_test = n - n_train
x = np.arange(n)
index_train = x[:n_train]
index_test = x[n_train:]
series = data[index_train]
y_test = data[index_test]
return series, y_test, n_test
def getInputOutput(series, input_size):
'''
Transforms the time series into desired
shape to be able to pass to the network
Args:
series: the time series.
input_size: int that defines the length
of the input sequence to be
fed to the network
Returns:
X_train: input dataset
y_train: output values
X_test: the last available sequence
'''
series = np.array(series)
xlen = len(series)
xrows = xlen - input_size
X_train, y_train = [], []
for i in range(xrows):
j = i + input_size
a = series[i:j, np.newaxis]
X_train.append(a)
y_train.append(series[j])
X_train,y_train = np.array(X_train), np.array(y_train)
X_test = series[xrows:].reshape(1,input_size,1)
return X_train, y_train, X_test