Winsorization: Clamp the Extremes, Keep the Rows

Feature EngineeringMedium+15 XP+15

Winsorization: Clamp the Extremes, Keep the Rows

You have a few extreme values and two tempting bad options: delete those rows (losing everything else they contain) or leave them in (letting them dominate every mean and every fitted coefficient). Winsorization takes a third route — keep the rows, but clamp the extremes to a percentile cutoff.

Task: write winsorize(values, lower_pct, upper_pct) returning the clamped values, each rounded to 4 decimal places.

  1. Find the value at the lower_pct percentile and at the upper_pct percentile.
  2. Replace anything below the lower cutoff with that cutoff, and anything above the upper cutoff with that one. Everything between passes through unchanged.

For both percentiles use the linear-interpolation rule on the sorted values:

position=(n−1)×p100\text{position} = (n - 1) \times \frac{p}{100}position=(n−1)×100p​

interpolating between neighbours when the position isn't a whole number.

  • lower_pct is below upper_pct, and both lie between 0 and 100.
  • With lower_pct = 0 and upper_pct = 100 the cutoffs are the actual minimum and maximum, so nothing is clamped.
  • Return floats throughout.

The distinction from trimming is the whole idea. Trimming removes the extreme rows; winsorizing keeps them and only flattens the one offending feature. So the row's other features, and its label, stay in your training set — you've suppressed one value's leverage without discarding a real observation.

What it costs you is the information that something extreme happened. After winsorizing, a transaction of £5,000,000 and one of £50,000 may both read as the 90th-percentile value, indistinguishable. If being extreme is itself the signal — as in fraud detection — clamp at your peril, and keep a flag for "this value was clamped".

Examples

values = [1, 2, 3, 4, 100]
lower_pct = 10
upper_pct = 90
returns [1.4, 2.0, 3.0, 4.0, 61.6]
values = [-10, 0, 1, 2, 50]
lower_pct = 25
upper_pct = 75
returns [0.0, 0.0, 1.0, 2.0, 2.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run