-
-
Notifications
You must be signed in to change notification settings - Fork 26.2k
[MRG+1] Fix bug in StratifiedShuffleSplit for multi-label data with targets having > 1000 labels #9922
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
[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? |
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.
LGTM
LGTM, merging, thanks a lot! |
…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
…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
…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 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.