How to calculate percentiles in NumPy

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 27th27^{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 nthn^{th} percentile of the given data (array) along the specified axis.

Syntax

The syntax of the numpy.percentile() function is shown below.

numpy.percentile(array, percentile, axis=None, out=None, overwrite_input=False, keepdims=False)

Parameters

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.

Return value

The numpy.percentile() function returns a scalar or array with percentile values along the specified axis.

Code

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)

Explanation

  • 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 90th90^{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)

Explanation

  • The code above is exactly the same as the previous example. The only difference is that, now, we create a 2-D array instead of a 1-D array.

This is how we can calculate percentiles in Python with the NumPy library.

Free Resources