compute mean in python for a generator

后端 未结 10 2226
遇见更好的自我
遇见更好的自我 2021-01-01 21:58

I\'m doing some statistics work, I have a (large) collection of random numbers to compute the mean of, I\'d like to work with generators, because I just need to compute the

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 22:36

    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.0
    
        for x in data:
            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.

提交回复
热议问题