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

Vectorize apply_replacement #207

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/fklearn/training/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ def apply_replacements(df: pd.DataFrame,
Default value to replace when original value is not present in the `vec` dict for the feature

"""
column_categorizer = lambda col: df[col].apply(lambda x: (np.nan
if isinstance(x, float) and np.isnan(x)
else vec[col].get(x, replace_unseen)))
def column_categorizer(col: str) -> pd.Series:
return df[col].map(lambda x: vec[col].get(x, replace_unseen), na_action='ignore')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this faster than .apply? I don't know how map works under the hood, but if it implements a for loop in the backend for lambda function, than its just as bad as apply no?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I wasn't benchmarking against the original implementation, this takes about the same time (4.5s original, 3.8s this one). Do you have an example where the original is very slow? That may be a better case to benchmark.


categ_columns = {col: column_categorizer(col) for col in columns}
return df.assign(**categ_columns)

Expand Down