Compute Mean In Python For A Generator
Solution 1:
In general if you're doing a streaming mean calculation of floating point numbers, you're probably better off using a more numerically stable algorithm than simply summing the generator and dividing by the length.
The simplest of these (that I know) is usually credited to Knuth, and also calculates variance. The link contains a python implementation, but just the mean portion is copied here for completeness.
def mean(data):
n = 0
mean = 0.0for x indata:
n += 1
mean += (x - mean)/n
if n < 1:
return float('nan')
else:
return mean
I know this question is super old, but it's still the first hit on google, so it seemed appropriate to post. I'm still sad that the python standard library doesn't contain this simple piece of code.
Solution 2:
Just one simple change to your code would let you use both. Generators were meant to be used interchangeably to lists in a for-loop.
def my_mean(values):
n =0
Sum = 0.0for v in values:
Sum += v
n += 1return Sum / n
Solution 3:
defmy_mean(values):
total = 0for n, v inenumerate(values, 1):
total += v
return total / n
print my_mean(X)
print my_mean(Y)
There is statistics.mean()
in Python 3.4 but it calls list()
on the input:
def mean(data):
if iter(data) isdata:
data = list(data)
n = len(data)
if n < 1:
raise StatisticsError('mean requires at least one data point')
return _sum(data)/n
where _sum()
returns an accurate sum (math.fsum()
-like function that in addition to float
also supports Fraction
, Decimal
).
Solution 4:
The old-fashioned way to do it:
defmy_mean(values):
sum, n = 0, 0for x in values:
sum += x
n += 1returnfloat(sum)/n
Solution 5:
One way would be
numpy.fromiter(Y, int).mean()
but this actually temporarily stores the numbers.
Post a Comment for "Compute Mean In Python For A Generator"