Quantcast
Channel: Finding the average of a list - Stack Overflow
Viewing all articles
Browse latest Browse all 26

Answer by cottontail for Finding the average of a list

$
0
0

Unlike statistics.mean(), statistics.fmean() works for a list of objects with different numeric types. For example:

from decimal import Decimalimport statisticsdata = [1, 4.5, Decimal('3.5')]statistics.mean(data)     # TypeErrorstatistics.fmean(data)    # OK

This is because under the hood, mean() uses statistics._sum() which returns a data type to convert the mean into (and Decimal is not on Python's number hierarchy), while fmean() uses math.fsum() which just adds the numbers up (which is also much faster than built-in sum() function).

One consequence of this is that fmean() always returns a float (because averaging involves division) while mean() could return a different type depending on the number types in the data. The following example shows that mean() can return different types while for the same lists, fmean() returns 3.0, a float for all of them.

statistics.mean([2, Fraction(4,1)])   # Fraction(3, 1) <--- fractions.Fractionstatistics.mean([2, 4.0])             # 3.0            <--- floatstatistics.mean([2, 4])               # 3              <--- int

Also, unlike sum(data)/len(data), fmean() (and mean()) works not just on lists but on general iterables such as generators as well. This is useful, if your data is massive and/or you need to perform off-the-cuff filtering before computing the mean.

For example, if a list has NaN values averaging returns NaN. If you want to average the list while ignoring NaN values, you can filter out the NaN values and pass a generator to fmean:

data = [1, 2, float('nan')]statistics.fmean(x for x in data if x==x)     # 1.5

Note that numpy has a function (numpy.nanmean()) that does the same job.

import numpy as npnp.nanmean(data)                              # 1.5

Viewing all articles
Browse latest Browse all 26

Trending Articles