Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers?
-
Average is ambiguous - mode and median are also commonly-used averagesjtlz2– jtlz22018-06-11 08:13:46 +00:00Commented Jun 11, 2018 at 8:13
-
1Mode and median are other measures of central tendency. They are not averages. The mode is the most common value seen in a data set and is not necessarily unique. The median is the value that represents the center of the data points. As the question implies, there are a few different types of averages, but all are different from median and mode calculations. purplemath.com/modules/meanmode.htmJarom– Jarom2018-08-01 04:48:48 +00:00Commented Aug 1, 2018 at 4:48
-
@Jarom That link disagrees with you: 'Mean, median, and mode are three kinds of "averages"'Marcelo Cantos– Marcelo Cantos2019-02-07 03:39:47 +00:00Commented Feb 7, 2019 at 3:39
13 Answers
I am not aware of anything in the standard library. However, you could use something like:
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
>>> mean([1,2,3,4])
2.5
>>> mean([])
0.0
In numpy, there's numpy.mean().
7 Comments
[] is 0, which can be done by float(sum(l))/max(len(l),1).max?NumPy has a numpy.mean which is an arithmetic mean. Usage is as simple as this:
>>> import numpy
>>> a = [1, 2, 4]
>>> numpy.mean(a)
2.3333333333333335
9 Comments
Use statistics.mean:
import statistics
print(statistics.mean([1,2,4])) # 2.3333333333333335
It's available since Python 3.4. For 3.1-3.3 users, an old version of the module is available on PyPI under the name stats. Just change statistics to stats.
4 Comments
timeit("numpy.mean(vec)), timeit("sum(vec)/len(vec)") and timeit("statistics.mean(vec)") - the latter is slower than the others by a huge factor (>100 in some cases on my PC). This appears to be due to a particularly precise implementation of the sum operator in statistics, see PEP and Code. Not sure about the reason for the large performance difference between statistics._sum and numpy.sum, though.statistics.mean tries to be correct. It calculates correctly the mean of [1e50, 1, -1e50] * 1000.statistics.mean will also accept a generator expression of values, which all solutions that use len() for the divisor will choke on.statistics.fmean functionYou don't even need numpy or scipy...
>>> a = [1, 2, 3, 4, 5, 6]
>>> print(sum(a) / len(a))
3
6 Comments
from __future__ import division at the top of your programa = list()? The proposed code results in ZeroDivisionError.Use scipy:
import scipy;
a=[1,2,4];
print(scipy.mean(a));
Instead of casting to float you can do following
def mean(nums):
return sum(nums, 0.0) / len(nums)
or using lambda
mean = lambda nums: sum(nums, 0.0) / len(nums)
UPDATES: 2019-12-15
Python 3.8 added function fmean to statistics module. Which is faster and always returns float.
Convert data to floats and compute the arithmetic mean.
This runs faster than the mean() function and it always returns a float. The data may be a sequence or iterable. If the input dataset is empty, raises a StatisticsError.
fmean([3.5, 4.0, 5.25])
4.25
New in version 3.8.
Comments
If you're using python >= 3.8, you can use the fmean function introduced in the statistics module which is part of the standard library:
>>> from statistics import fmean
>>> fmean([0, 1, 2, 3])
1.5
It's faster than the statistics.mean function, but it converts its data points to float beforehand, so it can be less accurate in some specific cases.
You can see its implementation here
Comments
The proper answer to your question is to use statistics.mean. But for fun, here is a version of mean that does not use the len() function, so it (like statistics.mean) can be used on generators, which do not support len():
from functools import reduce
from operator import truediv
def ave(seq):
return truediv(*reduce(lambda a, b: (a[0] + b[1], b[0]),
enumerate(seq, start=1),
(0, 0)))
Comments
I always supposed avg is omitted from the builtins/stdlib because it is as simple as
sum(L)/len(L) # L is some list
and any caveats would be addressed in caller code for local usage already.
Notable caveats:
non-float result: in python2, 9/4 is 2. to resolve, use
float(sum(L))/len(L)orfrom __future__ import divisiondivision by zero: the list may be empty. to resolve:
if not L: raise WhateverYouWantError("foo") avg = float(sum(L))/len(L)
Comments
Others already posted very good answers, but some people might still be looking for a classic way to find Mean(avg), so here I post this (code tested in Python 3.6):
def meanmanual(listt):
mean = 0
lsum = 0
lenoflist = len(listt)
for i in listt:
lsum += i
mean = lsum / lenoflist
return float(mean)
a = [1, 2, 3, 4, 5, 6]
meanmanual(a)
Answer: 3.5