question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

__sklearn_clone__ protocol proposal

See original GitHub issue

I 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.

  1. 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__
  2. 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.
  3. Ability to customize cloning: https://github.com/scikit-learn/scikit-learn/issues/15371#issuecomment-546655096

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
thomasjpfancommented, Dec 21, 2021

To me the difference is developer API vs user API:

  1. __sklearn_clone__: Estimators developers need to think about implementing it (optionally)
  2. 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).

1reaction
jnothmancommented, Mar 19, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found