HeadlinesBriefing favicon HeadlinesBriefing.com

Model Leakage: Cheating on Test

Towards Data Science •
×

A preprocessing pipeline let my car price model peek at the test set before the exam, and the twelve points of R squared it cheated its way to a 0.887 score. The model was trained on a UCI Automobile dataset with 193 rows after cleaning, using a neural network with two hidden layers of 64 units each. After fixing the order of two lines, the honest score dropped to 0.767, showing that the original metric was artificially high.

The leak stemmed from outlier handling, Standard Scaler, and One Hot Encoder steps that were fit on the entire dataset, including test rows, before the train_test_split. Because these steps learned from the test data, the scaling numbers, outlier bounds, and category lists were subtly influenced, inflating the R squared without changing the model itself. The preprocessing code ran in the wrong order, allowing the test set to quietly nudge the statistics that feed the model.

The corrected pipeline splits the data first, then applies preprocessing only to the training set, ensuring that validation and test rows are transformed with values learned solely from training. This order restores a realistic performance estimate and prevents misleadingly strong scores.