TensorTonic
Problems
Winsorization
Feature EngineeringData Processing
Medium

Winsorization limits extreme observations without removing them. Compute lower and upper percentile bounds by linearly interpolating within the sorted values, then clip every original value to those bounds.

For a percentile p and n sorted values, compute its fractional index:

k=(n−1)p100k = \frac{(n-1)p}{100}k=100(n−1)p​

Interpolate between the surrounding sorted entries:

qp=a⌊k⌋+(k−⌊k⌋)(a⌈k⌉−a⌊k⌋)q_p = a_{\lfloor k \rfloor} + (k-\lfloor k \rfloor)(a_{\lceil k \rceil}-a_{\lfloor k \rfloor})qp​=a⌊k⌋​+(k−⌊k⌋)(a⌈k⌉​−a⌊k⌋​)

Here, a is the sorted copy of values and q_p is the percentile bound. Clip values below the lower bound upward and values above the upper bound downward. Return the clipped values in their original order.

Loading visualization...

Examples

Input: values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], lower_pct = 10, upper_pct = 90

Output: [1.9, 2, 3, 4, 5, 6, 7, 8, 9, 9.1]

Explanation: The interpolated bounds are 1.9 and 9.1, so only the first and last values are clipped.

Input: values = [1, 2, 3, 4, 5], lower_pct = 0, upper_pct = 100

Output: [1, 2, 3, 4, 5]

Hint 1

Use (len(values) - 1) * percentile / 100 for the fractional sorted index.

Hint 2

Clip each original value with max(lower_bound, min(upper_bound, value)).

Requirements

  • Compute both percentile bounds using linear interpolation.
  • Clip values below the lower bound to that bound.
  • Clip values above the upper bound to that bound.
  • Preserve the original value order and input length.

Constraints

  • values is nonempty.
  • 0 <= lower_pct <= upper_pct <= 100.
  • Time limit: 300 ms.
Try Similar Problems
Implement Robust Scaling in PythonMediumMin-Max Scale Each Feature Column in PythonMediumLog Transform Features in PythonEasyImplement z-Score StandardizationEasyPercentiles / QuantilesEasyStreaming Min-Max NormalizationMediumOne-Hot Encoding (Multi-class)MediumEncode AlphaGo Zero Board HistoryMediumNormalize ColumnsMediumMissing Value ImputationEasyAngle FeaturesEasyFeature ScalingEasy
Scratchpad

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Winsorization

Winsorization

Feature EngineeringData Processing
Medium

Winsorization limits extreme observations without removing them. Compute lower and upper percentile bounds by linearly interpolating within the sorted values, then clip every original value to those bounds.

For a percentile p and n sorted values, compute its fractional index:

k=(n−1)p100k = \frac{(n-1)p}{100}k=100(n−1)p​

Interpolate between the surrounding sorted entries:

qp=a⌊k⌋+(k−⌊k⌋)(a⌈k⌉−a⌊k⌋)q_p = a_{\lfloor k \rfloor} + (k-\lfloor k \rfloor)(a_{\lceil k \rceil}-a_{\lfloor k \rfloor})qp​=a⌊k⌋​+(k−⌊k⌋)(a⌈k⌉​−a⌊k⌋​)

Here, a is the sorted copy of values and q_p is the percentile bound. Clip values below the lower bound upward and values above the upper bound downward. Return the clipped values in their original order.

Loading visualization...

Examples

Input: values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], lower_pct = 10, upper_pct = 90

Output: [1.9, 2, 3, 4, 5, 6, 7, 8, 9, 9.1]

Explanation: The interpolated bounds are 1.9 and 9.1, so only the first and last values are clipped.

Input: values = [1, 2, 3, 4, 5], lower_pct = 0, upper_pct = 100

Output: [1, 2, 3, 4, 5]

Hint 1

Use (len(values) - 1) * percentile / 100 for the fractional sorted index.

Hint 2

Clip each original value with max(lower_bound, min(upper_bound, value)).

Requirements

  • Compute both percentile bounds using linear interpolation.
  • Clip values below the lower bound to that bound.
  • Clip values above the upper bound to that bound.
  • Preserve the original value order and input length.

Constraints

  • values is nonempty.
  • 0 <= lower_pct <= upper_pct <= 100.
  • Time limit: 300 ms.
Try Similar Problems
Implement Robust Scaling in PythonMediumMin-Max Scale Each Feature Column in PythonMediumLog Transform Features in PythonEasyImplement z-Score StandardizationEasyPercentiles / QuantilesEasyStreaming Min-Max NormalizationMediumOne-Hot Encoding (Multi-class)MediumEncode AlphaGo Zero Board HistoryMediumNormalize ColumnsMediumMissing Value ImputationEasyAngle FeaturesEasyFeature ScalingEasy
Scratchpad

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1