FAQ: Quartiles - Q1 and Q3

This community-built FAQ covers the “Q1 and Q3” exercise from the lesson “Quartiles”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Q1 and Q3

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I tried to use the numpy percentile function to find the quartiles, but I’m not getting the correct answers. What am I doing wrong?

import numpy as np

dataset_one = [50, 10, 4, -3, 4, -20, 2]
dataset_two = [24, 20, 1, 45, -15, 40]

#Define the first and third quartile of both datasets here:

dataset_one_q1 = np.percentile(dataset_one,25)
# output is -0.5, correct answer is 3

dataset_one_q3 = np.percentile(dataset_one,75)
# output is 7.0, correct answer is 10

dataset_two_q1 = np.percentile(dataset_two,25)
#output is 5.75, correct answer is 1

dataset_two_q3 = np.percentile(dataset_two,75)
#output is 36.0, correct answer is 40

Thank you!

1 Like

It is because numpy.percentile uses a different method than the one described in this exercise.

The documentation of numpy.percentile has the following Notes:

Given a vector V of length N , the q-th percentile of V is the value q/100 of the way from the minimum to the maximum in a sorted copy of V . The values and distances of the two nearest neighbors as well as the interpolation parameter will determine the percentile if the normalized ranking does not match the location of q exactly. This function is the same as the median if q=50 , the same as the minimum if q=0 and the same as the maximum if q=100 .

The default setting for the interpolation parameter is linear.

For example, consider dataset_two. Since the length is 6, 0th, 20th, 40th, 60th, 80th, and 100th percentiles are exactly determined:

0th: -15
20th: 1
40th: 20
60th: 24
80th: 40
100th: 45

Other percentiles are interpolated linearly. For example, 25th falls between 20th and 40th, so the 25th percentile will be

1 + (20-1) * fraction.

Here, fraction = (25-20) / (40-20) = 0.25, so

1 + (20 -1 ) * 0.25 = 5.75.

We may find it easier to understand by drawing the graph as follows: