Skip to content

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

Merged
merged 3 commits into from
Oct 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions sklearn/utils/multiclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
from collections.abc import Sequence
from itertools import chain
import warnings

from scipy.sparse import issparse
from scipy.sparse.base import spmatrix
Expand Down Expand Up @@ -138,7 +139,17 @@ def is_multilabel(y):
True
"""
if hasattr(y, '__array__') or isinstance(y, Sequence):
y = np.asarray(y)
# DeprecationWarning will be replaced by ValueError, see NEP 34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

# https://numpy.org/neps/nep-0034-infer-dtype-is-object.html
with warnings.catch_warnings():
warnings.simplefilter('error', np.VisibleDeprecationWarning)
try:
y = np.asarray(y)
except np.VisibleDeprecationWarning:
# dtype=object should be provided explicitly for ragged arrays,
# see NEP 34
y = np.array(y, dtype=object)

if not (hasattr(y, "shape") and y.ndim == 2 and y.shape[1] > 1):
return False

Expand Down Expand Up @@ -250,7 +261,16 @@ def type_of_target(y):
if is_multilabel(y):
return 'multilabel-indicator'

y = np.asarray(y)
# DeprecationWarning will be replaced by ValueError, see NEP 34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

# https://numpy.org/neps/nep-0034-infer-dtype-is-object.html
with warnings.catch_warnings():
warnings.simplefilter('error', np.VisibleDeprecationWarning)
try:
y = np.asarray(y)
except np.VisibleDeprecationWarning:
# dtype=object should be provided explicitly for ragged arrays,
# see NEP 34
y = np.asarray(y, dtype=object)

# The old sequence of sequences format
try:
Expand Down