Repository navigation
DOC Document approximate stratification in StratifiedShuffleSplit - #33728
RudrenduPaul wants to merge 4 commits into
Conversation
…scikit-learn#33728) Built by Rudrendu Paul, developed with Claude Code
ArturoAmorQ
left a comment
There was a problem hiding this comment.
Thanks for the PR @RudrenduPaul. Here's a batch of comments.
For extra context: Train is allocated first from class_counts, and test is allocated from class_counts_remaining. So in the edge case where a class has very few samples, the train split consumes them all, leaving none for the test set. See :
scikit-learn/sklearn/model_selection/_split.py
Lines 2380 to 2385 in c1e7857
| - :class:`model_selection.StratifiedShuffleSplit` now documents that | ||
| per-class sample allocation uses an approximation of the multivariate | ||
| hypergeometric distribution, which can be off by one sample per class. | ||
| Heavily underrepresented classes may receive zero samples in a split. | ||
| By :user:`Rudrendu Paul <RudrenduPaul>`. |
There was a problem hiding this comment.
We don't need a change log for a documentation change. Please revert.
| .. note:: | ||
|
|
||
| The per-class allocation of samples to train and test splits is based | ||
| on an approximation of the multivariate hypergeometric distribution. | ||
| This approximation can be off by one sample per class. As a result, | ||
| when a class is heavily underrepresented relative to the total dataset | ||
| size, it may receive zero samples in either the train or test split | ||
| even though both ``n_train`` and ``n_test`` are greater than or equal | ||
| to the number of classes. In such cases, consider using | ||
| :class:`StratifiedKFold` instead, or ensure that the least populated | ||
| class has sufficiently many members relative to the split sizes. | ||
|
|
There was a problem hiding this comment.
Less is more
| .. note:: | |
| The per-class allocation of samples to train and test splits is based | |
| on an approximation of the multivariate hypergeometric distribution. | |
| This approximation can be off by one sample per class. As a result, | |
| when a class is heavily underrepresented relative to the total dataset | |
| size, it may receive zero samples in either the train or test split | |
| even though both ``n_train`` and ``n_test`` are greater than or equal | |
| to the number of classes. In such cases, consider using | |
| :class:`StratifiedKFold` instead, or ensure that the least populated | |
| class has sufficiently many members relative to the split sizes. |
|
Thanks for the thorough review @ArturoAmorQ! All four points make sense:
Will push an update shortly. |
There was a problem hiding this comment.
LGTM, thanks. But I rather wait for @glemaitre or someone else to confirm my suggested wording, because the "±1 counts" refers to the rounding in _approximate_mode itself, but the resulting difference between train and test counts for that class can be larger.
From the example in #28994, 2 samples of class 0 and a train/test ratio of 80/20, the proportional allocation gives 1.6 samples to train, which rounds up to 2, consuming both samples.
PranavAchar01
left a comment
There was a problem hiding this comment.
Documenting this is worthwhile, thanks. I went looking for the behaviour to check the wording and found it is sharper than the note suggests, which I think makes it more useful to state directly.
The note says the count "can differ by one, which ... may result in no samples of that class in the test set". In practice a class gets zero test samples exactly when
n_samples_in_class * test_size < 0.5
and when that holds it happens on every split, not occasionally. Sweeping it (200 majority samples, 30 seeds each):
n_minor test_size n*ts observed
2 0.1 0.20 zero on all seeds
2 0.2 0.40 zero on all seeds
2 0.25 0.50 never zero
3 0.1 0.30 zero on all seeds
3 0.2 0.60 never zero
4 0.1 0.40 zero on all seeds
4 0.2 0.80 never zero
The predicted and observed columns agreed on every combination I tried.
That also explains the report in #28994 exactly: with two samples in the minority class and the usual test_size=0.2, 2 * 0.2 = 0.4 < 0.5, so the test set never receives one and you need a third sample. Which is the issue's title.
So a reader hitting this can check the condition directly rather than discovering it empirically. Something like:
A class receives no samples in the test set when its number of samples
multiplied bytest_sizerounds to zero, that is when it is below 0.5.
For example a class with two samples andtest_size=0.2will never appear
in the test set.
Also a small wording point on the current text: "the per-class sample count between train and test splits can differ by one" reads as though train and test counts are being compared to each other, where I think the intent is that each split's per-class count can differ by one from the exact proportional allocation.
Only a suggestion, and "may" is not wrong, but since the underlying behaviour is deterministic it seems a shame not to give the reader the actual condition.
|
Thanks both for the detailed review! @PranavAchar01 I independently verified your condition against the actual @ArturoAmorQ your point about the wording is also correct: since Changes pushed in
Ran the full Over to @glemaitre for a final look at the wording. |
…Split Add a note to the StratifiedShuffleSplit class docstring explaining that per-class sample allocation uses an approximation of the multivariate hypergeometric distribution. This approximation can be off by one sample per class, which means that a heavily underrepresented class may receive zero samples in either split even when n_train and n_test both satisfy the n_classes requirement. Addresses scikit-learn#28994. Built by Rudrendu Paul, developed with Claude Code
…scikit-learn#33728) Built by Rudrendu Paul, developed with Claude Code
Per @ArturoAmorQ review: remove changelog file (not needed for docs), shorten the approximation note, and move it after the stratification introduction. Built by Rudrendu Paul, developed with Claude Code
Give the exact deterministic condition (n_samples_in_class * test_size < 0.5) with a worked example, and correct the wording so it compares each split's count to its own proportional allocation rather than train to test directly.
ec84aca to
d3b5427
Compare
What does this PR do?
Fixes #28994.
Adds a
.. note::to theStratifiedShuffleSplitclass docstring explaining that per-class sample allocation uses an approximation of the multivariate hypergeometric distribution (via_approximate_mode). This approximation can be off by one sample per class.Context
As reported in #28994 and confirmed by @glemaitre, when a class is heavily underrepresented relative to the total dataset size (e.g., 2 minority samples out of 50,000),
StratifiedShuffleSplitmay allocate zero samples from that class to either the train or test split. This happens even though bothn_trainandn_testsatisfy the existing>= n_classesvalidation check.Concretely, with 2 minority samples out of 50,000 total:
_approximate_mode([2, 49998], n_train, rng)assigns 1 to train_approximate_mode([1, 49999], n_test, rng)where1/50000 * n_test < 0.5, so the minority class gets 0 in testThis behavior is surprising to users who expect the stratification guarantee to hold.
What changed
Added a
.. note::block to theStratifiedShuffleSplitclass docstring explaining:StratifiedKFoldfor such cases, or ensure sufficient minority class membershipChecklist