Python / Python Mathematical Intuition and Scikit Learn Interview Questions
What is the difference between a single train/validation/test split and k-fold cross-validation for hyperparameter tuning, statistically?
A single validation split estimates a hyperparameter's performance using just one specific subset of data — this estimate has high variance because it depends entirely on which particular samples happened to land in the validation fold. If you tune hyperparameters against this single estimate, you risk overfitting to the quirks of that specific split (sometimes called "validation set overfitting").
K-fold cross-validation produces k separate performance estimates by rotating which fold serves as validation, then averages them. The variance of this average is mathematically lower than the variance of a single estimate (by a factor related to the correlation between folds, as discussed in the bias-variance tradeoff of CV), giving a more statistically reliable signal for comparing hyperparameter choices, at the cost of k times the computation.
from sklearn.model_selection import GridSearchCV, train_test_split from sklearn.svm import SVC # Single validation split approach (faster, noisier) X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2) best_score, best_C = 0, None for C in [0.1, 1, 10, 100]: model = SVC(C=C).fit(X_train, y_train) score = model.score(X_val, y_val) if score > best_score: best_score, best_C = score, C # K-fold cross-validation approach (slower, more reliable signal) param_grid = {'C': [0.1, 1, 10, 100]} grid_search = GridSearchCV(SVC(), param_grid, cv=5, scoring='accuracy') grid_search.fit(X_train_full, y_train_full) print('Best C:', grid_search.best_params_) print('CV score:', grid_search.best_score_) # Nested CV for unbiased performance estimate after tuning: # outer loop estimates generalization, inner loop tunes hyperparameters from sklearn.model_selection import cross_val_score nested_scores = cross_val_score(grid_search, X, y, cv=5)
More Related questions...