5.1 Frequency Distribution
For a unique count of each data value in a dataset use the syntax:
import numpy as np
x = [x1,x2,x3,...]
np.unique(x,return_counts=True)
Note: Dataset will be entered in Python list as x = [x1,x2,x3,...]
Example: The dataset below is the waiting time, in minutes, of customers at a fast food restaurant. Write a program to construct a frequency of unique values.
5, 3, 4, 1, 5, 3, 5, 2, 3, 2, 5, 4, 4, 6, 4, 3, 4, 1, 2, 5
- Click
+ Code - Type
import numpy as np - Type
x = [5, 3, 4, 1, 5, 3, 5, 2, 3, 2, 5, 4, 4, 6, 4, 3, 4, 1, 2, 5] - Type
np.unique(x,return_counts=True) - Click
playbutton - Two array output is displayed - unique value and frequency
import numpy as np
x = [5, 3, 4, 1, 5, 3, 5, 2, 3, 2, 5, 4, 4, 6, 4, 3, 4, 1, 2, 5]
np.unique(x,return_counts=True)
Show expected output
(array([1, 2, 3, 4, 5, 6]), array([2, 3, 4, 5, 5, 1]))Interpretation: In the above output, the first array is the list of unique elements and the second array is counts (frequencies) for each unique element. For example, the value 1 from the first array has a frequency of 2 from the second array, etc.
| value | frequency |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
| 5 | 5 |
| 6 | 1 |
Note: We will use the variable name x to store the data values in a Python List.
Example: 20 students were asked if they were registered to vote. Their responses are listed below. Write a program to construct a frequency table.
NO, NO, YES, YES, NO, YES, NO, NO, YES, NO, YES, NO, NO, YES, YES, YES, YES, NO, YES, YES
import numpy as np
x = ['NO', 'NO', 'YES', 'YES', 'NO', 'YES', 'NO', 'NO', 'YES', 'NO',
'YES', 'NO', 'NO', 'YES', 'YES', 'YES', 'YES', 'NO', 'YES', 'YES']
np.unique(x,return_counts=True)
Show expected output
(array(['NO', 'YES'], dtype='<U3'), array([ 9, 11]))Interpretation: from the above output:
| value | frequency |
|---|---|
| NO | 9 |
| YES | 11 |
Note: Qualitative data values are entered in 'quotes'. The dtype “<U3†refers to a Unicode string of 3 characters.
For making a frequency distribution with k classes (bins), use the following code:
import numpy as np
x = [x1,x2,x3,...]
np.histogram(x,bins=k,density=False)
If density=True the frequencies are replaced by relative frequencies.
Example: The dataset below is test scores of 20 students in a Math class. Write a program to construct a frequency distribution with 5 classes(bins).
33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100
- Click
+ Code - Type
import numpy as np - Type
x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74, 78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100] -
np.histogram(x, bins=5,density=False) - Two array output - frequencies and classes
import numpy as np
x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,
78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100]
np.histogram(x, bins=5,density=False)
Show expected output
(array([ 2, 5, 9, 4, 11]),
array([ 33. , 46.4, 59.8, 73.2, 86.6, 100. ]))Interpretation from the above output:
| classes | freq |
|---|---|
| 33 - 46.4 | 2 |
| 46.4 - 59.8 | 5 |
| 59.8 - 73.2 | 9 |
| 73.2 - 86.6 | 4 |
| 86.6 - 100 | 11 |
key: 33-46.4 is [33,46.4) or
Example: The dataset below is test scores of 20 students in a Math class. Write a program to construct a frequency distribution with 5 classes(bins). Save the two arrays as freq and class_limits
33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100
- Click
+ Code - Type
import numpy as np - Type
x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74, 78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100] - Type
freq, class_limit = np.histogram(x, bins=5,density=False) - Type
print(class_limit) - Type
print(freq)
import numpy as np
x = [33, 42, 49, 49, 53, 55, 55, 61, 63, 67, 68, 68, 69, 69, 72, 73, 74,
78, 80, 83, 88, 88, 88, 90, 92, 94, 94, 94, 94, 96, 100]
freq, class_limit = np.histogram(x, bins=5,density=False)
print(class_limit)
print(freq)
Show expected output
[ 33. 46.4 59.8 73.2 86.6 100. ]
[ 2 5 9 4 11]Interpretation from the above output:
| classes | freq |
|---|---|
| 33 - 46.4 | 2 |
| 46.4 - 59.8 | 5 |
| 59.8 - 73.2 | 9 |
| 73.2 - 86.6 | 4 |
| 86.6 - 100 | 11 |
key: 33-46.4 is [33,46.4) or
Example: Use the US Senators age (as of 2021) dataset given below to construct a relative frequency distribution with 5 bins (classes).
81,81,80,80,80,80,78,78,78,77,77,75,75,74,74,74,73,72,72,71,71,71,71,70,70,70,70,68,68,68,68,68,67,67,67,66,66,65,65,65,65,64,64,64,64,64,64,63,63,63,63,62,62,62,62,62,61,61,61,60,60,59,59,59,59,59,59,59,59,58,58,57,57,56,56,55,54,54,54,54,54,53,53,52,52,52,52,52,51,50,49,48,46,45,44,43,43,43,42,41
import numpy as np
x = [81,81,80,80,80,80,78,78,78,77,77,75,75,74,74,74,73,72,72,71,71,71,
71,70,70, 70,70,68,68,68,68,68,67,67,67,66,66,65,65,65,65,64,64,64,
64,64,64,63,63,63,63,62,62,62,62,62,61,61,61,60,60,59,59,59,59,59,
59,59,59,58,58,57,57,56,56,55,54,54,54,54,54,53,53,52,52,52,52,52,
51,50,49,48,46,45,44,43,43,43,42,41]
freq, bin_edges = np.histogram(x, bins=5,density=True)
print(bin_edges)
print(freq)
Show expected output
[41. 49. 57. 65. 73. 81.]
[0.01125 0.0225 0.04 0.03 0.02125]Interpretation from the above output:
| classes | rel-freq |
|---|---|
| 41 - 49 | 0.01125 |
| 49 - 57 | 0.0225 |
| 57 - 65 | 0.04 |
| 65 - 73 | 0.03 |
| 73 - 81 | 0.02125 |
key: 41 - 49 is [41,49)
0.04=4% of the Senators are aged between 75 to 65
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.