forked from luyiyun/NormAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.py
36 lines (30 loc) · 1.14 KB
/
transfer.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
import pandas as pd
import sklearn.preprocessing as skp
class Normalization:
Scalers = {
'standard': skp.StandardScaler,
'minmax': skp.MinMaxScaler,
'maxabs': skp.MaxAbsScaler,
'robust': skp.RobustScaler
}
def __init__(self, ty='standard', **kwargs):
self.ty = ty
self.scaler = __class__.Scalers[ty](**kwargs)
self.fit_ind = False
def __call__(self, x, y):
xindex, xcolumns = x.index, x.columns # scaler的结果是ndarray,但需要df
values = x.values
if self.fit_ind:
x = self.scaler.transform(values)
else:
x = self.scaler.fit_transform(values)
self.fit_ind = True
return pd.DataFrame(x, index=xindex, columns=xcolumns), y
def reset(self):
if self.fit_ind:
self.scaler = __class__.Scalers[self.ty]
self.fit_ind = False
def inverse_transform(self, x, y):
xindex, xcolumns = x.index, x.columns # scaler的结果是ndarray,但需要df
res = self.scaler.inverse_transform(x.values)
return pd.DataFrame(res, index=xindex, columns=xcolumns), y