numpy.percentile()function used to compute the nth percentile of the given data (array elements) along the specified axis.
Syntax : numpy.percentile(arr, n, axis=None, out=None)
Parameters :
arr :input array.
n : percentile value.
axis : axis along which we want to calculate the percentile value. Otherwise, it will consider arr to be flattened(works on all the axis). 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.Return :nth Percentile of the array (a scalar value if axis is none)or array with percentile values along specified axis.
Example 1: Compute Percentiles for a 1D Array
import numpy as np
# 1D array
arr = [20, 2, 7, 1, 34]
print("Array:", arr)
print("50th Percentile (Median):", np.percentile(arr, 50))
print("25th Percentile (Q1):", np.percentile(arr, 25))
print("75th Percentile (Q3):", np.percentile(arr, 75))
Output:
Array: [20, 2, 7, 1, 34] 50th Percentile (Median): 7.0 25th Percentile (Q1): 2.0 75th Percentile (Q3): 20.0
The 50th percentile equals the median, while 25th and 75th represent lower and upper quartiles respectively.
Example 2: Percentiles on a 2D Array
import numpy as np
arr = [
[14, 17, 12, 33, 44],
[15, 6, 27, 8, 19],
[23, 2, 54, 1, 4]
]
print("Array:\n", arr)
# Flattened percentile (all elements)
print("\n50th Percentile (Flattened):", np.percentile(arr, 50))
print("0th Percentile (Min):", np.percentile(arr, 0))
# Percentile along columns (axis=0)
print("\n50th Percentile along axis=0:", np.percentile(arr, 50, axis=0))
print("0th Percentile along axis=0:", np.percentile(arr, 0, axis=0))
Output:
Array: [[14, 17, 12, 33, 44], [15, 6, 27, 8, 19], [23, 2, 54, 1, 4]] 50th Percentile (Flattened): 15.0 0th Percentile (Min): 1.0 50th Percentile along axis=0: [15. 6. 27. 8. 19.] 0th Percentile along axis=0: [14. 2. 12. 1. 4.]
axis=0 calculates percentiles column-wise, treating each column independently.
Example 3: Row-Wise Percentiles (axis=1)
import numpy as np
arr = [
[14, 17, 12, 33, 44],
[15, 6, 27, 8, 19],
[23, 2, 54, 1, 4]
]
# Percentile along rows
print("50th Percentile (Median) along axis=1:", np.percentile(arr, 50, axis=1))
print("0th Percentile (Min) along axis=1:", np.percentile(arr, 0, axis=1))
# Keep same dimensions
print("\nUsing keepdims=True")
print("50th Percentile with keepdims:\n", np.percentile(arr, 50, axis=1, keepdims=True))
print("0th Percentile with keepdims:\n", np.percentile(arr, 0, axis=1, keepdims=True))
Output:
50th Percentile (Median) along axis=1: [17. 15. 4.] 0th Percentile (Min) along axis=1: [12. 6. 1.] Using keepdims=True 50th Percentile with keepdims: [[17.] [15.] [ 4.]] 0th Percentile with keepdims: [[12.] [ 6.] [ 1.]]
Setting keepdims=True preserves the original array’s dimensionality, useful for aligning results in matrix computations.
Example 4: Multiple Percentiles at Once
import numpy as np
arr = np.array([10, 20, 30, 40, 50, 60, 70])
percentiles = [10, 25, 50, 75, 90]
print("Array:", arr)
print("Percentiles:", np.percentile(arr, percentiles))
Output:
Array: [10 20 30 40 50 60 70] Percentiles: [16. 25. 40. 55. 64.]
You can compute multiple percentile values in a single call by passing a list of percentile values.
Summary
numpy.percentile() computes the nth percentile of a dataset.
Works on multi-dimensional arrays and supports flattening and broadcasting.
Use axis, keepdims, and interpolation options for flexible data summarization.
Useful for statistical analysis, outlier detection, and machine learning preprocessing.
Frequently Asked Questions
What is the difference between percentile and quantile in NumPy?
Both functions serve similar purposes. np.percentile() expects values between 0–100, while np.quantile() expects values between 0–1. For example, the 25th percentile equals the 0.25 quantile.
Can numpy.percentile() handle NaN values?
No, numpy.percentile() does not ignore NaNs by default. Use numpy.nanpercentile() to compute percentiles while ignoring NaN values.
How do interpolation methods affect the result?
The method or interpolation parameter defines how values between indices are estimated. For example, ‘linear’ interpolates, ‘lower’ picks the lower neighbor, and ‘higher’ picks the upper neighbor.
How can I calculate multiple percentiles at once?
You can pass a list of percentiles to np.percentile(). For example: np.percentile(arr, [25, 50, 75]) returns the 25th, 50th, and 75th percentiles.