0
0
SciPydata~20 mins

Percentiles and quantiles in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Percentiles and Quantiles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of percentile calculation with scipy
What is the output of this code snippet that calculates the 75th percentile of the data array?
SciPy
import numpy as np
from scipy import stats

data = np.array([10, 20, 30, 40, 50])
result = stats.scoreatpercentile(data, 75)
print(result)
A40.0
B45.0
C42.5
D37.5
Attempts:
2 left
💡 Hint
Remember that the 75th percentile is the value below which 75% of the data fall. scipy's scoreatpercentile uses linear interpolation by default.
data_output
intermediate
2:00remaining
Number of elements below the 60th percentile
Given this dataset, how many elements are below the 60th percentile calculated using scipy?
SciPy
import numpy as np
from scipy import stats

data = np.array([5, 15, 25, 35, 45, 55, 65])
percentile_60 = stats.scoreatpercentile(data, 60)
count_below = np.sum(data < percentile_60)
print(count_below)
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Calculate the 60th percentile value first, then count how many data points are strictly less than that value.
visualization
advanced
3:00remaining
Visualizing quantiles with a boxplot
Which option produces a boxplot showing the quartiles of the data array using matplotlib and scipy?
SciPy
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

data = np.array([12, 7, 3, 15, 8, 10, 6, 9, 11, 14])

q1 = stats.scoreatpercentile(data, 25)
q2 = stats.scoreatpercentile(data, 50)
q3 = stats.scoreatpercentile(data, 75)

plt.boxplot(data)
plt.title(f"Quartiles: Q1={q1}, Median={q2}, Q3={q3}")
plt.show()
AA boxplot with whiskers showing min and max, and a box from 7 to 12 with median at 9
BA boxplot with whiskers showing min and max, and a box from 6 to 15 with median at 10
CA boxplot with whiskers showing min and max, and a box from 7.25 to 11.75 with median at 9.5
DA boxplot with whiskers showing min and max, and a box from 3 to 15 with median at 9
Attempts:
2 left
💡 Hint
Check the quartile values calculated by scipy and compare with the boxplot box edges and median.
🧠 Conceptual
advanced
2:00remaining
Understanding interpolation methods in percentile calculation
Which statement correctly describes the effect of changing the interpolation method in scipy's percentile calculation?
AUsing 'nearest' interpolation always returns a data point from the dataset as the percentile value.
BUsing 'higher' interpolation returns the smallest data point in the dataset.
CUsing 'lower' interpolation averages the two closest data points around the percentile.
DUsing 'linear' interpolation returns the closest data point without averaging.
Attempts:
2 left
💡 Hint
Think about how each interpolation method chooses the percentile value relative to the data points.
🔧 Debug
expert
2:00remaining
Identify the error in percentile calculation code
What error will this code raise when executed?
SciPy
import numpy as np
from scipy import stats

data = np.array([1, 2, 3, 4, 5])
result = stats.scoreatpercentile(data, 110)
print(result)
AValueError: percentile must be in the range [0, 100]
BIndexError: index out of bounds
CTypeError: unsupported operand type(s)
DNo error, outputs 5
Attempts:
2 left
💡 Hint
Percentiles must be between 0 and 100 inclusive.