Lesson ยท 15 min
Numbers that describe data
A few numbers for many values
A list of 15 trip times is hard to read. A few numbers describe it: the number of values, the smallest value, the largest value, and a value in the middle.
The mean
The mean is the average: the total divided by the number of values. The module statistics has a function for it.
import statistics
times = [10, 12, 14, 20]
print(statistics.mean(times)) # 14
The median
Sort the values. The median is the value in the middle. When the number of values is even, the median is the mean of the two values in the middle.
print(statistics.median([10, 12, 14, 20, 90])) # 14
print(statistics.median([10, 12, 14, 20])) # 13.0
Half of the values are below the median, and half are above it.
One strange value moves the mean
Add one very large value to the list.
times = [10, 12, 14, 20, 600]
print(statistics.mean(times)) # 131.2
print(statistics.median(times)) # 14
The mean is now 131.2, and no value in the list is near that number. The median is still 14. One strange value moves the mean a long way, and it moves the median very little. A value that is far from all the others is an outlier.
Calculate both numbers. When the mean and the median are far apart, look for outliers, and find out what they are. An outlier can be a mistake in the data. It can also be a real event that you must report.
The smallest and the largest value
min and max show the two ends of the data. Look at them before you trust a mean. A trip of 0 minutes or of 600 minutes tells you something about the data.
An empty list
statistics.mean([]) stops with an error, because an empty list has no mean. A function that describes data must decide what it returns for an empty list. Check the length first.
Your turn
main.py loads the 15 trips of trips.csv. Do the tasks in order.
Tasks
5 to do
Task 1, not checked yet. Make a list
minuteswith the minutes of each trip. Make a variablemean_minuteswith the mean of the list, rounded to 1 digit after the decimal point.Task 2, not checked yet. Make a variable
median_minuteswith the median ofminutes.Task 3, not checked yet. The mean is almost 3 times the median, because one bike came back after 610 minutes. Make a list
usualwith the values ofminutesthat are 180 or less. Make a variablemean_usualwith its mean, rounded to 1 digit.Task 4, not checked yet. Write a function
describe(values). It returns a dictionary with the keyscount,min,max,meanandmedian. The mean has 1 digit after the decimal point. For an empty list, it returns{"count": 0}.Task 5, not checked yet. Write a function
outliers(values, factor). It returns the list of the values that are larger thanfactortimes the median. For an empty list, it returns an empty list.
Task 1 of 5
Make a list minutes with the minutes of each trip. Make a variable mean_minutes with the mean of the list, rounded to 1 digit after the decimal point.