-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgauss_rank_scaler.py
152 lines (137 loc) · 5.46 KB
/
gauss_rank_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import numpy as np
from joblib import Parallel, delayed
from scipy.interpolate import interp1d
from scipy.special import erf, erfinv
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils.validation import FLOAT_DTYPES, check_array, check_is_fitted
class GaussRankScaler(BaseEstimator, TransformerMixin):
"""Transform features by scaling each feature to a normal distribution.
Parameters
----------
epsilon : float, optional, default 1e-4
A small amount added to the lower bound or subtracted
from the upper bound. This value prevents infinite number
from occurring when applying the inverse error function.
copy : boolean, optional, default True
If False, try to avoid a copy and do inplace scaling instead.
This is not guaranteed to always work inplace; e.g. if the data is
not a NumPy array, a copy may still be returned.
n_jobs : int or None, optional, default None
Number of jobs to run in parallel.
``None`` means 1 and ``-1`` means using all processors.
interp_kind : str or int, optional, default 'linear'
Specifies the kind of interpolation as a string
('linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic',
'previous', 'next', where 'zero', 'slinear', 'quadratic' and 'cubic'
refer to a spline interpolation of zeroth, first, second or third
order; 'previous' and 'next' simply return the previous or next value
of the point) or as an integer specifying the order of the spline
interpolator to use.
interp_copy : bool, optional, default False
If True, the interpolation function makes internal copies of x and y.
If False, references to `x` and `y` are used.
Attributes
----------
interp_func_ : list
The interpolation function for each feature in the training set.
"""
def __init__(
self,
epsilon=1e-4,
copy=True,
n_jobs=None,
interp_kind="linear",
interp_copy=False,
):
self.epsilon = epsilon
self.copy = copy
self.interp_kind = interp_kind
self.interp_copy = interp_copy
self.fill_value = "extrapolate"
self.n_jobs = n_jobs
self.bound = 1.0 - self.epsilon
def fit(self, X, y=None):
"""Fit interpolation function to link rank with original data for future scaling
Parameters
----------
X : array-like, shape (n_samples, n_features)
The data used to fit interpolation function for later scaling along the features axis.
y
Ignored
"""
X = check_array(
X, copy=self.copy, estimator=self, dtype=FLOAT_DTYPES, force_all_finite=True
)
self.interp_func_ = Parallel(n_jobs=self.n_jobs)(
delayed(self._fit)(x) for x in X.T
)
return self
def _fit(self, x):
x = self.drop_duplicates(x)
rank = np.argsort(np.argsort(x))
factor = np.max(rank) / 2.0 * self.bound
scaled_rank = np.clip(rank / factor - self.bound, -self.bound, self.bound)
return interp1d(
x,
scaled_rank,
kind=self.interp_kind,
copy=self.interp_copy,
fill_value=self.fill_value,
)
def transform(self, X, copy=None):
"""Scale the data with the Gauss Rank algorithm
Parameters
----------
X : array-like, shape (n_samples, n_features)
The data used to scale along the features axis.
copy : bool, optional (default: None)
Copy the input X or not.
"""
check_is_fitted(self, "interp_func_")
copy = copy if copy is not None else self.copy
X = check_array(
X, copy=copy, estimator=self, dtype=FLOAT_DTYPES, force_all_finite=True
)
X = np.array(
Parallel(n_jobs=self.n_jobs)(
delayed(self._transform)(i, x) for i, x in enumerate(X.T)
)
).T
return X
def _transform(self, i, x):
clipped = np.clip(self.interp_func_[i](x), -self.bound, self.bound)
return erfinv(clipped)
def inverse_transform(self, X, copy=None):
"""Scale back the data to the original representation
Parameters
----------
X : array-like, shape [n_samples, n_features]
The data used to scale along the features axis.
copy : bool, optional (default: None)
Copy the input X or not.
"""
check_is_fitted(self, "interp_func_")
copy = copy if copy is not None else self.copy
X = check_array(
X, copy=copy, estimator=self, dtype=FLOAT_DTYPES, force_all_finite=True
)
X = np.array(
Parallel(n_jobs=self.n_jobs)(
delayed(self._inverse_transform)(i, x) for i, x in enumerate(X.T)
)
).T
return X
def _inverse_transform(self, i, x):
inv_interp_func = interp1d(
self.interp_func_[i].y,
self.interp_func_[i].x,
kind=self.interp_kind,
copy=self.interp_copy,
fill_value=self.fill_value,
)
return inv_interp_func(erf(x))
@staticmethod
def drop_duplicates(x):
is_unique = np.zeros_like(x, dtype=bool)
is_unique[np.unique(x, return_index=True)[1]] = True
return x[is_unique]