-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaler.py
77 lines (64 loc) · 2.67 KB
/
scaler.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
import numpy as np
class Scaler:
def __init__(self, X: np.ndarray = None): # n * F matrix
self.is_init = False
if not X is None:
self.init_transform(X)
self.is_init = True
def init_transform(self, X: np.ndarray):
raise NotImplementedError
def transform(self, X: np.ndarray):
raise NotImplementedError
def transform_rev(self, X: np.ndarray):
raise NotImplementedError
class MaxMinScaler(Scaler):
def init_transform(self, X: np.ndarray):
self.xmax = np.max(X, axis=0)
self.xmin = np.min(X, axis=0)
def transform(self, X: np.ndarray):
if not self.is_init:
self.init_transform(X)
self.is_init = True
return (X - self.xmin) / (self.xmax - self.xmin)
def transform_rev(self, X: np.ndarray):
if not self.is_init:
raise ValueError('Scaler is not initialized')
return X * (self.xmax - self.xmin) + self.xmin
class SignMaxMinScaler(Scaler):
def init_transform(self, X: np.ndarray):
xmax = np.max(X, axis=0)
xmin = np.min(X, axis=0)
self.bounds = np.stack((xmax, xmin), axis=0)
self.map_bounds = np.ones_like(self.bounds[0, :])
for i in range(self.bounds.shape[1]):
if self.bounds[0, i] <= 0:
self.map_bounds[i] = -1
elif self.bounds[1, i] >= 0:
self.map_bounds[i] = 1
else:
self.map_bounds[i] = 0
def transform(self, X: np.ndarray):
X = X.copy()
if not self.is_init:
self.init_transform(X)
self.is_init = True
for i in range(X.shape[1]):
if self.map_bounds[i] == 1:
X[:, i] = (X[:, i] - self.bounds[1, i]) / (self.bounds[0, i] - self.bounds[1, i])
elif self.map_bounds[i] == -1:
X[:, i] = (X[:, i] - self.bounds[0, i]) / (self.bounds[1, i] - self.bounds[0, i])
else:
X[:, i] = 2*(X[:, i] - self.bounds[1, i]) / (self.bounds[0, i] - self.bounds[1, i]) - 1
return X
def transform_rev(self, X: np.ndarray):
X = X.copy()
if not self.is_init:
raise ValueError('Scaler is not initialized')
for i in range(X.shape[1]):
if self.map_bounds[i] == 1:
X[:, i] = X[:, i] * (self.bounds[0, i] - self.bounds[1, i]) + self.bounds[1, i]
elif self.map_bounds[i] == -1:
X[:, i] = X[:, i] * (self.bounds[1, i] - self.bounds[0, i]) + self.bounds[0, i]
else:
X[:, i] = (X[:, i] + 1) * (self.bounds[0, i] - self.bounds[1, i]) / 2 + self.bounds[1, i]
return X