Skip to content

Commit

Permalink
Merge pull request scikit-learn-contrib#177 from ragrawal/astransformer
Browse files Browse the repository at this point in the history
ActAsTransformer
  • Loading branch information
dukebody authored Dec 1, 2018
2 parents 0024cf7 + c83a2de commit f3cf7f6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions sklearn_pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .cross_validation import cross_val_score, GridSearchCV, RandomizedSearchCV # NOQA
from .categorical_imputer import CategoricalImputer # NOQA
from .features_generator import gen_features # NOQA
from .act_as_transformer import ActAsTransformer # NOQA
21 changes: 21 additions & 0 deletions sklearn_pandas/act_as_transformer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sklearn.base import BaseEstimator, TransformerMixin
import numpy as np


class ActAsTransformer(BaseEstimator, TransformerMixin):
"""
Use this class to convert a random function into a
transformer.
"""

def __init__(self, func):
self.__func = func

def fit(self, x, y=None):
return self

def transform(self, x):
return np.vectorize(self.__func)(x)

def __call__(self, *args, **kwargs):
return self.__func(*args, **kwargs)
15 changes: 15 additions & 0 deletions tests/test_dataframe_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from sklearn_pandas import DataFrameMapper, cross_val_score
from sklearn_pandas.dataframe_mapper import _handle_feature, _build_transformer
from sklearn_pandas.pipeline import TransformerPipeline
from sklearn_pandas import ActAsTransformer


class MockXTransformer(object):
Expand Down Expand Up @@ -950,3 +951,17 @@ def test_heterogeneous_output_types_input_df():
dft = M.fit_transform(df)
assert dft['feat1'].dtype == np.dtype('int64')
assert dft['feat2'].dtype == np.dtype('float64')


def test_actastransformer():
"""
Test whether random transformations works
"""
df = pd.DataFrame({
'feat1': [10, 100],
})
M = DataFrameMapper([
('feat1', ActAsTransformer(np.log10))
], input_df=True, df_out=True, default=None)
dft = M.fit_transform(df)
assert_array_equal([1., 2.], dft['feat1'].values)

0 comments on commit f3cf7f6

Please sign in to comment.