Sub topic 7
Choosing k: bias, variance, and cost
This chapter assumes you know how k-fold cross-validation works, can run cross_val_score in scikit-learn, and understand overfitting. Here you will learn how to pick the number of folds, k, and why the choice matters.
What k changes
In k-fold cross-validation, each model trains on a fraction (k-1)/k of the data. Changing k changes three things:
- Training size per fold. With k=2, each model sees 50% of the data. With k=10, it sees 90%.
- Number of model fits. You train k models, so cost grows linearly with k.
- Test size per fold. Each test fold holds n/k samples, so a larger k means smaller, noisier test folds.
Bias of the estimate
Most models improve with more training data. A model trained on 50% of the data is usually worse than one trained on 100%. So a small k makes the score pessimistic: it underestimates how well the final model, trained on all the data, will do. This is the bias of the estimate.
A larger k trains on nearly the full dataset, so the bias shrinks. The effect is biggest when the dataset is small, or when the model's learning curve is still rising steeply.
Variance of the estimate
Variance describes how much the estimate would change if you drew a different dataset or used a different split. A larger k lowers bias, but it does not always lower variance.
- With large k, the training sets overlap heavily. The k models are very similar, so their errors are highly correlated. Averaging correlated values removes less noise.
- With small k, the training sets differ more, but each model is trained on less data.
The usual conclusion is that variance does not fall steadily as k grows, and the best trade-off is often in the middle. In practice, k=5 and k=10 work well for most problems. Treat this as a rule of thumb, not a theorem. The exact variance of k-fold estimates is hard to estimate, and no unbiased estimator of it exists.
Cost
Total cost is roughly k times the cost of one fit. If a model takes 10 minutes to train, 10-fold cross-validation takes about 100 minutes. This often decides the question. For expensive models such as large ensembles, use k=3 or k=5. For fast models on small data, you can afford a larger k or repeated runs.
Seeing the effect
The code below compares several values of k on one dataset. Scores usually change little. The spread across folds grows as k grows, because the test folds get smaller.
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_breast_cancer(return_X_y=True)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
for k in [2, 3, 5, 10, 20]:
cv = StratifiedKFold(n_splits=k, shuffle=True, random_state=0)
scores = cross_val_score(model, X, y, cv=cv, scoring="accuracy")
print(f"k={k:2d} mean={scores.mean():.3f} std={scores.std():.3f}")The per-fold standard deviation is not the uncertainty of the mean. It describes how scores vary between folds. Do not read small differences between two values of k as meaningful.
A practical guide
| Situation | Suggested k |
|---|---|
| Default choice | 5 or 10 |
| Very small dataset | 10 or more, or repeated k-fold |
| Slow model or large dataset | 3 or 5 |
| Rare classes | Keep k small enough that each test fold still contains every class |
Common errors
- Picking k to get a better score. Trying many values of k and keeping the best one turns k into a tuned setting. Choose k first, based on data size and cost.
- Comparing scores across different k. Scores from different values of k have different bias. Compare models using the same splits.
- Ignoring structure. A good k does not fix leakage from grouped or time-ordered data. Choose the splitting scheme first, then k.
Exercise
You have 200 samples and a model that takes 30 seconds to train. You want a low-bias estimate without waiting hours. Would you choose k=2, k=10, or k=200? Explain.
Show answer
k=10. It trains on 90% of the data, so the bias is small, and it needs 10 fits, about 5 minutes. k=2 trains on only half the data and gives a pessimistic score. k=200 (leave-one-out) needs 200 fits, about 100 minutes, and its fold models are almost identical, so it gains little over k=10.