Skip to content

Clearer exception on legacy multi-label data representation - #13184

Merged
qinhanmin2014 merged 3 commits into
scikit-learn:masterfrom
peterjc:patch-1
Feb 19, 2019
Merged

qinhanmin2014 merged 3 commits into
scikit-learn:masterfrom
peterjc:patch-1

Conversation

@peterjc

@peterjc peterjc commented Feb 18, 2019

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

See new exception added in #4919

What does this implement/fix? Explain your changes.

Motivating example:

>>> from sklearn.metrics import hamming_loss
>>> expt_list = [["A"], ["B"], ["C"], ["D"], ["E"], ["A", "E"]]
>>> pred_list = [["A"], ["B"], ["B", "C"], ["C"], ["E"], ["A"]]
>>> hamming_loss(expt_list, pred_list)
Traceback (most recent call last):
...
ValueError: You appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead.

I would have found it helpful if the exception mentioned the new helper class MultiLabelBinarizer, which could be used here as follows:

>>> from sklearn.metrics import hamming_loss
>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> expt_list = [["A"], ["B"], ["C"], ["D"], ["E"], ["A", "E"]]
>>> pred_list = [["A"], ["B"], ["B", "C"], ["C"], ["E"], ["A"]]
>>> relabel = MultiLabelBinarizer(["A", "B", "C", "D", "E"])  # Could be more classes
>>> expt = relabel.fit_transform(expt_list)
>>> pred = relabel.fit_transform(pred_list)
>>> hamming_loss(expt, pred)
0.13333333333333333

Any other comments?

Including the helper class name makes it far easier to find documentation on the new way to do this, e.g.

https://scikit-learn.org/stable/modules/multiclass.html#multilabel-classification-format

Motivating example:

```python
>>> from sklearn.metrics import hamming_loss
>>> expt_list = [["A"], ["B"], ["C"], ["D"], ["E"], ["A", "E"]]
>>> pred_list = [["A"], ["B"], ["B", "C"], ["C"], ["E"], ["A"]]
>>> hamming_loss(expt_list, pred_list)
Traceback (most recent call last):
...
ValueError: You appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead.
```

I would have found it helpful if the exception mentioned the new helper class ``MultiLabelBinarizer``, used as follows:

```python
>>> from sklearn.metrics import hamming_loss
>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> expt_list = [["A"], ["B"], ["C"], ["D"], ["E"], ["A", "E"]]
>>> pred_list = [["A"], ["B"], ["B", "C"], ["C"], ["E"], ["A"]]
>>> relabel = MultiLabelBinarizer(["A", "B", "C", "D", "E"])  # 5 classes
>>> expt = relabel.fit_transform(expt_list)
>>> pred = relabel.fit_transform(pred_list)
>>> hamming_loss(expt, pred)
0.13333333333333333
```
@jnothman

Copy link
Copy Markdown
Member

Hmmmm why is CI failing? I think you need to update tests that check for this message

@qinhanmin2014 qinhanmin2014 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks @peterjc

@qinhanmin2014
qinhanmin2014 merged commit f4060cb into scikit-learn:master Feb 19, 2019
@peterjc
peterjc deleted the patch-1 branch February 19, 2019 12:42
@peterjc

peterjc commented Feb 19, 2019

Copy link
Copy Markdown
Contributor Author

A pleasure, thank you both.

xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants