[MRG+1] Fix bug in StratifiedShuffleSplit for multi-label data with targets having > 1000 labels #9922
Conversation
…ecause str(row) uses an ellipsis when len(row) > 1000
Awesome, thank you for catching this and solving it! Could you also modify the relevant test so that it fails without the patch? Thanks! |
It would be nice to add a test. |
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? |
LGTM |
LGTM, merging, thanks a lot! |
jnothman
added 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
added 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
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.
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)))
returns4056
whilelen(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')
onrow
becausey = check_array(y, ensure_2d=False, dtype=None)
convertsy
to 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 anint32
data type.