Open In App

numpy.percentile() in python

Last Updated : 21 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

numpy.percentile() compute the q-th percentile of data along the specified axis. A percentile is a measure indicating the value below which a given percentage of observations in a group falls. Example:

Python
import numpy as np
a = np.array([1, 3, 5, 7, 9])
res = np.percentile(a, 50)
print(res)

Output
5.0

Syntax

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

Parameters:

Parameter

Description

a

Input array or object that can be converted to an array

q

Percentile(s) to compute (0–100). Can be scalar or array-like

axis

Axis along which the percentiles are computed. Default is None (flattened)

out

Optional output array

overwrite_input

If True, the input array can be modified for memory efficiency

interpolation

(Deprecated) Use method instead

method

Method to compute percentile: 'linear', 'lower', 'higher', 'midpoint', 'nearest'

keepdims

If True, the reduced axes are left in the result as dimensions with size one

Returns: The q-th percentile(s) of the array elements. If q is a list, it returns multiple percentiles.

Examples

Example 1: In this example, we compute the 25th, 50th and 75th percentiles of a 1D array.

Python
import numpy as np
a = np.array([10, 20, 30, 40, 50])
res = np.percentile(a, [25, 50, 75])
print(res)

Output
[20. 30. 40.]

Example 2: In this example, we compute the 50th percentile (median) along each row of a 2D array using the axis parameter.

Python
import numpy as np
a = np.array([[10, 7, 4], [3, 2, 1]])
res = np.percentile(a, 50, axis=1)
print(res)

Output
[7. 2.]

Example 3: In this example, we compute the 50th percentile (median) using the method='lower' option.

Python
import numpy as np
a = np.array([1, 2, 3, 4])
res = np.percentile(a, 50, method='lower')
print(res)

Output
2

Example 4: In this example, we compute the 50th percentile (median) along each row of a 2D array and use keepdims=True to preserve the original dimensions.

Python
import numpy as np
a = np.array([[10, 20, 30], [40, 50, 60]])
res = np.percentile(a, 50, axis=1, keepdims=True)
print(res)

Output
[[20.]
 [50.]]

Next Article

Similar Reads