Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

adrinjalali
Copy link
Member

Fixes #22857

There is an issue I hadn't realized: comparing ndarrays raises a ValueError and not a RuntimeError. So if the estimator does a copy of an ndarray input argument, clone would fail with a different error. I'm not sure how to proceed.

Also pinging @amueller (you wrote the test which fails with this test I think)

@jnothman
Copy link
Member

Yes, I did predict this would happen, but decided not to mention it on the issue... I don't think it's a major concern, since we're only dealing here with badly behaved constructors, or else we could define a helper to suppress the ValueError.

@jnothman
Copy link
Member

Or is your concern that the implementation might intentionally copy the array parameter, or realise a parameter stored in a C struct as an array? If so, let's use a helper.

@jnothman
Copy link
Member

I have a similar helper here

def _are_candidates_equal(dict1, dict2):
"""Test equality between candidate dicts
Falls back to testing identity where equality is unsupported, as it is
for arrays.
"""
try:
return bool(dict1 == dict2)
except ValueError:
pass
if dict1.keys() != dict2.keys():
return False
for k, v1 in dict1.items():
v2 = dict2[k]
if v1 is not v2:
return False
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
if v1 != v2:
return False
except ValueError:
return False
return True

@adrinjalali
Copy link
Member Author

Slightly confused by this bit:

        if v1 is not v2:
            return False
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            try:
                if v1 != v2:
                    return False
            except ValueError:
                return False

What should the input be for the code to reach if v1 != v2:? My intuition says I would write this as:

        if v1 is v2:
            continue
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            try:
                if v1 != v2:
                    return False
            except ValueError:
                return False

@jnothman
Copy link
Member

jnothman commented Apr 1, 2022

I agree that doesn't look right... Makes me wonder about my test coverage.

pass

# Register global_random_seed plugin if it is not already registered
if not config.pluginmanager.hasplugin("sklearn.tests.random_seed"):
Copy link
Member

Choose a reason for hiding this comment

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

Is this a merge error? This change is already on main.

Comment on lines -79 to -80
def __init__(self, a=np.array([0])):
self.a = a.copy()
Copy link
Member

Choose a reason for hiding this comment

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

Looking at the git history: 680ab51 and the original issue: #5540, the proposed feature in this PR was deprecated and removed in a older version of scikit-learn.

@jeremiedbb
Copy link
Member

Is this PR still relevant since we now have #24568 ?

@adrinjalali adrinjalali closed this Mar 3, 2023
@adrinjalali adrinjalali deleted the clone branch March 3, 2023 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RuntimeError: "Cannot clone object ..." when cloning an estimator that copies parameters in either __init__ or get_params
4 participants