-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathtwosigma_winsorizer.py
More file actions
26 lines (20 loc) · 944 Bytes
/
Copy pathtwosigma_winsorizer.py
File metadata and controls
26 lines (20 loc) · 944 Bytes
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
"""Winsorizes (truncates) univariate outliers outside of two standard deviations from the mean."""
from h2oaicore.transformer_utils import CustomTransformer
import datatable as dt
import numpy as np
class MyTwoSigmaWinsorizer(CustomTransformer):
_unsupervised = True
_testing_can_skip_failure = False # ensure tested as if shouldn't fail
@staticmethod
def get_default_properties():
return dict(col_type="numeric", min_cols=1, max_cols=1, relative_importance=1)
def fit_transform(self, X: dt.Frame, y: np.array = None):
self.mean = X.mean1()
self.sd = X.sd1()
return self.transform(X)
def transform(self, X: dt.Frame):
X = dt.Frame(X)
if self.sd is not None and self.mean is not None:
X[self.mean - 2 * self.sd > dt.f[0], float] = self.mean - 2 * self.sd
X[self.mean + 2 * self.sd < dt.f[0], float] = self.mean + 2 * self.sd
return X