tezvyn:

What is data leakage in preprocessing and cross-validation?

AI-drafted, machine-checkedSource: scikit-learn.orgintermediate

This tests recognition of data leakage through preprocessing statistics. A strong answer defines leakage, describes scaling using global statistics before CV splits, and states transformers must be fit per training fold.

WHAT THIS TESTS: The interviewer wants to know if you understand that data leakage is not just about having identical rows in train and test, but about any statistical information from the validation or test set influencing the training phase. In the context of cross-validation, the critical issue is whether preprocessing steps like scaling, imputation, or feature selection are fit using data that includes the current validation fold. This reveals whether you treat a CV fold as a proxy for truly unseen production data.

A GOOD ANSWER COVERS: First, a crisp definition of data leakage as the use of information not available at prediction time to create the model. Second, a concrete preprocessing example such as fitting a standard scaler on the entire dataset before cross-validation, which causes the mean and standard deviation to include the validation fold values, or imputing missing values using the global mean before splitting. Third, the correct workflow where each preprocessing transform is fit only on the training portion of each CV split and then applied to the validation portion. Fourth, the recommendation to use a Pipeline object so that fit and transform are automatically scoped to the training folds inside each CV iteration.

COMMON WRONG ANSWERS: A major red flag is saying you scale or impute the whole dataset before splitting because it is more convenient. Another is arguing that test data does not need to be transformed at all, or that leakage only matters with duplicated rows. Some candidates also confuse data leakage with overfitting or describe target leakage while ignoring feature leakage through preprocessing.

LIKELY FOLLOW-UPS: The interviewer may ask how this applies to feature selection inside cross-validation, or what happens if you use the target variable to impute missing values. They might also ask for the practical fix in scikit-learn, which is using Pipeline inside cross_val_score or GridSearchCV, or they could probe how temporal data changes the splitting strategy.

ONE CONCRETE EXAMPLE: Suppose you have a dataset with one feature and some missing values. You fill missing values with the global mean and then run five-fold cross-validation. In each fold, the training set mean is slightly biased by the validation fold values, so the model learns from information it should not have. The correct approach is to compute the mean only from the training rows of that fold and use it to fill both the training and validation rows for that fold. In scikit-learn, this is handled cleanly by building a Pipeline with SimpleImputer followed by an estimator, then passing the pipeline to cross_val_score.

Read the original → scikit-learn.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.