Repository navigation
Conversation
|
This is the alternative version of scikit-learn#34393 (fast encoders). For now I keep it on my fork, because it also contains the changes from scikit-learn#34452. Compared to PR scikit-learn#34393, it’s slower for the categorical pandas case (250 vs 150ms for the fit_transform), but PR scikit-learn#34393 was only optimizing this case and was as slow as main for other cases. It’s also a much smaller change in terms of logic: the path for Series works roughly as the existing paths (numerical & objects), but using Series primitives. |
|
scikit-learn#34452 was merged to main. I think you can open the PR against scikit-learn's main to move the review there. |
|
Could you close and reopen a PR to target the upstream repo to move the review there? |
|
Sorry, I redid some benchmarks this morning and I noticed a slowdown on small/medium datasets. Let me dig a bit/try finding mitigation strategies before opening the official PR. |
|
So... This PRs has several issues. First my earlier benchmarks are misleading:
In reality this PR causes performance regression in many cases for small-medium datasets (up to 30k samples). This is really hard to fix, and a good threshold would be hard to find (from 5k for some cases, and maybe up to 50k for the most extreme cases). It also introduces a regression for py-arrow tables. |
|
I wouldn't give up. I still think narwhals can improve performance of categorical encoding by using the information of the dtype directly instead of calling |
|
I tried really hard to make the narwhals path work (i.e. no/almost no regimes with performance regression), but I couldn't 😞 I did a pandas path instead: scikit-learn#34678 I'll still do the narwhals path for polars and pyarrow, but once this bug is fixed: narwhals-dev/narwhals#3841 (which should not be in too much time, I got a rely quick answer on the issue). Because for I need to test with pyarrow. |
*Because for polars |
Reference Issues/PRs
Alternative version of scikit-learn#34393
What does this implement/fix? Explain your changes.
This adds a fast path for non-numeric dataframe columns (pandas/polars/pyarrow), and makes encoders work with it:
_BaseEncoder._check_Xkeeps non-numeric dataframe columns as narwhals Series instead of forcing everything throughcheck_arrayinto numpy object arrays._unique/_encodeuse narwhals' native.value_counts()/.replace_strict()on them instead of pure-Pythonset/dict loops, which is where the speed-up below comes from._narwhals_indexing/_safe_indexing(sklearn/utils/_indexing.py) to return a narwhals object back when given one, so I could conveniently use it in this PR.TargetEncoder/OneHotEncoder/OrdinalEncoderno longer thread anensure_all_finiteargument through_fit/_transform/_check_X: it was always called with"allow-nan"at every call site, so_check_Xjust does that internally now.AI usage disclosure
I used AI assistance for:
Benchmarks
EDIT: Those benchmarks are misleading:
Using a variation of benchmark from scikit-learn#34394 on
OrdinalEncoder,kddcup09_churn, 100k rows, on main and on this branch.Speed-up (main / branch):