Skip to content

ENH Convert fitted attributes when the outermost fit returns - #2

Open
cakedev0 wants to merge 2 commits into
betatim:store-all-attrs-as-numpyfrom
cakedev0:fitted-attrs-outermost-fit
Open

cakedev0 wants to merge 2 commits into
betatim:store-all-attrs-as-numpyfrom
cakedev0:fitted-attrs-outermost-fit

Conversation

@cakedev0

@cakedev0 cakedev0 commented Oct 6, 2026

Copy link
Copy Markdown

Reference Issues/PRs

Proposed solution for the first point of my comment on scikit-learn#35053 (this PR targets its branch). Context in scikit-learn#34604.

What does this implement/fix? Explain your changes.

In scikit-learn#35053, attributes are converted to NumPy each time a _fit_context-decorated fit returns, including nested ones. This breaks code that reads fitted state back during a fit:

  • meta-estimators see the attributes of their sub-estimators as NumPy while they are still computing with X. For instance CalibratedClassifierCV(LogisticRegression(), method="temperature", ensemble=False).fit(X_torch, y_torch) raises Multiple namespaces for array inputs.
  • partial_fit and warm_start read back state that is now NumPy, while X is not.

This PR:

  • converts only when the outermost fit returns. _fit_context marks the fit as running in a thread-local, which sklearn.utils.parallel propagates to joblib workers (same as the config). The conversion then walks the sub-estimators once, each one converted according to its own opt-in. Constructor parameters are left untouched.
  • moves the running state of partial_fit to the namespace and device of X before updating it. I converted StandardScaler as an example: the state is (n_features,) so the extra transfers per batch are negligible compared to the batch itself.
  • adds check_array_api_incremental_fit: two partial_fit calls, or a warm_start=True refit, with array_api_strict on "device1". Forgetting to move the state then raises, while torch on CPU would silently accept some of these mixes.

I used a thread-local rather than a config key: users have no reason to set this flag, and config_context is public and checked by numpydoc.

Not covered here: calling cross_val_predict directly on an estimator fitted on torch still fails, because _enforce_prediction_order gets NumPy classes_ and torch predictions. That needs a fix in the code that reads classes_, which I think is a separate discussion. MinMaxScaler and GaussianNB are not converted yet, but the new check already runs on them.

AI usage disclosure

I used AI assistance for:

Any other comments?

I checked that the new tests catch what they are meant to catch: they fail if partial_fit doesn't move its state, if every fit converts (old behavior), or if the flag isn't propagated to joblib workers.

Array API common checks: 771 passed, 23 xfailed, 9 xpassed (StandardScaler and FeatureUnion([StandardScaler()]) no longer need the cross-namespace xfail).

🤖 Generated with Claude Code

Nested fits keep their arrays in the namespace and device of X, so the
enclosing fit can keep using them. partial_fit moves its running state to
X before updating it. StandardScaler is converted as the first incremental
estimator, and a new common check covers partial_fit and warm_start with
array_api_strict on a non-default device.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 6, 2026

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.

Comment thread sklearn/calibration.py


class _CalibratedClassifier:
class _CalibratedClassifier(BaseEstimator):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Note for reviewer: _CalibratedClassifier holds the classifiers fitted in CalibratedClassifierCV.fit. As a plain class, it stopped the walk in _fitted_attrs_as_numpy (which only follows objects with get_params), so with array API inputs their coef_ stayed in the namespace of X. move_estimator_to had the same blind spot: it left them as NumPy, and the moved copy shared them with the original.

@cakedev0
cakedev0 force-pushed the fitted-attrs-outermost-fit branch from cf62afe to 66eb066 Compare October 7, 2026 11:09
@cakedev0

cakedev0 commented Oct 7, 2026

Copy link
Copy Markdown
Author

I think this PR is a pretty good solution to avoid the regression CalibratedClassifierCV(LogisticRegression(), method="temperature", ensemble=False).fit(X_torch, y_torch)

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

❌ Linting issues

There was an issue running the linter job. Please update with upstream/main (link) and push the changes. If you already have done that, please send an empty commit with git commit --allow-empty and push the changes to trigger the CI.

Generated for commit: cf62afe. Link to the linter CI: here

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.

1 participant