Robust Scaling
Robust Scaling
Robust scaling normalizes features using statistics that are robust to outliers: the median and the interquartile range (IQR). Unlike standard scaling (which uses mean and standard deviation), a single extreme outlier does not distort the scaling. This makes robust scaling the preferred normalization method when data contains outliers that should not influence the scale.
Given a list of values, scale each value using the formula below. If the IQR is zero, return (value - median) without dividing.
Formula
xscaled=Q3−Q1x−medianwhere Q1 is the first quartile (median of the lower half) and Q3 is the third quartile (median of the upper half).
Examples
Input:
values = [1, 2, 3, 4, 5]
Output:
[-0.6667, -0.3333, 0.0, 0.3333, 0.6667]
Median = 3, Q1 = 1.5, Q3 = 4.5, IQR = 3. Each value is centered by 3 and divided by 3.
Input:
values = [10, 20, 30, 40]
Output:
[-0.75, -0.25, 0.25, 0.75]
Median = 25, Q1 = 15, Q3 = 35, IQR = 20. The even-length list uses the average of two middle values as the median.
Hint 1
Sort the values first. For the median: if n is odd, take the middle element; if even, average the two middle elements. For Q1, take the median of values before the middle; for Q3, take the median of values after the middle.
Hint 2
Be careful with the lower/upper halves. For n=5 (indices 0-4), the lower half is indices [0,1] and upper half is [3,4] (excluding the median at index 2). For n=4, the lower half is [0,1] and upper half is [2,3].
Requirements
- Compute the median as the middle value (odd length) or average of two middle values (even length)
- Compute Q1 as the median of the lower half and Q3 as the median of the upper half
- If IQR is zero, return (value - median) without dividing
- For a single element, return [0.0] since the median equals the value and IQR is zero
- Return a list of floats with the same length as the input
Constraints
- values has at least 1 element
- Values can be negative
- Return a list of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Robust Scaling
Robust Scaling
Robust scaling normalizes features using statistics that are robust to outliers: the median and the interquartile range (IQR). Unlike standard scaling (which uses mean and standard deviation), a single extreme outlier does not distort the scaling. This makes robust scaling the preferred normalization method when data contains outliers that should not influence the scale.
Given a list of values, scale each value using the formula below. If the IQR is zero, return (value - median) without dividing.
Formula
xscaled=Q3−Q1x−medianwhere Q1 is the first quartile (median of the lower half) and Q3 is the third quartile (median of the upper half).
Examples
Input:
values = [1, 2, 3, 4, 5]
Output:
[-0.6667, -0.3333, 0.0, 0.3333, 0.6667]
Median = 3, Q1 = 1.5, Q3 = 4.5, IQR = 3. Each value is centered by 3 and divided by 3.
Input:
values = [10, 20, 30, 40]
Output:
[-0.75, -0.25, 0.25, 0.75]
Median = 25, Q1 = 15, Q3 = 35, IQR = 20. The even-length list uses the average of two middle values as the median.
Hint 1
Sort the values first. For the median: if n is odd, take the middle element; if even, average the two middle elements. For Q1, take the median of values before the middle; for Q3, take the median of values after the middle.
Hint 2
Be careful with the lower/upper halves. For n=5 (indices 0-4), the lower half is indices [0,1] and upper half is [3,4] (excluding the median at index 2). For n=4, the lower half is [0,1] and upper half is [2,3].
Requirements
- Compute the median as the middle value (odd length) or average of two middle values (even length)
- Compute Q1 as the median of the lower half and Q3 as the median of the upper half
- If IQR is zero, return (value - median) without dividing
- For a single element, return [0.0] since the median equals the value and IQR is zero
- Return a list of floats with the same length as the input
Constraints
- values has at least 1 element
- Values can be negative
- Return a list of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array