__sklearn_clone__ protocol proposal
See original GitHub issueI propose a __sk_clone__
method that clone
calls (if the method exist). This way arbitrary objects can define how they want to be cloned. This is similar to mechanism behind __sk_is_fitted__
+ check_is_fitted
.
Purpose
There are PR that run into issues with clone
, and it would be nice to allow the estimator
to define how it wants to be cloned.
- Metadata routing: https://github.com/scikit-learn/scikit-learn/pull/21284 Needed to change
clone
to copy an private attribute that is configured outside of__init__
- Search spaces in estimator: https://github.com/scikit-learn/scikit-learn/pull/21784#issuecomment-979284821 the search space itself needs to be copied over when
clone
gets called. - Ability to customize cloning: https://github.com/scikit-learn/scikit-learn/issues/15371#issuecomment-546655096
Issue Analytics
- State:
- Created 2 years ago
- Comments:19 (19 by maintainers)
Top Results From Across the Web
sklearn.base.clone — scikit-learn 1.2.0 documentation
Construct a new unfitted estimator with the same parameters. Clone does a deep copy of the model in an estimator without actually copying...
Read more >Scikit-learn enhancement proposals - Read the Docs
This repository is for structured discussions about large modifications or additions to scikit-learn. The discussions must create an “enhancement proposal”, ...
Read more >Python scikit-learn: Cannot clone object... as the constructor ...
Without seeing your code, it's hard to tell exactly what goes wrong, but you are violating a scikit-learn API convention here.
Read more >Date-A-Scientist - Scikit-Learn Import error
... I get the error below. It's my first time working with it sklearn off platfor… ... line 80, in <module> from .base...
Read more >1980608 – sklearn import fails - Red Hat Bugzilla
base import clone File "/home/vitaly/.local/lib/python3.9/site-packages/sklearn/base.py", line 17, in <module> from .utils import _IS_32BIT File ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To me the difference is developer API vs user API:
__sklearn_clone__
: Estimators developers need to think about implementing it (optionally)clone
: User facing. This makes it harder to be optional since users would be expecting the method as a “scikit-learn compatible estimator”.To me, I like namespacing methods with double underscore (
__
) as it is developer facing and makes it explicit that it is interacting with scikit-learn’s functions. (__sklearn_is_fitted__
->check_is_fitted
).A draft SLEP is here: https://github.com/scikit-learn/enhancement_proposals/pull/67
To some extent @GaelVaroquaux’s objections still apply, but I think the core devs largely find the inflexibility of clone to be a limiting/frustrating.