← 返回學院
An instructor points to the middle stage in a three-stage diagram while two learners examine the sequence.
AI 知識核心2026年11月4日· 3 分鐘

Pipelines for Consistent Preprocessing

A pipeline chains imputer, scaler and classifier so each held-out example is transformed exactly as training data was. Fitting transformers inside each fold and reusing them at inference prevents inconsistent preprocessing and source-level leakage.

preprocessing pipelinesdata leakagecross-validation

來源

完整課程摘要

A pipeline chains preprocessing steps and a model into one object, so a single fit call prepares the whole sequence and a single predict call applies it to new data. Picture a table of apartment listings with two columns, floor area in square metres and distance to the nearest station in kilometres. One row is held out. The training rows contain a missing area value, so an imputer fills gaps with the training median, say 62 square metres. A scaler then subtracts each column's training mean and divides by its training standard deviation. Finally a classifier learns from the scaled rows.

When the held-out row arrives, the pipeline fills its gap with that same 62, scales it with those same stored means and deviations, and then asks the classifier for a label. A second held-out listing has a recorded area of 70 square metres and a distance of 1.2 kilometres. If the training mean area was 66 with a standard deviation of 12, its scaled area is about 0.33. That z-score is defined relative to training statistics, so it stays comparable across examples rather than drifting with each new batch of listings.

Where fitting happens matters. In five-fold cross-validation, fitting the imputer and scaler once on all rows before splitting lets each validation fold influence the median and means it is scored against. The pipeline alone does not stop this if you call fit on the full table first. The safer habit is to fit the entire pipeline separately inside each fold, so fold one's imputer sees only fold one's training rows. After choosing settings, refit on all training data and store the result.

At prediction time, pass a new listing to the fitted pipeline. Its prediction step applies each stored preprocessing transformation in sequence. A common error is passing raw test rows straight to the classifier, which then compares unscaled areas against scaled training features. Predictions look plausible but errors can be large, because a value of 80 sits far outside the range the classifier learned. Reusing the stored scaler keeps every number on the same footing. Caching fitted transformers can save time during a parameter search, provided the cached fit used identical rows.

A pipeline cannot repair leakage that lives in the source data. If the median area was computed from a table that already included the held-out rows, or if a feature such as a future inspection score was recorded after the outcome, wrapping steps in a pipeline changes nothing. Pipelines organise the order of operations and the reuse of fitted statistics. They do not audit how the raw table was assembled, so that check stays with the people who built it.

Teachers can give pairs a table of ten listings with one missing value and one held-out row. Ask them to write the median and mean they would use on a sticky note, then swap notes with another pair and compare. Pairs whose numbers differ explain which rows they counted. Trace the held-out row through each step on paper, recording the value after imputation and after scaling. End by asking each pair to name one leakage a pipeline cannot fix.