Bird
Raised Fist0
SciPydata~10 mins

Percentiles and quantiles in SciPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the 50th percentile of the data array using scipy.

SciPy
from scipy import stats
import numpy as np
data = np.array([10, 20, 30, 40, 50])
percentile_50 = stats.scoreatpercentile(data, [1])
print(percentile_50)
Drag options to blanks, or click blank then click option'
A25
B50
C75
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using 25 or 75 instead of 50 for the median percentile.
Confusing percentile value with data values.
2fill in blank
medium

Complete the code to calculate the 0.25 quantile (25th percentile) of the data using scipy's percentile function.

SciPy
from scipy import stats
import numpy as np
data = np.array([5, 15, 25, 35, 45])
quantile_25 = stats.scoreatpercentile(data, [1])
print(quantile_25)
Drag options to blanks, or click blank then click option'
A0.25
B0.75
C25
D75
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0.25 instead of 25 for the percentile argument.
Mixing up 25th and 75th percentiles.
3fill in blank
hard

Fix the error in the code to correctly calculate the 75th percentile using scipy.

SciPy
from scipy import stats
import numpy as np
data = np.array([2, 4, 6, 8, 10])
percentile_75 = stats.scoreatpercentile(data, [1])
print(percentile_75)
Drag options to blanks, or click blank then click option'
A75
B0.75
C7.5
D0.25
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0.75 instead of 75.
Using a data value instead of percentile value.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

SciPy
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for the value.
Using < instead of > in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 4 characters.

SciPy
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
result = { [1]: [2] for word in words if len(word) [3] 4 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of word.upper() for keys.
Using < instead of > in the condition.

Practice

(1/5)
1. What does the 90th percentile represent in a dataset?
easy
A. The average of the top 90% of data
B. The value below which 90% of the data falls
C. The value above which 90% of the data falls
D. The middle value of the dataset

Solution

  1. Step 1: Understand percentile definition

    A percentile indicates the value below which a given percentage of observations fall.
  2. Step 2: Apply to 90th percentile

    The 90th percentile means 90% of data points are below this value.
  3. Final Answer:

    The value below which 90% of the data falls -> Option B
  4. Quick Check:

    Percentile = value below % data [OK]
Hint: Percentile = value below that percent of data [OK]
Common Mistakes:
  • Confusing percentile with average
  • Thinking percentile is value above percentage
  • Mixing percentile with median
2. Which of the following is the correct way to find the 25th percentile using scipy.stats?
easy
A. stats.quantile(data, 25)
B. stats.percentile(data, 0.25)
C. stats.mquantiles(data, prob=0.25)
D. stats.scoreatpercentile(data, 25)

Solution

  1. Step 1: Recall scipy function for percentile

    The function to find percentile is stats.scoreatpercentile with arguments (data, percentile).
  2. Step 2: Check options

    stats.scoreatpercentile(data, 25) uses stats.scoreatpercentile(data, 25), which is correct for 25th percentile.
  3. Final Answer:

    stats.scoreatpercentile(data, 25) -> Option D
  4. Quick Check:

    Use scoreatpercentile for percentiles [OK]
Hint: Use stats.scoreatpercentile(data, percentile) [OK]
Common Mistakes:
  • Using wrong function names like percentile or quantile
  • Passing 0.25 instead of 25 for percentile
  • Confusing quantiles with percentiles
3. Given the data array [10, 20, 30, 40, 50], what is the output of stats.scoreatpercentile(data, 40)?
medium
A. 26.0
B. 22.0
C. 30.0
D. 20.0

Solution

  1. Step 1: Understand percentile calculation

    40th percentile means the value below which 40% of data lies. Data sorted: [10, 20, 30, 40, 50].
  2. Step 2: Calculate 40th percentile using interpolation

    Position = (40/100)*(n-1) = 0.4*4 = 1.6. Interpolate between data[1]=20 and data[2]=30: 20 + 0.6*(30-20) = 20 + 6 = 26.0.
  3. Final Answer:

    26.0 -> Option A
  4. Quick Check:

    Interpolation gives 26.0 [OK]
Hint: Percentile position = p/100*(n-1), interpolate values [OK]
Common Mistakes:
  • Choosing nearest data point without interpolation
  • Using index p instead of p/100*(n-1)
  • Confusing percentile with median
4. The code below is intended to find the quartiles of data using scipy but raises an error. What is the error?
import scipy.stats as stats
data = [5, 10, 15, 20, 25]
quartiles = stats.mstats.mquantiles(data, prob=[0.25, 0.5, 0.75])
print(quartiles)
medium
A. mquantiles requires numpy array, not list
B. prob argument should be a single float, not a list
C. No error, code runs correctly
D. mquantiles is not in scipy.stats.mstats

Solution

  1. Step 1: Check function location and usage

    stats.mstats.mquantiles is a valid function and accepts prob as a list for multiple quantiles.
  2. Step 2: Check input data type

    mquantiles accepts list or array-like data, so list input is valid.
  3. Final Answer:

    No error, code runs correctly -> Option C
  4. Quick Check:

    mquantiles accepts list and prob list [OK]
Hint: mquantiles accepts list and prob list [OK]
Common Mistakes:
  • Assuming mquantiles only accepts numpy arrays
  • Thinking prob must be a single float
  • Confusing function location in scipy
5. You have a dataset of exam scores: [55, 70, 65, 80, 90, 85, 75]. You want to divide the scores into four equal groups (quartiles) using scipy. Which code snippet correctly computes the quartiles?
hard
A. stats.mstats.mquantiles(scores, prob=[0.25, 0.5, 0.75])
B. stats.scoreatpercentile(scores, [25, 50, 75])
C. np.percentile(scores, [0.25, 0.5, 0.75])
D. stats.mstats.mquantiles(scores, prob=[25, 50, 75])

Solution

  1. Step 1: Identify correct function for quartiles

    Quartiles are quantiles at 25%, 50%, and 75%. stats.mstats.mquantiles accepts prob as fractions between 0 and 1.
  2. Step 2: Check options for correct prob format

    stats.mstats.mquantiles(scores, prob=[0.25, 0.5, 0.75]) uses prob=[0.25, 0.5, 0.75], which is correct. stats.scoreatpercentile(scores, [25, 50, 75]) uses scoreatpercentile but with a list, which is invalid. np.percentile(scores, [0.25, 0.5, 0.75]) uses numpy but with fractions instead of percentages. stats.mstats.mquantiles(scores, prob=[25, 50, 75]) uses prob with values >1, which is invalid.
  3. Final Answer:

    stats.mstats.mquantiles(scores, prob=[0.25, 0.5, 0.75]) -> Option A
  4. Quick Check:

    mquantiles prob values are fractions [OK]
Hint: Use mquantiles with prob as fractions (0.25, 0.5, 0.75) [OK]
Common Mistakes:
  • Passing percent values instead of fractions to prob
  • Using scoreatpercentile with list of percentiles
  • Confusing numpy percentile input format
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