0
0
NumPydata~5 mins

Percentiles with np.percentile() in NumPy

Choose your learning style9 modes available
Introduction

Percentiles help us understand the position of a value in a list compared to others. Using np.percentile() lets us find these positions easily.

To find the value below which a certain percentage of data falls, like the 25th percentile (first quartile).
To understand the spread of exam scores and see what score separates the top 10% from the rest.
To detect outliers by checking values at very low or very high percentiles.
To summarize data distribution without looking at every number.
Syntax
NumPy
np.percentile(data, percentile, axis=None, interpolation='linear')

data is your list or array of numbers.

percentile is a number between 0 and 100 showing which percentile to find.

Examples
Finds the 50th percentile (median) of the list.
NumPy
np.percentile([10, 20, 30, 40, 50], 50)
Finds the 25th percentile (first quartile) of the list.
NumPy
np.percentile([1, 2, 3, 4, 5], 25)
Finds the 50th percentile for each column in a 2D array.
NumPy
np.percentile([[10, 20], [30, 40]], 50, axis=0)
Sample Program

This code finds key percentiles of exam scores to understand their distribution.

NumPy
import numpy as np

# Sample data: exam scores
scores = [55, 70, 65, 90, 85, 75, 60]

# Find the 25th, 50th, and 75th percentiles
p25 = np.percentile(scores, 25)
p50 = np.percentile(scores, 50)
p75 = np.percentile(scores, 75)

print(f"25th percentile: {p25}")
print(f"50th percentile (median): {p50}")
print(f"75th percentile: {p75}")
OutputSuccess
Important Notes

Percentiles split data into 100 equal parts.

If the exact percentile position is between two data points, np.percentile() interpolates by default.

Summary

Percentiles show the relative standing of values in data.

np.percentile() is an easy way to calculate them in Python.

Use percentiles to summarize data and find thresholds.