Train-Test Split — ML Technical Interview Questions & Answers

Holding out data to evaluate generalization.

Train-Test Split

  • Simple meaning: You hide some rows until the end. Train = the model may look. Test = exam paper. Fit only on train. Score only on test. If you tune on test, you peeked at the exam.
  • Picture: 100 students. 80 train, 20 test. The model never sees those 20 while learning. Then you ask: how well did it guess their marks?
  • sklearn: train_test_split(X, y, test_size=0.2, random_state=42). Shuffle unless the data is a time series (then split by date, not random).
  • If you try many models, use a third slice (validation) or cross-validation. Test is the last number you report.
  • Trap: scaling or filling NA using statistics from the whole sheet including test. Trap: the same student in train and test (duplicate rows).
  all rows
    ├── 80% train  →  fit
    └── 20% test   →  score only

Technical Interview Tip: Fit on train. Score on test. Don’t peek.

Code Examples for Train-Test Split

Train-Test Split — 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

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

How to Explain Train-Test Split to an Interviewer

Why a test set?

To see if it works on unseen rows Not just memorised the sheet

sklearn helper?

train_test_split test_size=0.2 random_state