Open In App

numpy.percentile() in python

Improve
Improve
Like Article
Like
Save
Share
Report

numpy.percentile() function used to compute the nth percentile of the given data (array elements) along the specified axis. 

Python numpy.percentile() Syntax Function

Syntax: numpy.percentile(arr, n, axis=None, out=None,overwrite_input=False, method=’linear’, keepdims=False, *, interpolation=None) 
Parameters : 

  • arr: Input array or object.
  • n:  Percentile or sequence of percentiles to compute, which must be between 0 and 100 inclusive.
  • axis: Axis along which we want to calculate the percentile value. Otherwise, it will consider arr to be flattened(works on all the axes). axis = 0 means along the column and axis = 1 means working along the row.
  • out: Different array in which we want to place the result. The array must have same dimensions as expected output. 
  • overwrite_input (bool, optional): Specifies the interpolation method used when the desired quantile lies between two data points. Default is ‘linear’.
  • keepdims: (bool, optional): If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array a.
  • interpolation: (str, optional): Deprecated name for the method keyword argument.

Return :nth Percentile of the array (a scalar value if axis is none)or array with percentile values along specified axis.

Python NumPy percentile() Function Examples

Below are some examples by which we can understand how to calculate percentiles in NumPy in Python:

Finding Percentile Value Using NumPy

In this example, a 1D array `arr` is created with values [20, 2, 7, 1, 34]. Using NumPy’s `percentile` function, the 50th, 25th, and 75th percentiles of the array are calculated and printed, providing key statistical measures for the data distribution.

Python3




import numpy as np
 
# 1D array
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("50th percentile of arr : ",
      np.percentile(arr, 50))
print("25th percentile of arr : ",
      np.percentile(arr, 25))
print("75th percentile of arr : ",
      np.percentile(arr, 75))


Output:

arr :  [20, 2, 7, 1, 34]
50th percentile of arr : 7.0
25th percentile of arr : 2.0
75th percentile of arr : 20.0

Get the Percentile Value of 2-D Array Using NumPy

In this example, a 2D array `arr` is created. The np.percentile() function is applied to calculate percentiles both across the flattened array (axis=None) and along axis=0. The 50th and 0th percentiles are computed, providing statistical insights into the data distribution across different dimensions.

Python3




import numpy as np
 
# 2D array
arr = [[14, 17, 12, 33, 44],
       [15, 6, 27, 8, 19],
       [23, 2, 54, 1, 4, ]]
print("\narr : \n", arr)
 
# Percentile of the flattened array
print("\n50th Percentile of arr, axis = None : ",
      np.percentile(arr, 50))
print("0th Percentile of arr, axis = None : ",
      np.percentile(arr, 0))
 
# Percentile along the axis = 0
print("\n50th Percentile of arr, axis = 0 : ",
      np.percentile(arr, 50, axis=0))
print("0th Percentile of arr, axis = 0 : ",
      np.percentile(arr, 0, axis=0))


Output:

arr : 
[[14, 17, 12, 33, 44], [15, 6, 27, 8, 19], [23, 2, 54, 1, 4]]
50th Percentile of arr, axis = None : 15.0
0th Percentile of arr, axis = None : 1.0
50th Percentile of arr, axis = 0 : [15. 6. 27. 8. 19.]
0th Percentile of arr, axis = 0 : [14. 2. 12. 1. 4.]
50th Percentile of arr, axis = 1 : [17. 15. 4.]
0th Percentile of arr, axis = 1 : [12. 6. 1.]

Get the Percentile along the Axis in NumPy

In this example, a 2D array `arr` is created. The np.percentile() function is applied along axis=1 to calculate the 50th and 0th percentiles for each row, providing insights into the distribution of values along this axis. The use of `keepdims=True` ensures that the result retains the original dimensionality, maintaining clarity in the output.

Python3




import numpy as np
 
# 2D array
arr = [[14, 17, 12, 33, 44],
       [15, 6, 27, 8, 19],
       [23, 2, 54, 1, 4, ]]
print("\narr : \n", arr)
 
# Percentile along the axis = 1
print("\n50th Percentile of arr, axis = 1 : ",
      np.percentile(arr, 50, axis=1))
print("0th Percentile of arr, axis = 1 : ",
      np.percentile(arr, 0, axis=1))
 
print("\n0th Percentile of arr, axis = 1 : \n",
      np.percentile(arr, 50, axis=1, keepdims=True))
print("\n0th Percentile of arr, axis = 1 : \n",
      np.percentile(arr, 0, axis=1, keepdims=True))


Output:

arr : 
[[14, 17, 12, 33, 44], [15, 6, 27, 8, 19], [23, 2, 54, 1, 4]]
0th Percentile of arr, axis = 1 :
[[17.]
[15.]
[ 4.]]
0th Percentile of arr, axis = 1 :
[[12.]
[ 6.]
[ 1.]]


Last Updated : 24 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads