- Builtin min, max, sum
- Statistics module mean, median, mode, stdev, variance
- Soft error caused by key=abs
- Statistics on a list of precision decimals
- Statistics on a list of fractions
Summarizing numeric list data using built-in methods
Py3: Min, max and sum
nums = [-1,5,2,-7,3,6,2,5,8,5,-4,3,-8,3,5,-1] # min, max nums_min = min(nums) #= -8 nums_max = max(nums) #= 8 # sum, number of elements nums_sum = sum(nums) #= 26 nums_len = len(nums) #= 16
Notes: Min, max and sum of lists
List data can be sorted into custom order. If only the min and max values are needed - then built-in methods and provide quick access to the values. There is a summation function for numbers too. To find average, the sum can be divided by the length of the sequence.
Simple stats for numeric lists
Py3: Statistics module
import statistics as st nums = [-1,5,2,-7,3,6,2,5,8,5,-4,3,-8,3,5,-1] # calculate statistical parameters nums_mean = st.mean(nums) #= 1.625 nums_medn = st.median(nums) #= 3.0 nums_mode = st.mode(nums) #= 5 nums_stdv = st.stdev(nums) #= 4.6457... nums_vars = st.variance(nums) #= 21.5833...
Notes: Mean, median, mode and variance
The statistics module in the standard library can operate on numeric lists to provide basic statistical measures for data. Version 3.8 also adds quantiles, geometric mean, and multimodes where multiple values occur at similar frequencies.
Multi-valued key match chooses first item.
Py3: Min, max using absolute values
nums = [-1,5,2,-7,3,6,2,5,8,5,-4,3,-8,3,5,-1] # compute absolute valued min and max key_min = min(nums, key=abs) #= -1 key_max = max(nums, key=abs) #= 8 abs_min = min(*map(abs, nums)) #= 1 abs_max = max(*map(abs, nums)) #= 8 # key = [1,5,2,7,3,6,2,5,8,5,4,3,8,3,5,1] # calculating absolute valued statistics nums = [-1,1] key_min = min(nums, key=abs) #= -1 key_max = max(nums, key=abs) #= -1 nums = [1,-1] key_min = min(nums, key=abs) #= 1 abs_min = min(*map(abs, nums)) #= 1
Notes: Custom keys and care
The min and max methods provide support for custom keys like sort. However care needs to be taken in using them as keys. Here the abs function creates a redundancy for min and max. The better way is to convert to absolute values and not to use a key.
Statistics for high precision decimals.
Py3: Decimal statistics
import decimal as dc import statistics as st txt = '1.2, 1.4, 1.9, 2.4, 1.7, 1.9, 2.1, 1.0' nums = [dc.Decimal(t) for t in txt.split(',')] # statistics on decimal object dc_min = min(nums) #= 1.0 dc_max = max(nums) #= 2.4 dc_sum = sum(nums) #= 13.6 dc_mean = st.mean(nums) #= 1.7 dc_stdv = st.stdev(nums) #= 0.4720...
Notes: Working with decimals
Just like integers and floats, the statistics module gracefully handles high precision decimal numbers.
Fractional and rational number statistics.
Py3: Fraction statistics
import fractions as fr import statistics as st frc_tup = [(3,7), (1,4), (2,5), (3,7), (1,2)] nums = [fr.Fraction(n, d) for n,d in frc_tup] # statistics with fractions dc_min = min(nums) #= 1/4 dc_max = max(nums) #= 1/2 dc_sum = sum(nums) #= 281/140 dc_mean = st.mean(nums) #= 281/700 dc_stdv = st.stdev(nums) #= 0.09236...
Notes: Fractions module working with stats
Fractions expressed as numerator and denominators are processed by the stats module and the values are mostly returned as rational fractions. A few cases like stdev and variance cannot maintain a rational fraction format.