Skip to content

PRF Encoders: Fast path for narwhals Series - #9

Closed
cakedev0 wants to merge 23 commits into
mainfrom
refactor/encoder_validation
Closed

cakedev0 wants to merge 23 commits into
mainfrom
refactor/encoder_validation

Conversation

@cakedev0

@cakedev0 cakedev0 commented Jul 21, 2026 •

Copy link
Copy Markdown
Owner

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_X keeps non-numeric dataframe columns as narwhals Series instead of forcing everything through check_array into numpy object arrays.
  • _unique/_encode use narwhals' native .value_counts()/.replace_strict() on them instead of pure-Python set/dict loops, which is where the speed-up below comes from.
  • Extended _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/OrdinalEncoder no longer thread an ensure_all_finite argument through _fit/_transform/_check_X: it was always called with "allow-nan" at every call site, so _check_X just does that internally now.

AI usage disclosure

I used AI assistance for:

  • Code review
  • Test/benchmark generation
  • Code generation (but most is hand-written, and it was closely guided)

Benchmarks

EDIT: Those benchmarks are misleading:

  • they use 100k rows, which hides an big overhead of narwhals/pandas-through-narwhals (big enough to dominate up to ~30k rows).
  • they don't go through the "object" dtype path, which is the fastest path on main

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):

fit transform fit_transform
pandas/string 3.3x 2.9x 3.0x
pandas/categorical 5.1x 4.5x 5.1x
polars/string 2.1x 3.4x 3.6x
polars/categorical 4.2x 3.3x 4.1x

@cakedev0

Copy link
Copy Markdown
Owner Author

cc @lorentzenchr

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.

@cakedev0 cakedev0 changed the title Refactor/encoder validation PRF Encoders: Fast path for narwhals Series Jul 29, 2026
@ogrisel

ogrisel commented Aug 5, 2026

Copy link
Copy Markdown

scikit-learn#34452 was merged to main. I think you can open the PR against scikit-learn's main to move the review there.

@ogrisel

ogrisel commented Aug 6, 2026

Copy link
Copy Markdown

Could you close and reopen a PR to target the upstream repo to move the review there?

@cakedev0

cakedev0 commented Aug 6, 2026

Copy link
Copy Markdown
Owner Author

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.

@cakedev0
cakedev0 changed the base branch from refactor/encode to main August 6, 2026 14:40
@cakedev0

cakedev0 commented Aug 7, 2026 •

Copy link
Copy Markdown
Owner Author

So... This PRs has several issues.

First my earlier benchmarks are misleading:

  • they use 100k rows, which hides an big overhead of narwhals/pandas-through-narwhals (big enough to dominate up to ~30k rows).
  • they don't go through the "object" dtype path, which is the fastest path on main.
  • they only benchmark a count-computing encoder, which favors this branch

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.

@cakedev0 cakedev0 closed this Aug 7, 2026
@lorentzenchr

Copy link
Copy Markdown

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 unique.

@cakedev0

cakedev0 commented Aug 7, 2026 •

Copy link
Copy Markdown
Owner Author

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 narwhalspolars, the trade-off is much cleaner than for pandas (for the narwhals path): except for <2000 samples (where it's not too bad), it's always good, and it gets very (very) good for big datasets.

I need to test with pyarrow.

@cakedev0

cakedev0 commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

Because for narwhals

*Because for polars

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

Successfully merging this pull request may close these issues.

3 participants