I have a Numpy array and want to compute different percentiles of its elements. What is the pythonic way to compute?

+1 vote

You can use* **numpy**.percentile(). *It returns the q-th percentile(s) of the array elements. The function is as follows:

*numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)*

Check the following examples:

>>> import numpy as np

>>> a = [10, 20, 25, 30, 35, 15, 12]

>>> np.percentile(a, 50)

20.0

>>> a = np.array([[10, 7, 4], [3, 2, 1]])

>>> a

array([[10, 7, 4],

[ 3, 2, 1]])

>>> np.percentile(a, 50)

3.5

>>> np.percentile(a, 50, axis=0)

array([6.5, 4.5, 2.5])

>>> np.percentile(a, 50, axis=1)

array([7., 2.])