Created
September 10, 2015 23:52
-
-
Save cfperez/55ca1f61980ac94da03a to your computer and use it in GitHub Desktop.
LogTransformer to log scale features in sklearn Pipelines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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