Skip to content

Instantly share code, notes, and snippets.

@cfperez
Created September 10, 2015 23:52
Show Gist options
  • Save cfperez/55ca1f61980ac94da03a to your computer and use it in GitHub Desktop.
Save cfperez/55ca1f61980ac94da03a to your computer and use it in GitHub Desktop.
LogTransformer to log scale features in sklearn Pipelines
from sklearn.base import BaseEstimator,TransformerMixin
class LogTransformer(BaseEstimator,TransformerMixin):
def __init__(self, constant=1, base='e'):
from numpy import log,log10
if base == 'e' or base == np.e:
self.log = log
elif base == '10' or base == 10:
self.log = log10
else:
base_log = np.log(base)
self.log = lambda x: np.log(x)/base_log
self.constant = constant
def fit(self, X, y=None):
return self
def transform(self, features):
return self.log(features+self.constant)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment