Clip outliers using percentiles learned on the training set only
Why it matters Winsorizing heavy-tailed features like latency, spend or token counts stabilizes linear models and neural networks.
Clipping extreme values (winsorizing) keeps a few outliers from dominating a model. The clip limits are learned parameters, so they must come from the training data and be reused unchanged on test data.
Write clip_outliers(train, test, p_low, p_high). train and test are 1D float arrays; p_low < p_high are percentiles in 0..100. Compute lo = np.percentile(train, p_low) and hi = np.percentile(train, p_high) (default linear method). Return (train_clipped, test_clipped, lo, hi) where both arrays are clipped to [lo, hi].
def clip_outliers(train, test, p_low, p_high)Example
clip_outliers([1, 2, 3, 4, 100], [0, 50], 0, 75) gives train [1, 2, 3, 4, 4], test [1, 4], lo 1.0, hi 4.0.
Understand the idea
Winsorizing caps extreme values at chosen percentiles instead of deleting rows. It keeps the row but stops a single huge value from pulling a model around. The percentiles are like a mean or a scaler: learned from training data, then frozen.
Worked example: train [1, 2, 3, 4, 100]. The 0th percentile is 1. The 75th with linear interpolation sits at position 0.75 * 4 = 3 in the sorted list, which is 4. So lo = 1, hi = 4, and train becomes [1, 2, 3, 4, 4]. Test [0, 50] becomes [1, 4].
Rule: lo = percentile(train, p_low), hi = percentile(train, p_high), x_clipped = min(max(x, lo), hi), applied with the same lo and hi everywhere.
Feature pipelines for tabular models and reward-score normalization in RL use percentile clipping.
Shapes and data flow
train (N,), test (M,) -> same shapes, plus two scalars