stratified_split
stratified_split(samples, *, fractions=(0.7, 0.15, 0.15), group_by=None, seed=0)split_samples once per label, so no split comes out missing a class.
A label that is a set is a different algorithm and this dispatches to it, rather than being two functions a caller has to choose between: the question is the same one - does every part come out able to score every class - and the answer for a set of labels cannot be strata, because a film carrying three findings belongs to three of them. _iteratively_stratified has that half and the measurement for it. Which it is follows from the samples: labels set means a set, and a dataset labelled both ways is refused.
This is not a refinement, it is what a classification split needs to be. Measured over 400 seeds at 70/15/15, how often some part held none of the rare class:
images prevalence random stratified
100 3% 91.25% 0.00%
200 3% 63.75% 0.00%
500 3% 19.75% 0.00%
1000 3% 0.75% 0.00%
2000 1% 6.75% 0.00%
and the closed form agrees with the simulation - a part of share f holds none of r rare items with probability about (1-f)^r, so with three rare items the 15% parts miss them 0.85^3 = 0.6141 of the time each and some part misses them 0.9055 of the time against the 0.9125 measured above, which is half a standard error at 400 seeds.
That sentence quoted 0.5120 against a simulation of 51.18% until pyplatypus#329, and neither belongs to this table: 0.5120 is exactly 0.8^3, a part of share 0.2, and the nearest figure in this file is the 51.25% of the multi-label table 180 lines above - a different algorithm answering a different question. §4’s number measured in one setting and quoted in another. What it costs is nothing anybody can see: the worst drift from the requested fractions over 200 seeds is 0.0100 at 100 images, which is one image, and 0.0000 at 500.
The alternative is not a slightly worse split. Every per-class metric this task offers - balanced_accuracy, f1, auc - refuses to average over a class the pass never contained, so a split with an empty class fails at the end of the first epoch, after the epoch is paid for.
A wrapper rather than a flag inside split_samples, and deliberately: the assignment it does - shuffle the groups, hand each to whichever split is furthest below its target, then make sure none came out empty - is exactly what each stratum needs, so this calls it rather than reaching into it. One seed for every stratum, measured: the parts come out balanced at every seed without deriving one per label.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| samples | Sequence[Sample] | What was discovered. Every one must carry a label. |
required |
| fractions | Sequence[float] | Mapping[str, float] | As split_samples takes them. |
(0.7, 0.15, 0.15) |
| group_by | str | None | As split_samples takes it, and refused when any group holds more than one label - see below. |
None |
| seed | int | Which shuffle, the same for every stratum. | 0 |
Returns
| Name | Type | Description |
|---|---|---|
| Split | A Split, with the groups every stratum assigned. |
Raises
| Name | Type | Description |
|---|---|---|
| ConfigError | If a sample carries no label, if group_by matches a group holding more than one label, or - from split_samples, per stratum - if a class has fewer examples than there are splits to fill. |