-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
FIX handle VisibleDeprecationWarning in type_of_target and is_multilabel #18423
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
FIX handle VisibleDeprecationWarning in type_of_target and is_multilabel #18423
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @OlehKSS,
Thank you for your PR.
I think we should comment why y = np.asarray(y, dtype=object)
is required. Maybe something like:
# Specify dtype=object type for ragged arrays because
# NEP 34 disallow inferring these arrays from sequences
Moreover, we should include a TODO
note to fix this issue when changing the behaviour to raise a ValueError
instead.
99ae8b8
to
4301144
Compare
Hi, @alfaro96 |
Same as #18414, milestoned as 0.24 in order to update tests with respect to newer versions of numpy. |
sklearn/utils/multiclass.py
Outdated
@@ -137,8 +137,20 @@ def is_multilabel(y): | |||
>>> is_multilabel(np.array([[1, 0, 0]])) | |||
True | |||
""" | |||
import warnings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should import warnings on the top of the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
if hasattr(y, '__array__') or isinstance(y, Sequence): | ||
y = np.asarray(y) | ||
# TODO: Replace the warning context manager with a try-except statement | ||
# DeprecationWarning will be replaced by ValueError, see NEP 34 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# DeprecationWarning will be replaced by ValueError, see NEP 34 | |
# DeprecationWarning will be replaced by ValueError, see NEP 34 | |
# https://numpy.org/neps/nep-0034-infer-dtype-is-object.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
sklearn/utils/multiclass.py
Outdated
@@ -236,6 +248,8 @@ def type_of_target(y): | |||
>>> type_of_target(np.array([[0, 1], [1, 1]])) | |||
'multilabel-indicator' | |||
""" | |||
import warnings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
@@ -250,7 +264,16 @@ def type_of_target(y): | |||
if is_multilabel(y): | |||
return 'multilabel-indicator' | |||
|
|||
y = np.asarray(y) | |||
# TODO: Replace the warning context manager with a try-except statement | |||
# DeprecationWarning will be replaced by ValueError, see NEP 34 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# DeprecationWarning will be replaced by ValueError, see NEP 34 | |
# DeprecationWarning will be replaced by ValueError, see NEP 34 | |
# https://numpy.org/neps/nep-0034-infer-dtype-is-object.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise LGTM
@glemaitre I have updated this pull request according to your suggestions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR @OlehKSS !
LGTM
Reference Issues/PRs
Fix #18367
What does this implement/fix? Explain your changes.
Fix
numpy.VisibleDeprecationWarning
intype_of_target
,is_multilabel
.This warning was issued by
numpy.asarray
because of ragged/jagged sequences that were provided to this function. It was spotted in the following unit tests:Any other comments?
As far as I see, there are several ways to solve it:
numpy.array
instead. In that case, only users will see this warningnumpy.asarray(, dtype=object)
numpy.asarray
correctly.I decided to it as in the 3rd list item. Here, I assumed that it'd better let NumPy do this test and then catch that warning as an exception. Not sure whether it is the optimal way to do it. What do you think?