Skip to content

Unhelpful error message when running a classifier on a regression outcome #26270

@GaelVaroquaux

Description

@GaelVaroquaux

Describe the bug

When running a classifier on a regression outcome, we get a really unhelpful error message:

Steps/Code to Reproduce

In [2]: from sklearn import linear_model, datasets

In [3]: X, y = datasets.make_regression()

In [4]: model = linear_model.LogisticRegression()

In [5]: model.fit(X, y)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-6d10fe8973eb> in <module>
----> 1 model.fit(X, y)

~/.local/lib/python3.10/site-packages/sklearn/linear_model/_logistic.py in fit(self, X, y, sample_weight)
   1202             accept_large_sparse=solver not in ["liblinear", "sag", "saga"],
   1203         )
-> 1204         check_classification_targets(y)
   1205         self.classes_ = np.unique(y)
   1206 

~/.local/lib/python3.10/site-packages/sklearn/utils/multiclass.py in check_classification_targets(y)
    205         "multilabel-sequences",
    206     ]:
--> 207         raise ValueError("Unknown label type: %r" % y_type)
    208 
    209 

ValueError: Unknown label type: 'continuous'

Expected Results

An error message that puts the user on the right track.

Actual Results

We could add the following sentence to the error message "Maybe you are trying to fit a classifier (discrete classes) on a regression target (continuous values)".

Versions

Present on main.

In [6]: import sklearn; sklearn.show_versions()

System:
    python: 3.10.7 (main, Mar 10 2023, 10:47:39) [GCC 12.2.0]
executable: /usr/bin/python3
   machine: Linux-5.19.0-38-generic-x86_64-with-glibc2.36

Python dependencies:
      sklearn: 1.3.dev0
          pip: 22.2
   setuptools: 59.6.0
        numpy: 1.21.5
        scipy: 1.8.1
       Cython: 0.29.34
       pandas: 1.3.5
   matplotlib: 3.5.2
       joblib: 1.1.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
        version: 0.3.20
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8

       user_api: openmp
   internal_api: openmp
         prefix: libgomp
       filepath: /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0
        version: None
    num_threads: 8

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEasyWell-defined and straightforward way to resolve

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions