【发布时间】:2010-09-22 23:15:55
【问题描述】:
统计累加器允许执行增量计算。例如,为了计算在任意时间给定的数字流的算术平均值,可以创建一个对象来跟踪给定项目的当前数量n 及其总和sum。当请求均值时,对象只返回sum/n。
这样的累加器允许您进行增量计算,即当给定一个新数字时,您无需重新计算整个总和和计数。
可以为其他统计数据编写类似的累加器(参见boost library,用于 C++ 实现)。
您将如何在 Python 中实现累加器? The code I came up with 是:
class Accumulator(object):
"""
Used to accumulate the arithmetic mean of a stream of
numbers. This implementation does not allow to remove items
already accumulated, but it could easily be modified to do
so. also, other statistics could be accumulated.
"""
def __init__(self):
# upon initialization, the numnber of items currently
# accumulated (_n) and the total sum of the items acumulated
# (_sum) are set to zero because nothing has been accumulated
# yet.
self._n = 0
self._sum = 0.0
def add(self, item):
# the 'add' is used to add an item to this accumulator
try:
# try to convert the item to a float. If you are
# successful, add the float to the current sum and
# increase the number of accumulated items
self._sum += float(item)
self._n += 1
except ValueError:
# if you fail to convert the item to a float, simply
# ignore the exception (pass on it and do nothing)
pass
@property
def mean(self):
# the property 'mean' returns the current mean accumulated in
# the object
if self._n > 0:
# if you have more than zero items accumulated, then return
# their artithmetic average
return self._sum / self._n
else:
# if you have no items accumulated, return None (you could
# also raise an exception)
return None
# using the object:
# Create an instance of the object "Accumulator"
my_accumulator = Accumulator()
print my_accumulator.mean
# prints None because there are no items accumulated
# add one (a number)
my_accumulator.add(1)
print my_accumulator.mean
# prints 1.0
# add two (a string - it will be converted to a float)
my_accumulator.add('2')
print my_accumulator.mean
# prints 1.5
# add a 'NA' (will be ignored because it cannot be converted to float)
my_accumulator.add('NA')
print my_accumulator.mean
# prints 1.5 (notice that it ignored the 'NA')
有趣的设计问题出现了:
- 如何制作蓄能器 线程安全?
- 如何安全删除 项目?
- 如何以某种方式构建架构 允许其他统计数据 轻松插入(统计工厂)
【问题讨论】:
-
python 的累加器库的状态是什么?我想将它与 PyTables 一起使用。
标签: python oop statistics accumulator