Skip to content

FIX: HTML representation with nested meta-estimator - #33889

Merged
glemaitre merged 18 commits into
scikit-learn:mainfrom
DeaMariaLeon:bug-meta
May 25, 2026
Merged

glemaitre merged 18 commits into
scikit-learn:mainfrom
DeaMariaLeon:bug-meta

Conversation

@DeaMariaLeon

@DeaMariaLeon DeaMariaLeon commented Apr 29, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #33887

What does this implement/fix? Explain your changes.

We need to check that the meta-estimator has .get_feature_names_out() before try to display it.
TunedThresholdClassifierCV is missing the the visualizer block_sk_visual_block()

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation: for the second test
  • Documentation (including examples)
  • Research and understanding

Any other comments?

@glemaitre

Copy link
Copy Markdown
Member

We need to check that the meta-estimator has .get_feature_names_out() before try to display it.

It is weird because for the ColumnTransformer it should expose a .get_feature_names_out(). But the error that is raised is a NotFittedError like estimator at this stage an unfitted ColumnTransformer.

estimator.get_feature_names_out(), is_fitted_css_class
)
try:
output_features = estimator.get_feature_names_out()

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.

If it is to test the availability of get_feature_names_out, then if hasattr(estimator, get_feature_names_out) would be better than catching whatever exception I think.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I had tried that I think and there were cases when it wasn't working. It happened on this PR: #33779

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.

Would it be possible to be more specific instead of generic Exception? I think you just need to catch NotFittedError and AttributeError.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I had changed it to a generic Exception here after the following suggestion because there were other kinds of errors. That's another PR but it's the same change.

Image

(I tried to link to the comment itself but the link didn't work!)

@DeaMariaLeon

Copy link
Copy Markdown
Member Author

We need to check that the meta-estimator has .get_feature_names_out() before try to display it.

It is weird because for the ColumnTransformer it should expose a .get_feature_names_out(). But the error that is raised is a NotFittedError like estimator at this stage an unfitted ColumnTransformer.

With the fix I do get:
Screenshot 2026-04-29 at 12 42 04

On main, it breakes.

@glemaitre

Copy link
Copy Markdown
Member

Yes but we get "0 features" (at the output of the ColumnTransformer) while the fitted ColumnTransformer should be able to return you the name of the features.

@DeaMariaLeon

Copy link
Copy Markdown
Member Author

Yes but we get "0 features" (at the output of the ColumnTransformer) while the fitted ColumnTransformer should be able to return you the name of the features.

I missed that!.. It maybe another issue - with the ColumnTransformer. I'll keep searching..

@DeaMariaLeon

Copy link
Copy Markdown
Member Author

On your example #33887, estimator: Pipeline doesn't show anything with the dropdown arrow. So,TunedThresholdClassifierCV is missing the the visualizer block (_sk_visual_block().

Besides that, I need to check the column names entering ColumnTransformer, they don't seem correct.

@DeaMariaLeon DeaMariaLeon moved this to In progress in Labs May 4, 2026
@DeaMariaLeon DeaMariaLeon added this to Labs May 4, 2026
@DeaMariaLeon
DeaMariaLeon marked this pull request as ready for review May 4, 2026 12:57
@DeaMariaLeon DeaMariaLeon moved this from In progress to Blocked in Labs May 4, 2026
@cakedev0

cakedev0 commented May 6, 2026

Copy link
Copy Markdown
Member

Hi @DeaMariaLeon, thanks for the fix. I took a first look and here is some feedback:

  1. Can you update the PR desc. to mention this part?

So,TunedThresholdClassifierCV is missing the the visualizer block(_sk_visual_block()

  1. Maybe you should fix de codecov? (I'm not sure what are the standards in sklearn regarding that but it's red so it probably needs to be fixed ^^)

  2. codex review found this: "When get_feature_names_out() fails on a fitted composite that still has outputs, such as a FeatureUnion with one transformer lacking get_feature_names_out, this fallback
    passes "" to _features_html and the diagram renders a misleading 0 features total. In the exception path, omit the total-features block instead of reporting zero features"

It looks like a legit finding. Here is a reproducer:

import numpy as np

from sklearn.decomposition import PCA
from sklearn.pipeline import FeatureUnion
from sklearn.preprocessing import FunctionTransformer

X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])

union = FeatureUnion(
    [
        ("pca", PCA(n_components=1)),
        ("identity", FunctionTransformer()),
    ]
)

Xt = union.fit_transform(X)
assert Xt.shape == (3, 3)
union
image

@DeaMariaLeon

Copy link
Copy Markdown
Member Author

codex review found this: "When get_feature_names_out() fails on a fitted composite that still has outputs, such as a FeatureUnion with one transformer lacking get_feature_names_out, this fallback
passes "" to _features_html and the diagram renders a misleading 0 features total. In the exception path, omit the total-features block instead of reporting zero features"

It looks like a legit finding. Here is a reproducer:

@cakedev0 Thanks a lot for looking into this PR and it's great that you found a bug with codex.
Just so you know: the solution it suggests is incorrect. It's not that simple because there would be some confusing lines.

@cakedev0

cakedev0 commented May 7, 2026

Copy link
Copy Markdown
Member

the solution it suggests is incorrect

Yeah I'm not too surprised by that ^^ But do you think it can be solved (with a simple enough solution)?

Otherwise I think we can merge this PR without fixing it and open an issue to document this bug. That would be ok with me.

@DeaMariaLeon

DeaMariaLeon commented May 7, 2026 •

Copy link
Copy Markdown
Member Author

@cakedev0 It turns out that if we set feature_names_out="one-to-one" to FeatureUnion like:

union = FeatureUnion(
    [
        ("pca", PCA(n_components=1)),
        ("identity", FunctionTransformer(feature_names_out="one-to-one")),
    ]
)

Then the output is correct:

Screenshot 2026-05-07 at 15 03 49

I'm not sure if this is considered a bug then.

EDIT: This comes from FunctionTransformer. From its feature_names_out docstrings please see "...The get_feature_names_out method is only defined if feature_names_out is not None."
And get_feature_names_out is used in estimator.py to display the feature names.

@cakedev0

cakedev0 commented May 7, 2026 •

Copy link
Copy Markdown
Member

Well for the snippet I shared, I'd say showing "0 features" below "1 feature" is a bit confusing, showing nothing would be better. That would align with what I think is the current policy: "if we don't know, we don't show". And "1 feature" + "we don't know" = "we don't know"). But maybe I'm missing some context, and that's not what we want to do here.

Anyway, as I said, I'm ok not solving that in this PR, we can continue the discussion in a separate issue once this is merged.

@DeaMariaLeon

Copy link
Copy Markdown
Member Author

Well for the snippet I shared, I'd say showing "0 features" below "1 feature" is a bit confusing

That's right. I got confused myself thinking that setting the feature_names_out, this problem was non-existent. - I guess I got too happy.

Now, to answer the question that if there is an easy fix: I need to think about it, there is not an obvious solution.
I'm happy to work on this on a different PR if you think that it's fine.

BTW, I'm trying to fix the codecov failure but I'm not there yet. (Just to tell you I didn't ignore your point 2 above).

@DeaMariaLeon DeaMariaLeon moved this from Blocked to In progress in Labs May 11, 2026
@DeaMariaLeon

Copy link
Copy Markdown
Member Author

At this point a solution could be like follows and improved on a separate PR.
As I said, the reason is that FeatureUnion needs feature_names_out="one-to-one":

Screenshot 2026-05-11 at 18 52 24

@cakedev0 I'm so sorry I accidentally left today's call when you were talking to me! .. But I figured out the Codecov issue and it's green. Thanks.

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

LGTM, thanks!

Two suggestions on the tests, but not blocking.

Comment on lines +177 to +186
class BrokenTransformer(TransformerMixin, BaseEstimator):
def fit(self, X, y=None):
self.n_features_in_ = X.shape[1]
return self

def get_feature_names_out(self, input_features=None):
raise RuntimeError("Simulated failure")

X = np.array([[1, 2], [3, 4]])
t = BrokenTransformer()

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 think you could test that with the FunctionTransformer?

Suggested change
class BrokenTransformer(TransformerMixin, BaseEstimator):
def fit(self, X, y=None):
self.n_features_in_ = X.shape[1]
return self
def get_feature_names_out(self, input_features=None):
raise RuntimeError("Simulated failure")
X = np.array([[1, 2], [3, 4]])
t = BrokenTransformer()
X = np.array([[1, 2], [3, 4]])
t = FunctionTransformer()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If I do that, the try/except for a singleblock in estimator.py are skipped so Codecov will complain again.

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.

Are you sure? t = FunctionTransformer() is a single block too I think.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it's a single block but is_fitted_css_class is empty because it's the first estimator I believe.
In any case it doesn't get into try/except, I tested it with the debugger.

Comment on lines +167 to +168
assert "1 feature" in html
assert "identity" in html

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.

Suggested change
assert "1 feature" in html
assert "identity" in html
assert "1 feature" in html
assert "0 feature" not in html # no misleading "0 feature" at the block level
assert "identity" in html

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea, thanks.
Just fyi, I had to add <div> 0 features</div> because 0 features was found somewhere else in the documentation area.

@cakedev0

Copy link
Copy Markdown
Member

At this point a solution could be like follows and improved on a separate PR.

I think it's good the way it is now, thanks 👍 no need for improvement IMO.

I'm so sorry I accidentally left today's call when you were talking to me!

No worries, yesterday end-of-the-day was very busy for me in the end, so I didn't have time to reach back after that.

@DeaMariaLeon
DeaMariaLeon requested a review from glemaitre May 13, 2026 08:05
@DeaMariaLeon DeaMariaLeon moved this from In progress to Blocked in Labs May 13, 2026

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

LGTM. Thanks @DeaMariaLeon

@glemaitre glemaitre added To backport PR merged in master that need a backport to a release branch defined based on the milestone. No Changelog Needed labels May 25, 2026
@glemaitre

Copy link
Copy Markdown
Member

We don't need any changelog as long we add in the next release (ping @jeremiedbb).

@glemaitre

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

Let's if we get somewhere with copilot.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label May 25, 2026
@jeremiedbb jeremiedbb added this to the 1.9 milestone May 25, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label May 25, 2026
@glemaitre
glemaitre merged commit 9c2a643 into scikit-learn:main May 25, 2026
33 checks passed
@github-project-automation github-project-automation Bot moved this from Blocked to Done in Labs May 25, 2026
@DeaMariaLeon

Copy link
Copy Markdown
Member Author

Thank you All!

@DeaMariaLeon
DeaMariaLeon deleted the bug-meta branch May 26, 2026 13:36
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
jeremiedbb pushed a commit that referenced this pull request Jun 2, 2026
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend module:utils No Changelog Needed To backport PR merged in master that need a backport to a release branch defined based on the milestone.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: HTML representation fails with nested meta-estimator

4 participants