typestar

Choosing the folds in Python

KFold and StratifiedKFold decide how the data is carved up.

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

X = np.arange(20).reshape(10, 2)
y = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])

plain = KFold(n_splits=5, shuffle=True, random_state=0)
balanced = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)

for train_idx, test_idx in plain.split(X):
    print("plain", test_idx)
    break

for train_idx, test_idx in balanced.split(X, y):
    print("stratified", test_idx, y[test_idx])
    break

How it works

  1. shuffle=True guards against ordering in the source data.
  2. StratifiedKFold preserves class balance in every fold.
  3. split yields index arrays, so you can inspect the folds.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
456
Tokens
136
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 74 seconds.

Type this snippet

Step 2 of 4 in Validation, step 9 of 25 in Machine learning with scikit-learn.

← Previous Next →