Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand percentile meaning

    A percentile indicates the value below which a certain percentage of data lies.
  2. Step 2: Connect to np.percentile()

    The function np.percentile() returns this value for the given percentage.
  3. Final Answer:

    The value below which a given percentage of data falls -> Option A
  4. 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

  1. Step 1: Recall np.percentile() parameters

    The first argument is the data array, the second is the percentile number.
  2. Step 2: Check each option

    np.percentile(data, 25) correctly uses np.percentile(data, 25). Others have wrong argument order or syntax.
  3. Final Answer:

    np.percentile(data, 25) -> Option B
  4. 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

  1. Step 1: Understand percentile calculation

    40th percentile means value below which 40% of data lies. Data sorted: [10,20,30,40,50]
  2. 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
  3. Final Answer:

    26.0 -> Option C
  4. 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

  1. Step 1: Check np.percentile() argument rules

    The function expects the percentile as the second positional argument, not as a keyword 'percentile'.
  2. Step 2: Validate other options

    Percentile can be 90 (not 0.9), function is not a method of array, and sorting is handled internally.
  3. Final Answer:

    The keyword argument 'percentile' is invalid; percentile should be positional -> Option D
  4. 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

  1. Step 1: Calculate 75th percentile threshold

    Use np.percentile(data, 75) to get the value below which 75% data lies.
  2. Step 2: Filter data including values equal to threshold

    Use <= to include values equal to threshold, so filtered = data[data <= threshold].
  3. Final Answer:

    threshold = np.percentile(data, 75) filtered = data[data <= threshold] -> Option A
  4. Quick Check:

    Percentile 75 + filter <= threshold [OK]
Hint: Use percentile value and filter with <= threshold [OK]
Common Mistakes:
  • Using 0.75 instead of 75 for percentile
  • Filtering with < instead of <=
  • Filtering with > instead of <= or <
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard