Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird Error #8

Open
lauragustafson opened this issue Apr 30, 2018 · 1 comment
Open

Weird Error #8

lauragustafson opened this issue Apr 30, 2018 · 1 comment

Comments

@lauragustafson
Copy link

lauragustafson commented Apr 30, 2018

from fhub_core import Feature
from fhub_transformers.missing import LagImputer
from sklearn.preprocessing import Imputer, StandardScaler, MinMaxScaler
from fhub_transformers import NamedFramer, SimpleFunctionTransformer
feature = Feature(input=input, transformer=transformer)

input = 'precipitation_amt_mm'
transformer = [
    LagImputer(groupby_kwargs={'level': 'city'}),
    Imputer(),
    SimpleFunctionTransformer(
        lambda ser: ser if ser > 0 else ser
    ),
]
mapper = feature.as_dataframe_mapper()
mapper.fit(X_df, y_df)
mapper.transform(X_df)

gives error:

AttributeError                            Traceback (most recent call last)
<ipython-input-83-0de258be511f> in <module>()
----> 1 mapper.transform(X_df)

~/miniconda3/envs/dengue_preiction/lib/python3.6/site-packages/sklearn_pandas/dataframe_mapper.py in transform(self, X)
    277             if transformers is not None:
    278                 with add_column_names_to_exception(columns):
--> 279                     Xt = transformers.transform(Xt)
    280             extracted.append(_handle_feature(Xt))
    281 

~/miniconda3/envs/dengue_preiction/lib/python3.6/site-packages/fhub_core/feature.py in wrapped(X, y, **kwargs)
     48                     return func(convert(X), y=convert(y), **kwargs)
     49                 else:
---> 50                     return func(convert(X), **kwargs)
     51             except catch as e:
     52                 formatted_exc = indent(traceback.format_exc(), n=8)

~/miniconda3/envs/dengue_preiction/lib/python3.6/site-packages/fhub_core/feature.py in transform(self, X, **transform_kwargs)
     22     def transform(self, X, **transform_kwargs):
     23         _transform = make_robust_to_tabular_types(super().transform)
---> 24         return _transform(X, **transform_kwargs)
     25 
     26 

~/miniconda3/envs/dengue_preiction/lib/python3.6/site-packages/fhub_core/feature.py in wrapped(X, y, **kwargs)
     48                     return func(convert(X), y=convert(y), **kwargs)
     49                 else:
---> 50                     return func(convert(X), **kwargs)
     51             except catch as e:
     52                 formatted_exc = indent(traceback.format_exc(), n=8)

~/miniconda3/envs/dengue_preiction/lib/python3.6/site-packages/sklearn/pipeline.py in _transform(self, X)
    424         for name, transform in self.steps:
    425             if transform is not None:
--> 426                 Xt = transform.transform(Xt)
    427         return Xt
    428 

~/miniconda3/envs/dengue_preiction/lib/python3.6/site-packages/fhub_transformers/base.py in transform(self, X, **transform_kwargs)
     36     def transform(self, X, **transform_kwargs):
     37         if self.groupby_kwargs:
---> 38             call = X.sort_index().groupby(**self.groupby_kwargs).apply
     39         else:
     40             call = X.sort_index().pipe

AttributeError: precipitation_amt_mm: 'numpy.ndarray' object has no attribute 'sort_index'

Not sure if this is just me using this incorrectly.

@micahjsmith
Copy link
Owner

micahjsmith commented Apr 30, 2018

The input to each component in the transformer pipeline is a tabular type (pd.DataFrame, pd.Series, np.array, etc.). So in lambda ser, the parameter ser is the entire series, whereas it looks like you want to apply a lambda to every row. So this syntax wouldn't work.

You could use

def f(ser):
    ser.apply(lambda row: row if row > 0 else row)

and pass f to SimpleFunctionTransformer.

Regardless of whether your transformation has any meaning, see above xref about debugging output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants