Skip to content

Do not set n_features_in_ attribute before fit#6624

Merged
rapids-bot[bot] merged 7 commits intorapidsai:branch-25.06from
betatim:fix-n-features-in-attr
May 8, 2025
Merged

Do not set n_features_in_ attribute before fit#6624
rapids-bot[bot] merged 7 commits intorapidsai:branch-25.06from
betatim:fix-n-features-in-attr

Conversation

@betatim
Copy link
Copy Markdown
Member

@betatim betatim commented May 6, 2025

Fixes #6528

Scikit-learn compatible estimators should not set fitted attributes (those ending in a _) before they are fit. In the reported case the problem occurs because skore uses check_is_fitted from scikit-learn to decide if the estimator passed in is already fitted or not. This check looks for the presence of fitted attributes. This means the random forest model appeared to be fitted, when it wasn't.

What is the right label for a bug fix like this? While it does "break" existing user code that erroneously relies on this attribute always existing, that is buggy code as well. Hence I'd classify it as a bug fix. But it seems like I can only choose between non-breaking and breaking?

@betatim betatim requested a review from a team as a code owner May 6, 2025 08:06
@betatim betatim requested review from cjnolet and vyasr May 6, 2025 08:06
@github-actions github-actions Bot added the Cython / Python Cython or Python issue label May 6, 2025
@betatim betatim requested review from csadorf and jcrist and removed request for cjnolet and vyasr May 6, 2025 08:07
@betatim betatim added bug Something isn't working non-breaking Non-breaking change labels May 6, 2025
Copy link
Copy Markdown
Member

@jcrist jcrist left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! LGTM (once tests pass).

Coincidentally, I noticed this in the estimator proxy refactor PR where we wanted a simple way to check if an estimator has been fit before or not. Since all cuml estimators always had n_features_in_ set, using sklearn's check_is_fitted wasn't sufficient there either. There's a lot of other places in cuml where fitted attributes will return None if unset, so this PR alone isn't sufficient to resolve that, but it's a step in the right direction.

I'd agree that this is non-breaking. I doubt anyone is relying on the current behavior, and if they are their usage doesn't follow sklearn conventions so I'd consider it a bug fix.

betatim added 2 commits May 6, 2025 14:57
More estimators now pass `check_is_fitted`
@betatim
Copy link
Copy Markdown
Member Author

betatim commented May 8, 2025

/merge

@rapids-bot rapids-bot Bot merged commit 41e397d into rapidsai:branch-25.06 May 8, 2025
80 checks passed
@betatim betatim deleted the fix-n-features-in-attr branch May 8, 2025 11:15
Ofek-Haim pushed a commit to Ofek-Haim/cuml that referenced this pull request May 13, 2025
Fixes rapidsai#6528

Scikit-learn compatible estimators should not set fitted attributes (those ending in a `_`) before they are fit. In the reported case the problem occurs because `skore` uses `check_is_fitted` from scikit-learn to decide if the estimator passed in is already fitted or not. This check looks for the presence of fitted attributes. This means the random forest model appeared to be fitted, when it wasn't.

What is the right label for a bug fix like this? While it does "break" existing user code that erroneously relies on this attribute always existing, that is buggy code as well. Hence I'd classify it as a bug fix. But it seems like I can only choose between non-breaking and breaking?

Authors:
  - Tim Head (https://github.com/betatim)
  - Simon Adorf (https://github.com/csadorf)

Approvers:
  - Victor Lafargue (https://github.com/viclafargue)
  - Jim Crist-Harif (https://github.com/jcrist)

URL: rapidsai#6624
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Cython / Python Cython or Python issue non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] AttributeError when using cuml.accel with skore EstimatorReport

4 participants