Skip to content

ENH Display fitted attributes in HTML representation - #31442

Closed
DeaMariaLeon wants to merge 92 commits into
scikit-learn:mainfrom
DeaMariaLeon:fitted-att
Closed

DeaMariaLeon wants to merge 92 commits into
scikit-learn:mainfrom
DeaMariaLeon:fitted-att

Conversation

@DeaMariaLeon

@DeaMariaLeon DeaMariaLeon commented May 28, 2025 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Towards #26595

What does this implement/fix? Explain your changes.

Display the (public) fitted attributes with their type. Shows also shape for array-valued attributes.

Any other comments?

Continuation of work started on this PR: #30763

cc @glemaitre
edit:

Screenshot 2026-01-02 at 14 52 12

@DeaMariaLeon
DeaMariaLeon marked this pull request as ready for review June 6, 2025 08:32
@DeaMariaLeon DeaMariaLeon changed the title WIP: ENH Display fitted attributes in HTML representation ENH Display fitted attributes in HTML representation Jun 6, 2025
Comment thread sklearn/base.py Resolved
Comment thread sklearn/base.py Resolved
Comment thread sklearn/tests/test_base.py Resolved

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

A few more comments

Comment thread sklearn/compose/_column_transformer.py Resolved
Comment thread sklearn/compose/_column_transformer.py Resolved
Comment thread sklearn/compose/_column_transformer.py Outdated
Comment on lines +1234 to +1243
if hasattr(self, "_remainder"):
remainder_columns = self._remainder[2]
if (
hasattr(self, "feature_names_in_")
and remainder_columns
and not all(isinstance(col, str) for col in remainder_columns)
):
remainder_columns = self.feature_names_in_[
remainder_columns
].tolist()

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.

Is there a case where _remainder is not defined ? I've the impression that it's always defined when fitted

Suggested change
if hasattr(self, "_remainder"):
remainder_columns = self._remainder[2]
if (
hasattr(self, "feature_names_in_")
and remainder_columns
and not all(isinstance(col, str) for col in remainder_columns)
):
remainder_columns = self.feature_names_in_[
remainder_columns
].tolist()
remainder_columns = self._remainder[2]
if (
hasattr(self, "feature_names_in_")
and remainder_columns
and not all(isinstance(col, str) for col in remainder_columns)
):
remainder_columns = self.feature_names_in_[remainder_columns].tolist()

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 have to find the example where it was showing "unwanted boxes" if I didn't add the part of drop.

Besides that, on the original code there is the drop check (line 1227 of _column_transformer.py on main) :

if isinstance(self.remainder, str) and self.remainder == "drop":

I think that if I don't add it, it always shows remainder or something. And because the first if changed, I had to write this differently.

Let me check.

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'm not talking about drop, but about the if hasattr(self, "_remainder"): condition. Since this block of code is in a if branch where the columntransformer is fitter, I think that this check is not necessary because it will always evaluate to True, unless I missed something.

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.

True, thanks.

Comment thread sklearn/base.py Outdated

def _get_fitted_attr_html(self, doc_link=""):
"""Get fitted attributes of the estimator."""

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.

nitpick: I'd move this method next to _get_params_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.

Done

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 wonder if I should add more to the docstring.

@DeaMariaLeon
DeaMariaLeon removed the request for review from glemaitre February 23, 2026 13:58
Comment on lines +1233 to 1241
# We can find remainder and its column only when it's fitted

remainder_columns = self._remainder[2]
if (
hasattr(self, "feature_names_in_")
and remainder_columns
and not all(isinstance(col, str) for col in remainder_columns)
):
remainder_columns = self.feature_names_in_[remainder_columns].tolist()

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 just noticed that remainder_columns is not used. A good side effect of relying on the fitted transformers :)

Suggested change
# We can find remainder and its column only when it's fitted
remainder_columns = self._remainder[2]
if (
hasattr(self, "feature_names_in_")
and remainder_columns
and not all(isinstance(col, str) for col in remainder_columns)
):
remainder_columns = self.feature_names_in_[remainder_columns].tolist()

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.

why didn't I think of it? 😐

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 found what it was used for: when we fit on a dataframe we get the feature names and list the ones corresponding to the remainder instead of integer indices.

So we need to get that back in and replace the remainder in the transformers list (using chain like it previously was).

Sorry for the back and forth.

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 need to take notes of what I find while it's still fresh in my head: my issue is my own workflow.

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.

We could also expand the inline comments in the code to make that more explicit.

@ogrisel ogrisel Feb 25, 2026 •

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 found what it was used for: when we fit on a dataframe we get the feature names and list the ones corresponding to the remainder instead of integer indices.

It's weird that the tests do not break as a result. But I suppose that such a test will only be included once we merge #31937 (check that pandas column names appear in the HTML diagram of a fitted pipeline).

@jeremiedbb jeremiedbb Feb 25, 2026 •

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.

Well it's just the columns selected by the remainder, so the input features, not the output features. We could definitely already add such a test.

@ogrisel, I think that you should continue your review in #33399, which proposes an alternative table layout that several persons prefer over this one

Comment thread sklearn/compose/_column_transformer.py Resolved
@antoinebaker

antoinebaker commented Feb 24, 2026 •

Copy link
Copy Markdown
Member

I don't find the current table display for fitted attributes user-friendly:
Capture d’écran 2026-02-24 à 10 58 52

It displays too much information and not in a compact readable way.

I get the need to have more information for array like fitted attributes (their shape at least, maybe dtype, maybe container, maybe some summary statistics). But for these we could do it in a follow up PR ?

Option 1 (minimal)

I think a good way forward for this PR would be to have a minimal layout (for now):

  • same layout as Parameters
  • simple list of name, (pretty printed) value

I like the idea to have a harmonized display for Parameters / Fitted Attributes as they play a similar role (list of input stuff, list of output stuff). We can get the shape of an array attribute through the docstring when hovering. We can guess the type from the value (but we may need to improve the prettry printing in a follow up PR).

WDTY @jeremiedbb @DeaMariaLeon @glemaitre ?

@antoinebaker

antoinebaker commented Feb 24, 2026 •

Copy link
Copy Markdown
Member

Option 2 (tree view list)

For a more advanced layout (in a follow up PR ), I think a tree view list of the fitted attributes could be nice ? Each fitted attribute name is a togglable list item, if we want more information we click on the fitted attribute name. We can customize the displayed information based on the attribute type.

@antoinebaker

antoinebaker commented Feb 24, 2026 •

Copy link
Copy Markdown
Member

Option 3 (simplified type)

If we really want to display the "type" along the name / value, I would use a simplified type. We can keep int, float, dict etc and use array(*shape) for arrays [for example array(2, 10) for a n array of shape (2,10)].

@jeremiedbb

Copy link
Copy Markdown
Member

It displays too much information and not in a compact readable way.

I expressed the same concern a couple of times. We have a lot of information and the fact that it's not the same kind of info for all the attributes results in many empty cells which doesn't render very nicely.

I think a tree view list of the fitted attributes could be nice ? Each fitted attribute name is a togglable list item, if we want more information we click on the fitted attribute name.

It may mess up with the fact that names are already clickable to send you the online doc. It would mean adding an extra button to expand the details, which itself may not interact well with the tooltip that shows up on hovering over the name, and overwhelm the table.

If we really want to display the "type" along the name / value, I would use a simplified type. We can keep int, float, dict etc and use array(*shape) for arrays [for example array(2, 10) for a n array of shape (2,10)].

I thought about that alternative but wanted to also show the dtype. I figured that it would be too long, or at least as long as the current version. And I didn't find a nice way to represent both the shape and the dtype.
But it might better render than the current empty cells, so I'd like to see a try for that alternative. @DeaMariaLeon maybe you can make a new branch from this one and open a separate PR so that we can compare both alternatives ?

@DeaMariaLeon

Copy link
Copy Markdown
Member Author

I expressed the same concern a couple of times.

I think I was expecting a definitive instruction to remove stuff, plus I was busy implementing the rest. I didn't mean to ignore your concerns.

maybe you can make a new branch from this one

Ok

@jeremiedbb

Copy link
Copy Markdown
Member

I think I was expecting a definitive instruction to remove stuff, plus I was busy implementing the rest. I didn't mean to ignore your concerns.

don't worry, they were not targeted at you. I was hoping to get the attention of another core dev :)

@antoinebaker

Copy link
Copy Markdown
Member

Indeed sorry @DeaMariaLeon that the instructions aren't clear yet on the desired display of the Fitted attributes, it seems to change as the PR evolves. We can maybe wait on some feedback of the core devs before moving on ?

IMO, the minimal option 1 seems the most promising for a fast merge. We just replicate what has been done for Parameters (without the copy button) and "voila". I feel it's good enough for a first version. @jeremiedbb what's your feeling about the minimal option 1 ?

@DeaMariaLeon

Copy link
Copy Markdown
Member Author

If you guys want to look at #33399 - like I wrote there, I'm not sure that I understood correctly.

Regarding Option 1: Hover effects don't work on tablets or "touch-based" devices, do they??

@DeaMariaLeon

DeaMariaLeon commented Feb 24, 2026 •

Copy link
Copy Markdown
Member Author

@antoinebaker - I did the PR before seeing you last comment, that's why I didn't wait. 😬

EDIT: and don't worry, no hurt feelings. :)

@antoinebaker

antoinebaker commented Feb 24, 2026 •

Copy link
Copy Markdown
Member

Oh that's great you already implemented option 3 ! Would you mind trying out option 1 in yet another draft PR ?

Regarding Option 1: Hover effects don't work on tablets or "touch-based" devices, do they??

I don't know in general, but the hover effect works on my laptop (mac os) trackpad.

@jeremiedbb

Copy link
Copy Markdown
Member

IMO, the minimal option 1 seems the most promising for a fast merge. We just replicate what has been done for Parameters (without the copy button) and "voila". I feel it's good enough for a first version. @jeremiedbb what's your feeling about the minimal option 1 ?

That's a pragmatic solution indeed. However, I like a lot the alternative in #33399 and I think it could be pretty much as fast to review as the minimal one.

@ogrisel

ogrisel commented Feb 25, 2026 •

Copy link
Copy Markdown
Member

I don't know in general, but the hover effect works on my laptop (mac os) trackpad.

I think @DeaMariaLeon refers to mobile devices with a touch screen only (no mouse / trackpad). In this case it's not possible to display extra info when hovering. It's only possible to display things when clicking/tapping on stuff.

@ogrisel

ogrisel commented Feb 25, 2026

Copy link
Copy Markdown
Member

I think we have a consensus that #33399 is better. Let's close this one.

@ogrisel ogrisel closed this Feb 25, 2026
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Feb 25, 2026
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.

5 participants