[MRG+1] Fix bug in StratifiedShuffleSplit for multi-label data with targets having > 1000 labels#9922
Merged
lesteve merged 3 commits intoscikit-learn:masterfrom Oct 17, 2017
Conversation
…ecause str(row) uses an ellipsis when len(row) > 1000
Member
|
Awesome, thank you for catching this and solving it! Could you also modify the relevant test so that it fails without the patch? Thanks! |
Member
|
It would be nice to add a test. |
Contributor
Author
|
Good idea @vene and @lesteve. I added a test for a Is there anything else that would need to be done to hook up this test? |
Member
|
LGTM, merging, thanks a lot! |
jnothman
pushed a commit
to jnothman/scikit-learn
that referenced
this pull request
Oct 17, 2017
…argets having > 1000 labels (scikit-learn#9922) * Use ' '.join(row) for multi-label targets in StratifiedShuffleSplit because str(row) uses an ellipsis when len(row) > 1000 * Add a new test for multilabel problems with more than a thousand labels
maskani-moh
pushed a commit
to maskani-moh/scikit-learn
that referenced
this pull request
Nov 15, 2017
…argets having > 1000 labels (scikit-learn#9922) * Use ' '.join(row) for multi-label targets in StratifiedShuffleSplit because str(row) uses an ellipsis when len(row) > 1000 * Add a new test for multilabel problems with more than a thousand labels
jwjohnson314
pushed a commit
to jwjohnson314/scikit-learn
that referenced
this pull request
Dec 18, 2017
…argets having > 1000 labels (scikit-learn#9922) * Use ' '.join(row) for multi-label targets in StratifiedShuffleSplit because str(row) uses an ellipsis when len(row) > 1000 * Add a new test for multilabel problems with more than a thousand labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug for multi-label targets in
StratifiedShuffleSplit. The solution being used now is the "label powerset" method: each sequence of labels is mapped to a string withstr(row), which transforms a multi-label problem into a multi-class problem.To see the source of the problem, note that
len(str(np.arange(1000)))returns4056whilelen(str(np.arange(1001)))returns36. The reason is that arrays with > 1000 elements are truncated with an ellipsis:str(np.arange(1001))gives'[ 0 1 2 ..., 998 999 1000]'. Thus, for multi-label targets with > 1000 labels, samples are mapped onto the same short string whenever their first three values and last three values are the same, which is not the intended behavior.The solution proposed here, discussed with @vene in this comment thread, is to use
' '.join(row.astype('str'))to convert each target to a string. We are guaranteed that we can do call.astype('str')onrowbecausey = check_array(y, ensure_2d=False, dtype=None)convertsyto a numpy array.As an added benefit, this approach ends up being several faster than
str(row)whenlen(row) < 1000:I also considered using
sklearn.utils.murmurhash3_32(suggested by @vene as another option) but concluded it was tricky to coerce all the kinds of labels people might use into anint32data type.