forked from h2oai/driverlessai-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_transformer.py
32 lines (26 loc) · 1.1 KB
/
log_transformer.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
"""Converts numbers to their Logarithm"""
from h2oaicore.transformer_utils import CustomTransformer
import datatable as dt
import numpy as np
class MyLogTransformer(CustomTransformer):
@staticmethod
def get_default_properties():
return dict(col_type="numeric", min_cols=1, max_cols=3, relative_importance=1)
def fit_transform(self, X: dt.Frame, y: np.array = None):
return self.transform(X)
def transform(self, X: dt.Frame):
return X[:, [dt.log(dt.f[i]) for i in range(X.ncols)]]
# optional
_mojo = True
from h2oaicore.mojo import MojoWriter, MojoFrame
def to_mojo(self, mojo: MojoWriter, iframe: MojoFrame):
from h2oaicore.mojo import MojoColumn, MojoFrame
from h2oaicore.mojo_transformers import MjT_Log
xnew = iframe[self.input_feature_names]
oframe = MojoFrame()
for col in xnew:
ocol = MojoColumn(name=col.name, dtype=np.float64)
ocol_frame = MojoFrame(columns=[ocol])
mojo += MjT_Log(iframe=MojoFrame(columns=[col]), oframe=ocol_frame)
oframe += ocol
return oframe