Skip to content

PRF: Faster encoders thanks to F-order layout - #34392

Merged
OmarManzoor merged 6 commits into
scikit-learn:mainfrom
cakedev0:opt/encoders/f-order
Jul 2, 2026
Merged

OmarManzoor merged 6 commits into
scikit-learn:mainfrom
cakedev0:opt/encoders/f-order

Conversation

@cakedev0

@cakedev0 cakedev0 commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Inspired by the flamegraph of this script which then led me to profile encoders and found out that for integers input, some of the bottlenecks were in simple numpy operations that should be very fast, and then I realized it was doing col-wise things on C-order arrays.

I used benchmark from this PR #34394 for measurements below.

What does this implement/fix? Explain your changes.

This optimizes memory layout used by encoders (TargetEncoder, OneHotEncoder, OrdinalEncoder) by using F-order output/intermediate arrays instead of default C-order.

AI usage disclosure

I used AI assistance for: Adapting the Cython code to use X_indices. And that's all.

Benchmarks

I ran python benchmarks/bench_encoding.py -f integer (added in PR #34394) on main and this branch.

TLDR:

  • for one-hot & ordinal: transform is almost 2x faster, fit_transform is ~30% faster.
  • for target transformer: fit, transform and fit_transform are 2-3x faster.

Note: when inside a ColumnTransformer(..., remainder="passthrough"), speed-up stays the same expect for one-hot where the cost of sparse h-stack is dominant and makes the speed-up less visible.

Main:

onehot fit 67.6 ms
onehot transform 139.8 ms
onehot fit_transform 205.9 ms
ordinal fit 64.6 ms
ordinal transform 110.7 ms
ordinal fit_transform 172.7 ms
target fit 174.5 ms
target transform 209.6 ms
target fit_transform 599.4 ms

This branch:

onehot fit 67.4 ms
onehot transform 81.8 ms
onehot fit_transform 142.8 ms
ordinal fit 65.8 ms
ordinal transform 58.5 ms
ordinal fit_transform 120.7 ms
target fit 96.1 ms
target transform 62.2 ms
target fit_transform 239.0 ms

Note: the speed-up for ordinal/onehot.fit_transform is much more important when I compare locally the new fast path for pandas/polars categorical with or without the optimization made in this PR. In this case, it's around 2x.

@ogrisel

ogrisel commented Jul 1, 2026

Copy link
Copy Markdown
Member

Please add a changelog entry for this PR.

@cakedev0
cakedev0 marked this pull request as ready for review July 1, 2026 14:25

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I ran the benchmarks and can reproduce similar speed-ups on Apple M4 under macOS.

The code change does not add too much extra complexity and the speed-up is significant enough to justify this change.

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thank you @cakedev0

@OmarManzoor
OmarManzoor merged commit a62d492 into scikit-learn:main Jul 2, 2026
34 checks passed
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants