Advanced Python Development Course
Chapter
>
Level
Math Modules
Statistics Module
Objective
Walk down to the office and chart down some statistics data for the farm’s metrics using the statistics module.
Down the stairs from the attic there’s an office, in there files are stored holding important data for the farm. It would be a good idea to process that data and document it to project future growth of the farm. To do this we will implement the use of the statistics Module. Modules allow us to import tools such as functions, classes and the like allowing to expand pythons functionality. In order to set up a module you write import ahead of the name of the module, that’s right, you’ve been using a module to control the player this whole time!
import statistics
The module adds in several functions for calculating statistics, here are some that we’ll be using to process the statistics data in the office:
statistics.mean(): Calculates the average of a set of datastatistics.median(): Calculates the middle value of a set of datastatistics.median_low(): Calculates the lowest median value of a set of datastatistics.median_high(): Calculates the highest median value of a set of datastatistics.mode(): Calculates the central tendency of a set of data, in other words the value most present in the data sample.statistics.variance(): Calculates the variance of the values in a set of data. A high number means the values are spread out, a low number means the values are close together.
Start off by walking to the light X marks in front of filing cabinets , use the the read() function to check the files and read the data. There are three (3) reports to read in the field, create three (3) lists to store the data for each: weather_report , export_report and harvest_report . The data in each report holds six (6) values representing data pertaining to the first six (6) months of the year. Note and store the six (6) values in each list when you read them.
Once you’ve noted and stored the data in lists, walk to the dark X mark over the blue carpet. Create five variables named: mean , median , low , high and variance . These variables will house the value of the statistics functions we outlined previously. What we’re going to do is add the list of the report we would like to get statistics from, this will generate statistics for the data provided.
mean = statistics.mean( report list ) median = statistics.median( report list ) low = statistics.median_low( report list ) high = statistics.median_high( report list ) variance = statistics.variance( report list )
With this statistics data we will use the write() function to note the data in memos, the code editor will have the sequences pre-written. On the the blue carpet X mark, outline the variables for the export_report and write them down. On the the green carpet X mark, outline the variables for the harvest_report and write them down. On the the red carpet X mark, outline the variables for the weather_report and write them down. You must populate the variables for each report first before you write them to have the proper statistics data written down for each.
Once all the statistics data has been charted down, walk to the gold X mark and create a new list named group_report . On this list you will add the export_report and harvest_report in that order. This will create a nested list which can’t be used with the statistic functions, in order to remedy this use a List comprehension in order to flatten the nested list, like this: group_report= [item for sublist in group_report for item in sublist]
Set up two more variables: variance and mode with their respective statistics functions. Add the group_report list to the variance statistic function, and add the weather_report list to the mode statistic function, like this: variance = statistics.variance( group_report ) , mode = statistics.mode( weather_report ) . Execute the write() function set up in the code editor for these variables in order to complete the level.