In this shot, we will learn how to calculate percentiles with NumPy.
A percentile is defined as a score at or below which a given percentage falls. For example, the $27^{th}$ percentile is the score below which 27% of the scores will be found.
In other words, let’s say you score in the 99th percentile in a certain exam; this means you are above 99% of the people taking the exam.
We can use the numpy.percentile()
function to calculate percentiles in Python.
The numpy.percentile()
function is used to calculate the $n^{th}$ percentile of the given data (array) along the specified axis.
The syntax of the numpy.percentile()
function is shown below.
numpy.percentile(array, percentile, axis=None, out=None, overwrite_input=False, keepdims=False)
The numpy.percentile()
function accepts the following parameters:
array
: The source array whose percentile needs to be computed.
percentile
: Signifies the percentile that needs to be computed.
axis
(optional): Defines the axis along which the percentile is calculated. By default, a flattened array is used.
out
(optional): An alternate output array where we can place the result.
overwrite_input
(optional): Can be used to modify the input array.
keepdims
(optional): Creates reduced axes with dimensions of one size.
The numpy.percentile()
function returns a scalar or array with percentile values along the specified axis.
Let’s look at the code.
# Using 1-D array import numpy as np # Array of data arr = [5,6,9,87,2,3,5,7,2,6,5,2,3,4,69,4] # Finding the 90 percentile x = np.percentile(arr, 90) print(x)
In line 2, we import the numpy
library with alias np
.
In line 5, we create an array of data.
In line 8, we use the np.percentile()
function to find the $90^{th}$ percentile from the given dataset.
The code above deals with a 1-D array. Now, we will explore a 2-D array.
#using 2-D array import numpy as np # Array of data arr = [[5,6,8],[6,9,2]] # Finding the 90 percentile x = np.percentile(arr, 90) print(x)
This is how we can calculate percentiles in Python with the NumPy library.
RELATED TAGS
CONTRIBUTOR
View all Courses