在python中为生成器计算平均值 [英] compute mean in python for a generator

查看:120
本文介绍了在python中为生成器计算平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些统计工作,我有一个(大)随机数集合来计算均值,我想与生成器一起使用,因为我只需要计算均值,所以我不需要需要存储数字.

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 mean, so I don't need to store the numbers.

问题在于,如果您将numpy.mean传递给生成器,它就会中断.我可以编写一个简单的函数来执行我想要的操作,但是我想知道是否有适当的内置方法来做到这一点?

The problem is that numpy.mean breaks if you pass it a generator. I can write a simple function to do what I want, but I'm wondering if there's a proper, built-in way to do this?

如果我能说"sum(values)/len(values)"会很好,但是len对生成器不起作用,将已经消耗的值相加.

It would be nice if I could say "sum(values)/len(values)", but len doesn't work for genetators, and sum already consumed values.

这是一个例子:

import numpy 

def my_mean(values):
    n = 0
    Sum = 0.0
    try:
        while True:
            Sum += next(values)
            n += 1
    except StopIteration: pass
    return float(Sum)/n

X = [k for k in range(1,7)]
Y = (k for k in range(1,7))

print numpy.mean(X)
print my_mean(Y)

这两个都给出相同的,正确的答案,购买my_mean不适用于列表,而numpy.mean不适用于生成器.

these both give the same, correct, answer, buy my_mean doesn't work for lists, and numpy.mean doesn't work for generators.

我真的很喜欢使用发电机的想法,但是这样的细节似乎破坏了事情.

I really like the idea of working with generators, but details like this seem to spoil things.

推荐答案

只需对代码进行简单的更改就可以同时使用两者.生成器本可以互换用于for循环中的列表.

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.0
    for v in values:
        Sum += v
        n += 1
    return Sum / n

这篇关于在python中为生成器计算平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆