Calculate Percentile

Calculate the nth percentile of an array using linear interpolation.

Code

General
import math
sorted_arr = sorted(arr)
index = (p / 100) * (len(sorted_arr) - 1)
lower = math.floor(index)
fraction = index - lower
return sorted_arr[lower] + fraction * (sorted_arr[lower + 1] - sorted_arr[lower] if lower + 1 < len(sorted_arr) else 0)

Parameters

Array of numbers

Percentile (0-100)

Server

More Python Snippets