Percentiles with np.percentile() in NumPy - Time & Space Complexity
We want to understand how the time needed to find percentiles changes as the data size grows.
How does the work increase when we have more numbers to analyze?
Analyze the time complexity of the following code snippet.
import numpy as np
data = np.random.rand(1000)
percentile_25 = np.percentile(data, 25)
percentile_50 = np.percentile(data, 50)
percentile_75 = np.percentile(data, 75)
This code finds the 25th, 50th, and 75th percentiles of a list of 1000 random numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the array to find the percentile positions.
- How many times: Sorting happens once per percentile call, but sorting dominates the cost.
As the number of data points grows, sorting takes more time, but not in a simple doubling way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 operations |
| 100 | About 700 operations |
| 1000 | About 10,000 operations |
Pattern observation: The work grows faster than the input size but slower than its square, roughly like n times log n.
Time Complexity: O(n log n)
This means the time to find percentiles grows a bit faster than the number of data points, mainly because of sorting.
[X] Wrong: "Finding a percentile is just picking a value, so it takes the same time no matter the data size."
[OK] Correct: To find the percentile, the data usually needs to be sorted or partially sorted, which takes more time as data grows.
Understanding how sorting affects percentile calculations helps you explain performance in data analysis tasks clearly and confidently.
"What if we used a method that finds percentiles without full sorting? How would the time complexity change?"
Practice
np.percentile() calculate in a dataset?Solution
Step 1: Understand percentile meaning
A percentile indicates the value below which a certain percentage of data lies.Step 2: Connect to np.percentile()
The functionnp.percentile()returns this value for the given percentage.Final Answer:
The value below which a given percentage of data falls -> Option AQuick Check:
Percentile = value below % data [OK]
- Confusing percentile with mean or sum
- Thinking percentile counts data points
- Mixing percentile with total dataset size
data?Solution
Step 1: Recall np.percentile() parameters
The first argument is the data array, the second is the percentile number.Step 2: Check each option
np.percentile(data, 25) correctly usesnp.percentile(data, 25). Others have wrong argument order or syntax.Final Answer:
np.percentile(data, 25) -> Option BQuick Check:
np.percentile(array, percentile) [OK]
- Swapping arguments order
- Using keyword 'percentile' incorrectly
- Adding % sign inside function
arr = np.array([10, 20, 30, 40, 50]), what is the output of np.percentile(arr, 40)?Solution
Step 1: Understand percentile calculation
40th percentile means value below which 40% of data lies. Data sorted: [10,20,30,40,50]Step 2: Calculate 40th percentile
Position = (40/100)*(5-1) = 1.6. Interpolate between arr[1]=20 and arr[2]=30: 20 + 0.6*(30-20) = 26.0Final Answer:
26.0 -> Option CQuick Check:
40th percentile = 26.0 [OK]
- Picking nearest data point without interpolation
- Using wrong index calculation
- Confusing percentile with quartile
data?
np.percentile(data, percentile=90)
Solution
Step 1: Check np.percentile() argument rules
The function expects the percentile as the second positional argument, not as a keyword 'percentile'.Step 2: Validate other options
Percentile can be 90 (not 0.9), function is not a method of array, and sorting is handled internally.Final Answer:
The keyword argument 'percentile' is invalid; percentile should be positional -> Option DQuick Check:
Percentile argument is positional [OK]
- Using invalid keyword argument
- Assuming percentile is fraction (0-1)
- Trying to sort data manually
data = np.array([5, 7, 8, 12, 15, 18, 22, 27, 30]). You want to find the value below which 75% of the data falls and then filter the dataset to only include values below this percentile. Which code correctly does this?Solution
Step 1: Calculate 75th percentile threshold
Usenp.percentile(data, 75)to get the value below which 75% data lies.Step 2: Filter data including values equal to threshold
Use <= to include values equal to threshold, sofiltered = data[data <= threshold].Final Answer:
threshold = np.percentile(data, 75) filtered = data[data <= threshold] -> Option AQuick Check:
Percentile 75 + filter <= threshold [OK]
- Using 0.75 instead of 75 for percentile
- Filtering with < instead of <=
- Filtering with > instead of <= or <
Ready to prove it?
Pass the test and this pattern moves unknown
