Skip to content

FIX Fix TargetEncoder error with metadata routing enabled and without groups - #35185

Merged
StefanieSenger merged 4 commits into
scikit-learn:mainfrom
lepennec:routing_target_encoder
Oct 21, 2026
Merged

StefanieSenger merged 4 commits into
scikit-learn:mainfrom
lepennec:routing_target_encoder

Conversation

@lepennec

@lepennec lepennec commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #35139

What does this implement/fix? Explain your changes.

With enable_metadata_routing=True, TargetEncoder.fit_transform(X, y) raises KeyError: 'groups' when it is called without groups.
This PR solves the issue with a one-line modification in ".../sklearn/preprocessing/_target_encoder.py", line 366.

           if params["groups"] is not None:

by

 `         if params.get("groups") is not None:`

Introduce yourself

I am a professor of applied math at École polytechnique, very close to the Turing build, so important to the scikit-learn community. I use scikit-learn mainly for teaching, as the ones doing the hard jobs are now my students. I am the original poster of the issue.

AI usage disclosure

I used AI assistance for:

  • Helping me prepare the PR, not the code itself.

@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

@lesteve lesteve added the Metadata Routing all issues related to metadata routing, slep006, sample props label Oct 21, 2026
@lesteve

lesteve commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Thanks a lot for the PR, cc @StefanieSenger for visibility.

@lesteve lesteve added this to Labs Oct 21, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Labs Oct 21, 2026
@lesteve

lesteve commented Oct 21, 2026

Copy link
Copy Markdown
Member

Side-question for @lepennec, I am curious about how people use metadata routing in the wild and what kind of use cases they have.

Can you explain in a few words what you use metadata routing for in your course? Maybe you have some public course materials that I can look at 🙏?

@lesteve lesteve changed the title TargetEncoder routing fix FIX Fix TargetEncoder error with metadata routing enabled and without groups Oct 21, 2026

@lesteve lesteve 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.

Looks good to me, thanks for the issue and PR!

I pushed a small tweak to make the test function name more explicit and add a reference to the original issue.

@lesteve lesteve added the Quick Review For PRs that are quick to review label Oct 21, 2026
@lepennec

Copy link
Copy Markdown
Contributor Author

Side-question for @lepennec, I am curious about how people use metadata routing in the wild and what kind of use cases they have.

Can you explain in a few words what you use metadata routing for in your course? Maybe you have some public course materials that I can look at 🙏?

I have a few labs in which I use an X-aware cost, i.e., a cost in which the loss depends on Y and f(X) as usual, but also on X itself. For instance, if you want to accept or not a loan, the amount of the loan is important. To use such a custom cost/metric in the scikit-learn cross-validation machinery, you need to route this amount. I have sent you an invitation to a WIP repository in which I use this. The first example is exercise 2 of Lab 1.

@StefanieSenger

Copy link
Copy Markdown
Member

I have a few labs in which I use an X-aware cost, i.e., a cost in which the loss depends on Y and f(X) as usual, but also on X itself. For instance, if you want to accept or not a loan, the amount of the loan is important. To use such a custom cost/metric in the scikit-learn cross-validation machinery, you need to route this amount. I have sent you an invitation to a WIP repository in which I use this. The first example is exercise 2 of Lab 1.

Thanks for sharing this. That's pretty much like @glemaitre's Post-tuning the decision threshold for cost-sensitive learning. Do you also use TunedThreshold* for this?

@StefanieSenger StefanieSenger 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.

This looks great. Thanks a lot for the report and the fix, @lepennec.
And congratulations to you first scikit-learn contribution.🥇

@StefanieSenger
StefanieSenger merged commit ffbf519 into scikit-learn:main Oct 21, 2026
43 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Labs Oct 21, 2026
@lepennec

Copy link
Copy Markdown
Contributor Author

I have a few labs in which I use an X-aware cost, i.e., a cost in which the loss depends on Y and f(X) as usual, but also on X itself. For instance, if you want to accept or not a loan, the amount of the loan is important. To use such a custom cost/metric in the scikit-learn cross-validation machinery, you need to route this amount. I have sent you an invitation to a WIP repository in which I use this. The first example is exercise 2 of Lab 1.

Thanks for sharing this. That's pretty much like @glemaitre's Post-tuning the decision threshold for cost-sensitive learning. Do you also use TunedThreshold* for this?

This is indeed very similar and could probably be traced back to the same source (we worked together on a continuous education program). I stick to the optimal Bayes predictor and thus do not further optimize the threshold; that is, however, a good idea of follow-up.

@glemaitre

Copy link
Copy Markdown
Member

Just passing by to say hello @lepennec :)

Nice to see that use cases where the work of the past releases has an impact. They are certainly advanced cases but real one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Metadata Routing all issues related to metadata routing, slep006, sample props module:preprocessing Quick Review For PRs that are quick to review size/S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TargetEncoder.fit_transform raises KeyError: 'groups' when metadata routing is enabled and no groups are passed

4 participants