Numpy percentile – Python NumPy percentile() Function

NumPy percentile() Function:

Numpy percentile: The q-th percentile of the array elements or the q-th percentile of the data along the given axis is returned by the percentile() function of the NumPy module.

Syntax:

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

Parameters

a: This is required. It is the array given as input

q: This is required. It indicates the percentile or sequence of percentiles to compute that must be between 0 and 100 inclusive (array of float).

axis: This is optional. It is the axis or axes along which to operate. The axis=None operation is done on a flattened array by default.

out: This is optional. It is the output array in which the result will be stored. It must be the same shape as the expected result.

interpolation: This is optional. When the desired percentile is between two data points, it specifies the interpolation method to use. It can use values like ‘linear,’ ‘lower,’ ‘higher, ‘midpoint,’ and ‘nearest.’

keepdims: This is optional. The reduced axes are left in the output as dimensions with size one if this is set to True. The result will broadcast correctly against the input array if you use this option.

Return Value: 

The percentile points of “a” are returned. The output is a scalar if q is a single percentile and axis=None. In other cases, an array is returned.

NumPy percentile() Function in Python

Example1

Approach:

  • Import NumPy module using the import keyword
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array and some random percentile say 50 as an argument to the percentile() function of NumPy module
  • Here we get the 50th percentile point of the above array
  • Similarly, we can get 25,60,75 th percentile by passing them as tuple(second argument of the percentile function)
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5, 25, 50],[35, 40, 60]])              
# Print the above given array.
print("The above given array is:",gvn_arry)
print()
# Pass the given array and some random percentile say 50 as an argument to the percentile() function of numpy module 
#Here we get the 50th percentile point of the above array
print("50th percentile point of the above array:", np.percentile(gvn_arry, 50))
print()
#Similarly we can get 25,60,75 th percentile by passing them as tuple(second argument of the percentile function)
print("25 th , 60th , 75th percentile points of the above array are:\n", 
       np.percentile(gvn_arry, (25, 60, 75)))

Output:

The above given array is: [[ 5 25 50]
 [35 40 60]]

50th percentile point of the above array: 37.5

25 th , 60th , 75th percentile points of the above array are:
 [27.5 40.  47.5]

Example2

NP percentile: The percentile points are calculated over the given axes when the axis parameter is provided.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5, 25, 50],[35, 40, 60]])              
# Print the above given array.
print("The above given array is:",gvn_arry)
print()
#Gett the 50th percentile point along 0 axis by passing 50 as second argument and axis=0
print("50th percentile point of the above array along 0 axis : ", 
       np.percentile(gvn_arry, 50, axis=0))
#Get the 50th percentile point along 0 axis by passing 50 as second argument and axis=1
print("50th percentile point of the above array along 1 axis : ", 
       np.percentile(gvn_arry, 50, axis=1))

Output:

The above given array is: [[ 5 25 50]
 [35 40 60]]

50th percentile point of the above array along 0 axis :  [20.  32.5 55. ]
50th percentile point of the above array along 1 axis :  [25. 40.]

Example3

When calculating percentile points, the interpolation option can be used to specify the interpolation method to apply. Consider the following situation:

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5, 25, 50],[35, 40, 60]])              
# Print the above given array.
print("The above given array is:",gvn_arry)
print()
# Pass the given array and some random percentile say 50, and say interpolation is lower as an arguments to the percentile() function a of numpy module 
#Here we get the 50th percentile point of the above array
print("50th percentile point of the above array when interpolation is lower:", np.percentile(gvn_arry, 50, interpolation='lower'))

Output:

The above given array is: [[ 5 25 50]
 [35 40 60]]

50th percentile point of the above array when interpolation is lower: 35