Cross-Validation — ML Technical Interview Questions & Answers

k-fold validation for a more reliable performance estimate.

Cross-Validation

  • Simple meaning: One 80/20 split can be lucky. Cross-validation rotates the test fold. k-fold: cut into k parts, train on k−1, test on 1, repeat k times, average the score.
  • Picture: 5-fold. Five different “exam papers”. If all five scores are ~0.82, you trust it. If they are 0.50 then 0.95, the model is unstable.
  • sklearn: cross_val_score(model, X, y, cv=5). Still keep a final hold-out test if you have enough rows — CV is for choosing the model, test is the last report.
  • Trap: using CV on rows that already leaked (scaler fit on all X). Trap: shuffling a time series — use TimeSeriesSplit.

Technical Interview Tip: k-fold: k exams, average the mark. Don’t leak the scaler.

Code Examples for Cross-Validation

Cross-Validation — working example

# Holdout split (sklearn: train_test_split). Pure Python for the lab:
X = [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
cut = int(0.8 * len(X))
X_train, X_test = X[:cut], X[cut:]
y_train, y_test = y[:cut], y[cut:]
print(len(X_train), len(X_test))
print(y_test)

# k-fold idea: rotate the held-out fold; average scores

Cross-Validation — hold out test data so you measure generalization. Cross-validation rotates folds for a stabler estimate.

How to Explain Cross-Validation to an Interviewer

What is k-fold CV?

k parts Train on k−1, test on 1 Average

sklearn?

cross_val_score cv=5