Compute requested percentiles with linear interpolation. After sorting n values, convert percentile q to a zero-based position:
r=100q(n−1)Let l=⌊r⌋, u=⌈r⌉, and w=r−l. Interpolate between the sorted values:
Pq=(1−w)xl+wxuApply this calculation to every value in q and return a NumPy array in the same order as the requested percentiles.
Input: x = [1, 2, 3, 4], q = [25, 50, 75]
Output: [1.75, 2.5, 3.25]
Explanation: The three percentile positions fall between adjacent sorted values and are linearly interpolated.
Input: x = [1, 2, 3, 4, 5], q = [50]
Output: [3.0]
Input: x = [4, 1, 3, 2], q = [25, 75]
Output: [1.75, 3.25]
Use positions = q / 100.0 * (x.size - 1).
Use np.floor and np.ceil to locate the interpolation neighbors.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Compute requested percentiles with linear interpolation. After sorting n values, convert percentile q to a zero-based position:
r=100q(n−1)Let l=⌊r⌋, u=⌈r⌉, and w=r−l. Interpolate between the sorted values:
Pq=(1−w)xl+wxuApply this calculation to every value in q and return a NumPy array in the same order as the requested percentiles.
Input: x = [1, 2, 3, 4], q = [25, 50, 75]
Output: [1.75, 2.5, 3.25]
Explanation: The three percentile positions fall between adjacent sorted values and are linearly interpolated.
Input: x = [1, 2, 3, 4, 5], q = [50]
Output: [3.0]
Input: x = [4, 1, 3, 2], q = [25, 75]
Output: [1.75, 3.25]
Use positions = q / 100.0 * (x.size - 1).
Use np.floor and np.ceil to locate the interpolation neighbors.
Sign in to take notes on this problem
Accepts: array
Accepts: array