FAQ: Quantiles - Quantiles in NumPy

This community-built FAQ covers the “Quantiles in NumPy” exercise from the lesson “Quantiles”.

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

FAQs on the exercise Quantiles in NumPy

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!

import numpy as np

dataset = [5, 10, -20, 42, -9, 10]
ten_percent = np.quantile(dataset, 0.10)   # -14.5

For those who wondered why ten_percent holds the value -14.5, I would like to share my research about numpy.quantile(), based on the documentation.

The documentation has the following Notes:

Given a vector V of length N , the q-th quantile of V is the value q 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 quantile if the normalized ranking does not match the location of q exactly. This function is the same as the median if q=0.5 , the same as the minimum if q=0.0 and the same as the maximum if q=1.0 .

The default setting for the interpolation parameter is linear :

interpolation {‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’}
This optional parameter specifies the interpolation method to use when the desired quantile lies between two data points i < j :

  • linear: i + (j - i) * fraction , where fraction is the fractional part of the index surrounded by i and j .

Now consider dataset in the example code at the beginning. Since the length is 6, quantiles at 0%, 20%, 40%, 60%, 80%, and 100% are exactly determined:

0%: -20
20%: -9
40%: 5
60%: 10
80%: 10
100%: 42

Other quantiles are interpolated linearly. For example, 10% falls between 0% and 20%, so the quantile at 10% will be

-20 + (-9 - (-20)) * fraction.

Here, fraction = 10 / 20 = 0.5, so it is

-20 + (-9 - (-20)) * 0.5 = -14.5.

We may find it easier to understand by drawing the graph as follows:
numpy-quantile-example

2 Likes