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
shuffle=Trueguards against ordering in the source data.StratifiedKFoldpreserves class balance in every fold.splityields index arrays, so you can inspect the folds.
Keywords and builtins used here
asbreakforprint
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.
Step 2 of 4 in Validation, step 9 of 25 in Machine learning with scikit-learn.