Skip to content

Commit

Permalink
LogTransformer raises error if transform is called before it's fitted
Browse files Browse the repository at this point in the history
  • Loading branch information
knstmrd committed Sep 21, 2018
1 parent 6d44f6c commit a6e9c65
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/transforms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
import pandas as pd
from .exceptions import DataProcessorError
from sklearn.exceptions import NotFittedError


class LogTransformer(BaseEstimator, TransformerMixin):
Expand All @@ -9,13 +11,18 @@ class LogTransformer(BaseEstimator, TransformerMixin):
replace the values in the column with a logarithm: X[i, col] = np.log(1 + X[i, col] - min(X[:, col]))
"""
def __init__(self, threshold=1e5):
self.fitted = False
self.threshold = threshold

def __str__(self):
return 'LogTransformer(threshold={})'.format(self.threshold)

def _reset(self):
if hasattr(self, 'columns_'):
del self.columns_
del self.column_names_
del self.min_vals_
self.fitted = False

def fit(self, X):
self._reset()
Expand Down Expand Up @@ -50,15 +57,20 @@ def fit(self, X):
if type(X) == pd.DataFrame:
self.column_names_ = [X.columns[i] for i in self.columns_]

self.fitted = True

return self

def transform(self, X):
if type(X) == pd.DataFrame:
Y = X.values
else:
Y = X
if self.fitted:
if type(X) == pd.DataFrame:
Y = X.values
else:
Y = X

for i, col in enumerate(self.columns_):
Y[:, col] = np.log1p(Y[:, col] - self.min_vals_[i])
for i, col in enumerate(self.columns_):
Y[:, col] = np.log1p(Y[:, col] - self.min_vals_[i])

return X
return X
else:
raise NotFittedError('This LogTransformer has not been fitted yet')

0 comments on commit a6e9c65

Please sign in to comment.