Every Fold Gets Its Share

Machine Learning in CodeMedium+10 XP+10

Every Fold Gets Its Share

A clinic's records arrived sorted, with all the positive cases bunched together. Plain folds cut the rows into blocks in order, so some folds can end up with no positives at all. Stratified folds keep the class balance the same in every fold.

Task: write positives_per_fold(labels, n_folds).

  • labels holds 0 or 1 for each row, in the order the rows arrived.
  • Split the rows with KFold(n_splits=n_folds) and with StratifiedKFold(n_splits=n_folds), both without shuffling.
  • For each splitter, count how many 1s land in each test fold, in the order the splitter gives the folds.

Return a dict with two lists of whole numbers: "plain" for KFold and "stratified" for StratifiedKFold.

The splitters only look at the number of rows and the labels. Any placeholder X with one row per label will do.

Examples

labels = [0] * 12 + [1] * 3
n_folds = 3
returns {'plain': [0, 0, 3], 'stratified': [1, 1, 1]}
labels = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
n_folds = 4
returns {'plain': [0, 0, 1, 3], 'stratified': [1, 1, 1, 1]}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run