8000 check_sparsify_coefficients in check_estimator does not respect binary_only tag · Issue #16798 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content
check_sparsify_coefficients in check_estimator does not respect binary_only tag #16798
@gramhagen

Description

@gramhagen

Describe the bug

When running check_estimator on an estimator with binary_only tag set to true the check_sparsify_coefficients test uses inputs with more than 2 classes.

Steps/Code to Reproduce

import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.utils.estimator_checks import check_estimator
from sklearn.utils.validation import check_array

class TestCheckSparsifyCoefBinary(LogisticRegression):
    def fit(self, X, y, sample_weight=None):
        num_classes = len(np.unique(check_array(y, ensure_2d=False)))
        if num_classes > 2:
            raise Exception(f"Found {num_classes} classes in binary estimator")
        return super().fit(X=X, y=y, sample_weight=sample_weight)
    
    def _more_tags(self):
        return dict(binary_only=True)

check_estimator(TestCheckSparsifyCoefBinary)

Expected Results

all checks pass

Actual Results

Traceback (most recent call last):
  File "test.py", line 33, in <module>
    check_estimator(TestCheckSparsifyBinary)
  File "/home/scgraham/repos/scikit-learn/sklearn/utils/estimator_checks.py", line 466, in check_estimator
    check(estimator)
  File "/home/scgraham/repos/scikit-learn/sklearn/utils/_testing.py", line 317, in wrapper
    return fn(*args, **kwargs)
  File "/home/scgraham/repos/scikit-learn/sklearn/utils/estimator_checks.py", line 2481, in check_sparsify_coefficients
    est.fit(X, y)
  File "test.py", line 12, in fit
    raise Exception(f"Found {num_classes} classes in binary estimator")
Exception: Found 3 classes in binary estimator

Versions

System:
    python: 3.8.2 (default, Mar 26 2020, 15:53:00)  [GCC 7.3.0]
executable: /home/scgraham/miniconda3/envs/sklearn/bin/python
   machine: Linux-4.4.0-18362-Microsoft-x86_64-with-glibc2.10

Python dependencies:
       pip: 20.0.2
setuptools: 46.1.1.post20200323
   sklearn: 0.23.dev0
     numpy: 1.18.1
     scipy: 1.4.1
    Cython: 0.29.15
    pandas: 1.0.3
matplotlib: None
    joblib: 0.14.1

Built with OpenMP: True

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0