【发布时间】:2012-05-28 01:08:48
【问题描述】:
我有大量的条目,每一个都是浮点数。这些数据x 可以通过迭代器访问。我需要使用10<y<=20、20<y<=50、...之类的选择对所有条目进行分类,其中y 是来自其他可迭代对象的数据。条目的数量远远超过选择的数量。最后我想要一个字典,比如:
{ 0: [all events with 10<x<=20],
1: [all events with 20<x<=50], ... }
或类似的东西。比如我在做:
for x, y in itertools.izip(variable_values, binning_values):
thebin = binner_function(y)
self.data[tuple(thebin)].append(x)
一般y 是多维的。
这很慢,是否有更快的解决方案,例如使用 numpy?我认为问题来自我正在使用的list.append 方法,而不是来自binner_function
【问题讨论】:
-
你看过
numpy.histogram()吗? docs.scipy.org/doc/numpy/reference/generated/… -
binner_function 必须是通用的吗?如果您发布了它的实现,我们也许可以编写一个更有效的版本,可能带有 dict/list 理解。
-
numpy.histogram不好:它只返回每个类别的计数
标签: python optimization numpy binning