CodeBlanks Logo Icon CodeBlanks

Stratified

Stratified k-fold preserves class distribution in each fold.

StratifiedKFold

In classification the folds must keep the class proportions; otherwise one fold might have no minority class at all. StratifiedKFold guarantees this:

from sklearn.model_selection import StratifiedKFold
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
for egitim, dogrulama in skf.split(X, y):   # y is needed
    print(np.bincount(y[dogrulama]))

This time split also takes y, because it keeps the proportions by looking at it.

Grouped and time-based splitting

If several measurements of the same patient are in both training and validation, the model "recognises" the patient and the score inflates. GroupKFold keeps all examples of a group in the same fold. For time series, TimeSeriesSplit always trains on the past and validates on the future:

from sklearn.model_selection import GroupKFold, TimeSeriesSplit
cross_val_score(model, X, y, cv=GroupKFold(5), groups=hasta_id)
for tr, va in TimeSeriesSplit(n_splits=3).split(X):
    print(tr.max() < va.min())   # True: training always comes first

The right splitter is the one that mimics how the data will really be used.

Practice Fold proportions

For y made of 12 zeros and 3 ones, compute the number of 1s in the folds of KFold(n_splits=3) and StratifiedKFold(n_splits=3) (both without shuffling) as a list with y[dogrulama].sum(). Print the two lists on separate lines.

Starter code

model.py
import numpy as np
from sklearn.model_selection import KFold, StratifiedKFold

X = np.arange(15).reshape(-1, 1)
y = np.array([0] * 12 + [1] * 3)

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.model_selection import KFold, StratifiedKFold

X = np.arange(15).reshape(-1, 1)
y = np.array([0] * 12 + [1] * 3)

for bolucu in [KFold(n_splits=3), StratifiedKFold(n_splits=3)]:
    print([int(y[dogrulama].sum()) for _, dogrulama in bolucu.split(X, y)])

Expected output

[0, 0, 3]
[1, 1, 1]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why does <code>StratifiedKFold.split</code> need the <code>y</code> parameter?

  • It doesn't
  • To keep the class proportions Correct answer
  • To train the model
  • To compute scores

Explanation: Distribution into folds is done for each class separately.

2 Keep the examples of the same group in the same fold.

Question
cross_val_score(model, X, y, cv=GroupKFold(5), ____=hasta_id)
Answer
cross_val_score(model, X, y, cv=GroupKFold(5), groups=hasta_id)

Explanation: groups gives which group each example belongs to.

3 Match the situations with the splitters.

Term Meaning
Imbalanced classes StratifiedKFold
Measurements of the same patient GroupKFold
Daily sales TimeSeriesSplit
Independent samples KFold

All lessons in Intermediate