7.1 Percentiles (Quantiles)
The p percentile is the value that is greater than (or equal to) p percent of the ordered data.
- quartile is equal to percentile
- quartile is equal to percentile
- quartile is equal to percentile
In Python percentiles are called quantiles
numpy syntax for percentile
import numpy as np
np.quantile(x,p)
Note: p is the percentile in decimals. For example, the 70th percentile has p value of 0.70, for 1st Quartile which is the same as 25th Percentile, p =.25
To display the 25th, 50th and 75th percentiles in one line, use the syntax
np.quantile(x,[.25,0.5,0.75])
- Click
+ Code - Type
import numpy as np - Type
x=[81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32, 52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19] - Type
np.quantile(x,.7) - Click
playbutton
import numpy as np
x=[81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32,
52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19]
np.quantile(x,.7)
Show expected output
50.599999999999994Interpretation: The 70th percentile is approximately 50.6
Example: The dataset below is scores of 20 students in a Math class.
81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32, 52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19
- Find the 70th percentile.
- Find the 83rd percentile.
- Find the 1st quartile.
- Find the 3rd quartile.
Note: These results could be different from other results that use a different method of computing percentiles.
import numpy as np
x=[81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32,
52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19]
np.quantile(x,[.70,.83,.25,.75])
Show expected output
array([50.6 , 79.07, 25.25, 52.75])Interpretation:
| Percentile | score |
|---|---|
| 70th | 50.60 |
| 83rd | 79.07 |
| 25th | 25.25 |
| 75th | 52.75 |
Example: The dataset below is scores of 20 students in a Math class.
81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32, 52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19
- Find the Interquartile range.
Note: These results could be different from other results that use a different method of computing percentiles.
import numpy as np
x=[81, 21, 53, 32, 80, 13, 26, 42, 35, 41, 14, 25, 87, 62, 46, 40, 32,
52, 85, 28, 17, 36, 83, 79, 18, 50, 17, 36, 36, 19]
np.quantile(x,[.70,.83,.25,.75])
IQR = np.quantile(x,.75)-np.quantile(x,.25) # IQR = Q3 - Q1
IQR
Show expected output
27.5Example: A random sample of 35 annual salaries of Animal Control employees of the City of Chicago is listed below. Write a numpy program to compute the 90th percentile, 2nd quartile and 10th percentile.
45288, 96096, 69468, 79872, 92520, 49968, 76248, 110076, 84324, 101496, 100668, 66864, 49968, 101496, 49968, 69468, 69468, 96096, 100716, 73380, 52044, 76248, 66864, 66864, 76848, 66864, 139392, 56280, 69468, 66864,73380, 87564, 49968, 49968, 76848
import numpy as np
x = [45288, 96096, 69468, 79872, 92520, 49968, 76248, 110076, 84324, 101496,
100668, 66864, 49968, 101496, 49968, 69468, 69468, 96096, 100716, 73380,
52044, 76248, 66864, 66864, 76848, 66864, 139392, 56280, 69468, 66864,
73380, 87564, 49968, 49968, 76848]
np.quantile(x,[0.90,0.50,0.10])
Show expected output
array([101184., 73380., 49968.])Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.