0
0
NumPydata~10 mins

Percentiles with np.percentile() in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Percentiles with np.percentile()
Start with data array
Choose percentile value p
Sort data in ascending order
Calculate rank index for p
Interpolate if needed
Return value at percentile p
The process takes a data array and a percentile value, sorts the data, finds the position for that percentile, interpolates if needed, and returns the percentile value.
Execution Sample
NumPy
import numpy as np

data = np.array([10, 20, 30, 40, 50])
result = np.percentile(data, 40)
print(result)
Calculate the 40th percentile of a simple numeric array.
Execution Table
StepActionData StateCalculationResult
1Start with data array[10, 20, 30, 40, 50]N/AN/A
2Choose percentile pp = 40N/AN/A
3Sort data[10, 20, 30, 40, 50]Already sortedN/A
4Calculate rank indexn=5rank = (p/100)*(n-1) = 0.4*4 = 1.6rank=1.6
5Interpolate between index 1 and 2values at 1=20, 2=3020 + 0.6*(30-20) = 20 + 6 = 2626.0
6Return percentile valueN/AN/A26.0
💡 Percentile value 26.0 returned after interpolation at rank 1.6
Variable Tracker
VariableStartAfter Step 4After Step 5Final
data[10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50]
pN/A404040
nN/A555
rankN/A1.61.61.6
resultN/AN/A26.026.0
Key Moments - 2 Insights
Why do we interpolate between two data points for percentile 40?
Because the rank 1.6 is not an integer index, np.percentile interpolates between the values at index 1 and 2 (20 and 30) to find the exact percentile value, as shown in execution_table step 5.
Why is the data sorted before calculating the percentile?
Percentiles depend on the order of data. Sorting ensures we can find the correct position for the percentile, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the rank calculated for the 40th percentile?
A1.6
B2.0
C0.4
D4.0
💡 Hint
Check the 'Calculate rank index' row in the execution_table.
At which step does interpolation happen in the execution table?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the step mentioning 'Interpolate' in the Action column.
If the percentile p was 50, what would be the rank index?
A3.0
B2.5
C2.0
D1.5
💡 Hint
Use the formula rank = (p/100)*(n-1) with n=5 and p=50.
Concept Snapshot
np.percentile(data, p) computes the p-th percentile of data.
Data is sorted first.
Rank index = (p/100)*(n-1).
If rank is not integer, interpolate between neighbors.
Returns the value at percentile p.
Full Transcript
This visual execution shows how np.percentile calculates the 40th percentile of a data array. First, the data is sorted. Then the rank index is calculated using the formula (p/100)*(n-1), where n is the number of data points. For p=40 and n=5, rank is 1.6. Since 1.6 is not an integer, interpolation happens between the values at index 1 and 2, which are 20 and 30. The interpolated value is 26.0, which is returned as the 40th percentile. Variables like data, p, n, rank, and result change as the steps progress. Key moments include understanding why sorting and interpolation are needed. The quizzes test understanding of rank calculation, interpolation step, and rank for a different percentile.