0
0
NumPydata~20 mins

Percentiles with np.percentile() in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Percentile Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.percentile() on a simple array
What is the output of this code snippet?
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
result = np.percentile(arr, 40)
print(result)
A26.0
B20.0
C30.0
D40.0
Attempts:
2 left
💡 Hint
Percentile 40 means the value below which 40% of data falls.
data_output
intermediate
2:00remaining
Percentiles of a 2D array along axis 0
What is the output of np.percentile(arr, 50, axis=0) for the given 2D array?
NumPy
import numpy as np
arr = np.array([[1, 3, 5], [2, 4, 6], [3, 5, 7]])
result = np.percentile(arr, 50, axis=0)
print(result)
A[3. 5. 7.]
B[1. 3. 5.]
C[2. 4. 6.]
D[2.5 4.5 6.5]
Attempts:
2 left
💡 Hint
50th percentile is the median along the specified axis.
visualization
advanced
3:00remaining
Visualizing percentiles on a dataset
Which option correctly describes the plot generated by this code?
NumPy
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
data = np.random.normal(0, 1, 1000)
percentiles = np.percentile(data, [25, 50, 75])
plt.hist(data, bins=30, alpha=0.5)
for p in percentiles:
    plt.axvline(p, color='r', linestyle='--')
plt.show()
AHistogram with red dashed lines at the minimum, median, and maximum values
BScatter plot of data points with red dashed lines at quartiles
CLine plot of data with red dashed lines at mean and standard deviation
DHistogram with red dashed lines at the 25th, 50th, and 75th percentiles
Attempts:
2 left
💡 Hint
Percentiles 25, 50, and 75 correspond to quartiles.
🧠 Conceptual
advanced
2:00remaining
Understanding interpolation in np.percentile()
If np.percentile() is called with interpolation='nearest' on the array [1, 2, 3, 4], what is the 30th percentile value?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.percentile(arr, 30, interpolation='nearest')
print(result)
A1
B2
C4
D3
Attempts:
2 left
💡 Hint
Nearest interpolation picks the closest data point to the percentile rank.
🔧 Debug
expert
2:00remaining
Error raised by incorrect np.percentile() usage
What error does this code raise?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = np.percentile(arr, 110)
print(result)
AValueError: percentile must be in the range [0, 100]
BIndexError: index out of bounds
CNo error, returns 3
DTypeError: unsupported operand type(s)
Attempts:
2 left
💡 Hint
Percentiles must be between 0 and 100 inclusive.