The Wayback Machine - https://web.archive.org/web/20240831134629/https://www.geeksforgeeks.org/numpy-percentile-in-python/
Open In App

numpy.percentile() in python

Last Updated : 24 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.]]


Similar Reads

Print values above 75th percentile from series Using Quantile using Pandas
Given a series, the task is to print all the elements that are above the 75th percentile using Pandas in Python. There is a series of data, we have to find all the values of the series object whose value is greater than the 75th Percentile. Print values above 75th percentile from series Using QuantileCreate a series object of any datasetWe will cal
2 min read
Rank Based Percentile Gui Calculator using Tkinter
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. In this article, we will learn how to create a Rank Based - Percentile Gui Calculator application using Tkinter, with a step-by-step guide. Prerequisites: Introduction to TkinterProgram to calculate t
7 min read
PyQt5 - Percentile Calculator
In this article we will see how we can create a percentile calculator using PyQt5, below is how the percentile calculator will look like GUI implementation steps 1. Create a heading label that display the calculator name 2. Create label and line edit pair for total students, label to show what user has to enter and line edit to enter text 3. Simila
4 min read
Percentile rank of a column in a Pandas DataFrame
Let us see how to find the percentile rank of a column in a Pandas DataFrame. We will use the rank() function with the argument pct = True to find the percentile rank. Example 1 : # import the module import pandas as pd # create a DataFrame data = {'Name': ['Mukul', 'Rohan', 'Mayank', 'Shubham', 'Aakash'], 'Location' : ['Saharanpur', 'Meerut', 'Agr
1 min read
Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
Python | Numpy numpy.ndarray.__ge__()
With the help of numpy.ndarray.__ge__() method of Numpy, We can find that which element in an array is greater then or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ge__($self, value, /) Return: self>=value Example #1 : In this example
1 min read
Python | Numpy numpy.ndarray.__ne__()
With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!=value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__neg__()
With the help of numpy.ndarray.__neg__() method of Numpy, one can multiply each and every element of an array with -1. Hence, the resultant array having values like positive values becomes negative and negative values become positive. Syntax: ndarray.__neg__($self, /) Return: -self Example #1 : In this example we can see that after applying numpy._
1 min read
Python | Numpy numpy.ndarray.__pos__()
With the help of numpy.ndarray.__pos__() method of Numpy, one can multiply each and every element of an array with 1. Hence, the resultant array having values same as original array. Syntax: ndarray.__pos__($self, /) Return: +self Example #1 : In this example we can see that after applying numpy.__pos__(), we get the simple array that can be the sa
1 min read
Python | Numpy numpy.ndarray.__truediv__()
With the help of Numpy numpy.ndarray.__truediv__(), we can divide a particular value that is provided as a parameter in the ndarray.__truediv__() method. Value will be divided to each and every element in a numpy array. Syntax: ndarray.__truediv__($self, value, /) Return: self/value Example #1 : In this example, we can see that each element in an a
1 min read
Python | Numpy numpy.ndarray.__sub__()
With the help of Numpy numpy.ndarray.__sub__(), We can subtract a particular value that is provided as a parameter in the ndarray.__sub__() method. Value will be subtracted to each and every element in a numpy array. Syntax: ndarray.__sub__($self, value, /) Return: self-value Example #1 : In this example we can see that each and every element in an
1 min read
Python | Numpy numpy.ndarray.__add__()
With the help of Numpy numpy.ndarray.__add__(), we can add a particular value that is provided as a parameter in the ndarray.__add__() method. Value will be added to each and every element in a numpy array. Syntax: ndarray.__add__($self, value, /) Return: self+value Example #1 : In this example we can see that each and every element in an array is
1 min read
Python | Numpy numpy.ndarray.__floordiv__()
With the help of Numpy numpy.ndarray.__floordiv__(), one can divide a particular value that is provided as a parameter in the ndarray.__floordiv__() method. Value will be divided to each and every element in a numpy array and remember it always gives the floor value after division. Syntax: ndarray.__floordiv__($self, value, /) Return: self//value E
1 min read
Python | Numpy numpy.ndarray.__mod__()
With the help of Numpy numpy.ndarray.__mod__(), every element in an array is operated on binary operator i.e mod(%). Remember we can use any type of values in an array and value for mod is applied as the parameter in ndarray.__mod__(). Syntax: ndarray.__mod__($self, value, /) Return: self%value Example #1 : In this example we can see that value tha
1 min read
Python | Numpy numpy.ndarray.__invert__()
With the help of Numpy numpy.ndarray.__invert__(), one can invert the elements of an array. We don't have to provide any type of parameter but remember that this method only works for integer values. Syntax: ndarray.__invert__($self, /) Return: ~self Example #1 : In this example we can see that every element in an array is operated on a unary opera
1 min read
Python | Numpy numpy.ndarray.__divmod__()
With the help of Numpy numpy.ndarray.__divmod__() method, we will get two arrays one is having elements that is divided by value that is provided in numpy.ndarray.__divmod__() method and second is having elements that perform mod operation with same value as provided in numpy.ndarray.__divmod__() method. Syntax: ndarray.__divmod__($self, value, /)
1 min read
Python | Numpy numpy.ndarray.__pow__()
With the help of Numpy numpy.ndarray.__pow__() method, we will get all the elements powered with the value that is provided as a parameter in numpy.ndarray.__pow__() method. Syntax: ndarray.__pow__($self, value, mod=None, /) Return: pow(self, value, mod) Example #1 : In this example we can see that every element get powered with the value that is p
1 min read
Python | Numpy numpy.ndarray.__rshift__()
With the help of numpy.ndarray.__rshift__() method, we can get the elements that is right shifted by the value that is provided as a parameter in numpy.ndarray.__rshift__() method. Syntax: ndarray.__rshift__($self, value, /) Return: self>>value Example #1 : In this example we can see that every element is right shifted by the value that is pa
1 min read
Python | Numpy numpy.ndarray.__lshift__()
With the help of Numpy numpy.ndarray.__lshift__() method, we can get the elements that is left shifted by the value that is provided as a parameter in numpy.ndarray.__lshift__() method. Syntax: ndarray.__lshift__($self, value, /) Return: self<<value Example #1 : In this example we can see that every element is left shifted by the value that i
1 min read
Python | Numpy numpy.ndarray.__imul__()
With the help of numpy.ndarray.__imul__() method, we can multiply a particular value that is provided as a parameter in the ndarray.__imul__() method. Value will be multiplied to every element in a numpy array. Syntax: ndarray.__imul__($self, value, /)Return: self*=value Example #1 : In this example we can see that each and every element in an arra
1 min read
Python | Numpy numpy.ndarray.__isub__()
With the help of numpy.ndarray.__isub__() method, we can subtract a particular value that is provided as a parameter in the ndarray.__isub__() method. Value will be subtracted to every element in a numpy array. Syntax: ndarray.__isub__($self, value, /) Return: self-=value Example #1 : In this example we can see that each and every element in an arr
1 min read
Python | Numpy numpy.ndarray.__iadd__()
With the help of numpy.ndarray.__iadd__() method, we can add a particular value that is provided as a parameter in the ndarray.__iadd__() method. Value will be added to every element in a numpy array. Syntax: ndarray.__iadd__($self, value, /) Return: self+=value Example #1 : In this example we can see that each and every element in an array is adde
1 min read
Python | Numpy numpy.ndarray.__xor__()
With the help of Numpy numpy.ndarray.__xor__() method, we can get the elements that is XOR by the value that is provided as a parameter in numpy.ndarray.__xor__() method. Syntax: ndarray.__xor__($self, value, /) Return: self^value Example #1 : In this example we can see that every element is xor by the value that is passed as a parameter in ndarray
1 min read
Python | Numpy numpy.ndarray.__and__()
With the help of Numpy numpy.ndarray.__and__() method, we can get the elements that is anded by the value that is provided as a parameter in numpy.ndarray.__and__() method. Syntax: ndarray.__and__($self, value, /) Return: self&value Example #1 : In this example we can see that every element is anded by the value that is passed as a parameter in
1 min read
Python | Numpy numpy.ndarray.__or__()
With the help of Numpy numpy.ndarray.__or__() method, we can get the elements that is OR by the value that is provided as a parameter in numpy.ndarray.__or__() method. Syntax: ndarray.__or__($self, value, /) Return: self|value Example #1 : In this example we can see that every element is or by the value that is passed as a parameter in ndarray.__or
1 min read
Python | Numpy numpy.matrix.any()
With the help of Numpy numpy.matrix.any() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison if any of the element matches it return true. Syntax : numpy.matrix.any() Return : Return true if any match found else false Example #1 : In this example we can see
1 min read