Repository: scikit-learn/scikit-learn
Pull request: Include informative error message when checking classification target is of a non-regression type
<!--
Thanks for contributing a pull request! Please ensure you have taken a look at
the contribution guidelines: https://github.com/scikit-learn/scikit-learn/blob/main/CONTRIBUTING.md
-->

#### Reference Issues/PRs
<!--
Example: Fixes #1234. See also #3456.
Please use keywords (e.g., Fixes) to create link to the issues or pull requests
you resolved, so that they will automatically be closed when your pull request
is merged. See https://github.com/blog/1506-closing-issues-via-pull-requests
-->
Fixes #26270

#### What does this implement/fix? Explain your changes.
This includes an informative error message when checking  a classification target in `multiclass.py` . This provides a helpful error message to the user when a classifier is fit on a regression type/outcome.

#### Any other comments?
The error message will offer more insight for a user to get back on track.

<!--
Please be aware that we are a loose team of volunteers so patience is
necessary; assistance handling other issues is very welcome. We value
all user contributions, no matter how minor they are. If we are slow to
review, either the pull request needs some benchmarking, tinkering,
convincing, etc. or more likely the reviewers are simply busy. In either
case, we ask for your understanding during the review process.
For more information, see our FAQ on this topic:
http://scikit-learn.org/dev/faq.html#why-is-my-pull-request-not-getting-any-attention.

Thanks for contributing!
-->


Problem statement:
Unhelpful error message when running a classifier on a regression outcome
### Describe the bug

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


### Steps/Code to Reproduce

```python
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

```shell
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
```
```

Hints from issue discussion:
I will take this up and open a PR ASAP.

Commit message: Include informative error message

Patch to review:
diff --git a/sklearn/utils/multiclass.py b/sklearn/utils/multiclass.py
index 02725f8986948..6b05c750d8331 100644
--- a/sklearn/utils/multiclass.py
+++ b/sklearn/utils/multiclass.py
@@ -215,7 +215,11 @@ def check_classification_targets(y):
         "multilabel-indicator",
         "multilabel-sequences",
     ]:
-        raise ValueError("Unknown label type: %r" % y_type)
+        raise ValueError(
+            "Unknown label type: %r"
+            "Maybe you are trying to fit a classifier for discrete classes"
+            "on a regression target with continuous values" % y_type
+        )
 
 
 def type_of_target(y, input_name=""):


Code review comments:
diff_hunk: @@ -215,7 +215,11 @@ def check_classification_targets(y):
         "multilabel-indicator",
         "multilabel-sequences",
     ]:
-        raise ValueError("Unknown label type: %r" % y_type)
+        raise ValueError(
+            "Unknown label type: %r"
+            "Maybe you are trying to fit a classifier for discrete classes"
+            "on a regression target with continuous values" % y_type
path: sklearn/utils/multiclass.py
text: @user1:
```suggestion
            f"Unknown label type: {y_type}. Maybe you are trying to fit a "
            "classifier which expects discrete classes as output on a "
            "regression target having continuous values."
```