Skip to content

Test - sklearn/utils/estimator_checks.py::check_supervised_y_2d does not clone estimator #10977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
omtinez opened this issue Apr 14, 2018 · 2 comments

Comments

@omtinez
Copy link

omtinez commented Apr 14, 2018

Description

Test function check_supervised_y_2d reuses an already fitted estimator which could leave potential side effects. For example, if an estimator uses a flag to only set random state once, the call to set_random_state(estimator) will have no effect and that can lead to a false negative (failure) in this test.

Steps/Code to Reproduce

import numpy
import warnings
from sklearn.base import BaseEstimator
from sklearn.exceptions import DataConversionWarning
from sklearn.utils.estimator_checks import check_supervised_y_2d

class RoundRobinClassifier(BaseEstimator):
    def __init__(self, random_state=0):
        self.random_state = random_state
        
    def fit(self, X, y):
        if len(y.shape[1:]) >= 1:
            warnings.warn(
                'A column-vector y was passed when a 1d array was expected',
                DataConversionWarning)
        if not hasattr(self, 'idx'):
            self.idx = int(self.random_state)
        self.classes_ = numpy.unique(y)
        return self
    
    def predict(self, X):
        pred = []
        for i, _ in enumerate(X):
            pred.append(self.classes_[self.idx % len(self.classes_)])
            self.idx = (self.idx + 1) % len(self.classes_)
        return numpy.asarray(pred)

classif = RoundRobinClassifier()
check_supervised_y_2d(classif.__class__.__name__, classif)

Expected Results

Test passes, since the required warning is thrown.

Actual Results

AssertionError:
Not equal to tolerance rtol=1e-07, atol=0

(mismatch 100.0%)
x: array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0])
y: array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1])

Versions

Platform info:

  • Darwin-17.4.0-x86_64-i386-64bit
  • Python 3.6.3 |Intel Corporation| (default, Oct 16 2017, 10:30:26)
  • [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]
  • NumPy 1.14.2
  • SciPy 1.0.1

Tested in the following versions:

  • Scikit-Learn 0.19.1
  • Scikit-Learn 0.20.dev0
@jnothman
Copy link
Member

jnothman commented Apr 15, 2018 via email

@cmarmo
Copy link
Contributor

cmarmo commented Apr 23, 2021

The test pass with the current main version (c88c89c).
I'm closing this one.

@cmarmo cmarmo closed this as completed Apr 23, 2021
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 a pull request may close this issue.

3 participants