A percentile is a value below which a certain percentage of data falls. For example, the 25th percentile is the value below which 25% of the data lies.
Click to reveal answer
beginner
What does the function np.percentile() do?
np.percentile() calculates the value below which a given percentage of data in an array falls. It helps find specific percentiles like median (50th percentile) or quartiles.
Click to reveal answer
beginner
How do you find the median of an array arr using np.percentile()?
Use np.percentile(arr, 50). The 50th percentile is the median, the middle value of the data.
Click to reveal answer
intermediate
What is the difference between the 25th and 75th percentiles?
The 25th percentile (Q1) is the value below which 25% of data falls, and the 75th percentile (Q3) is the value below which 75% of data falls. The difference between them is called the interquartile range (IQR), showing data spread.
Click to reveal answer
intermediate
How does np.percentile() handle multi-dimensional arrays?
np.percentile() can calculate percentiles along a specified axis using the axis parameter. This lets you find percentiles for rows or columns separately.
Click to reveal answer
What does np.percentile(arr, 90) return?
AThe average of the data in <code>arr</code>
BThe value below which 90% of the data in <code>arr</code> falls
CThe maximum value in <code>arr</code>
DThe value above which 90% of the data falls
✗ Incorrect
np.percentile(arr, 90) returns the 90th percentile, meaning 90% of data is below this value.
Which percentile corresponds to the median?
A50th percentile
B25th percentile
C75th percentile
D100th percentile
✗ Incorrect
The median is the 50th percentile, the middle value of the data.
How do you calculate the interquartile range (IQR) using percentiles?
What parameter do you use in np.percentile() to calculate percentiles along columns of a 2D array?
Aaxis=2
Baxis=1
Caxis=None
Daxis=0
✗ Incorrect
Use axis=0 to calculate percentiles down columns (each column separately).
If you want the 0th percentile of data, what value does np.percentile() return?
AMinimum value
BMaximum value
CMean value
DMedian value
✗ Incorrect
The 0th percentile is the smallest value in the data.
Explain what a percentile is and how you can use np.percentile() to find the median and quartiles in a dataset.
Think about how percentiles split data into parts and how np.percentile helps find those split points.
You got /4 concepts.
Describe how to calculate the interquartile range (IQR) using np.percentile() and why it is useful.
IQR is the difference between two key percentiles that capture the middle half of data.
You got /4 concepts.
Practice
(1/5)
1. What does the function np.percentile() calculate in a dataset?
easy
A. The value below which a given percentage of data falls
B. The average value of the dataset
C. The total sum of all data points
D. The number of data points in the 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 function np.percentile() returns this value for the given percentage.
Final Answer:
The value below which a given percentage of data falls -> Option A
Quick Check:
Percentile = value below % data [OK]
Hint: Percentile = cutoff value for given percent [OK]
Common Mistakes:
Confusing percentile with mean or sum
Thinking percentile counts data points
Mixing percentile with total dataset size
2. Which of the following is the correct syntax to find the 25th percentile of a numpy array data?
easy
A. np.percentile(25, data)
B. np.percentile(data, 25)
C. np.percentile(data, percentile=25)
D. np.percentile(25%)
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 uses np.percentile(data, 25). Others have wrong argument order or syntax.
Final Answer:
np.percentile(data, 25) -> Option B
Quick Check:
np.percentile(array, percentile) [OK]
Hint: First argument data, second percentile number [OK]
Common Mistakes:
Swapping arguments order
Using keyword 'percentile' incorrectly
Adding % sign inside function
3. Given the array arr = np.array([10, 20, 30, 40, 50]), what is the output of np.percentile(arr, 40)?
medium
A. 20.0
B. 30.0
C. 26.0
D. 40.0
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.0
Final Answer:
26.0 -> Option C
Quick Check:
40th percentile = 26.0 [OK]
Hint: Percentile index = p/100*(n-1), interpolate if needed [OK]
Common Mistakes:
Picking nearest data point without interpolation
Using wrong index calculation
Confusing percentile with quartile
4. What is wrong with this code snippet to find the 90th percentile of data?
np.percentile(data, percentile=90)
medium
A. The data array must be sorted before calling np.percentile()
B. The percentile value must be between 0 and 1, not 90
C. The function should be called as data.percentile(90)
D. The keyword argument 'percentile' is invalid; percentile should be positional
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 D
Quick Check:
Percentile argument is positional [OK]
Hint: Percentile must be second positional argument [OK]
Common Mistakes:
Using invalid keyword argument
Assuming percentile is fraction (0-1)
Trying to sort data manually
5. You have a dataset 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?
hard
A. threshold = np.percentile(data, 75)
filtered = data[data <= threshold]
B. threshold = np.percentile(data, 0.75)
filtered = data[data <= threshold]
C. threshold = np.percentile(data, 75)
filtered = data[data < threshold]
D. threshold = np.percentile(data, 75)
filtered = data[data > threshold]
Solution
Step 1: Calculate 75th percentile threshold
Use np.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, so filtered = data[data <= threshold].