-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.py
83 lines (64 loc) · 2.16 KB
/
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
import numpy as np
from sklearn.preprocessing import MinMaxScaler
def pad(FTCP, pad_width):
'''
This function zero pads (to the end of) the FTCP representation along the second dimension
Parameters
----------
FTCP : numpy ndarray
FTCP representation as numpy ndarray.
pad_width : int
Number of values padded to the end of the second dimension.
Returns
-------
FTCP : numpy ndarray
Padded FTCP representation.
'''
FTCP = np.pad(FTCP, ((0, 0), (0, pad_width), (0, 0)), constant_values=0)
return FTCP
def minmax(FTCP):
'''
This function performs data normalization for FTCP representation along the second dimension
Parameters
----------
FTCP : numpy ndarray
FTCP representation as numpy ndarray.
Returns
-------
FTCP_normed : numpy ndarray
Normalized FTCP representation.
scaler : sklearn MinMaxScaler object
MinMaxScaler used for the normalization.
'''
dim0, dim1, dim2 = FTCP.shape
scaler = MinMaxScaler()
FTCP_ = np.transpose(FTCP, (1, 0, 2))
FTCP_ = FTCP_.reshape(dim1, dim0*dim2)
FTCP_ = scaler.fit_transform(FTCP_.T)
FTCP_ = FTCP_.T
FTCP_ = FTCP_.reshape(dim1, dim0, dim2)
FTCP_normed = np.transpose(FTCP_, (1, 0, 2))
return FTCP_normed, scaler
def inv_minmax(FTCP_normed, scaler):
'''
This function is the inverse of minmax,
which denormalize the FTCP representation along the second dimension
Parameters
----------
FTCP_normed : numpy ndarray
Normalized FTCP representation.
scaler : sklearn MinMaxScaler object
MinMaxScaler used for the normalization.
Returns
-------
FTCP : numpy ndarray
Denormalized FTCP representation as numpy ndarray.
'''
dim0, dim1, dim2 = FTCP_normed.shape
FTCP_ = np.transpose(FTCP_normed, (1, 0, 2))
FTCP_ = FTCP_.reshape(dim1, dim0*dim2)
FTCP_ = scaler.inverse_transform(FTCP_.T)
FTCP_ = FTCP_.T
FTCP_ = FTCP_.reshape(dim1, dim0, dim2)
FTCP = np.transpose(FTCP_, (1, 0, 2))
return FTCP