我有大量条目,每个条目都是 float 。这些数据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 中获取作业的快速方法是使用 np.digitize
:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.digitize.html
您仍然需要将生成的作业分成几组。如果 x 或 y 是多维的,则必须首先展平数组。然后,您可以获得唯一的 bin 分配,然后与 np.where
结合迭代这些分配,将分配分成组。如果箱的数量远小于需要装箱的元素的数量,这可能会更快。
作为一个有点简单的例子,您需要针对您的特定问题进行调整/详细说明(但希望足以让您开始使用 numpy 解决方案):
In [1]: import numpy as np
In [2]: x = np.random.normal(size=(50,))
In [3]: b = np.linspace(-20,20,50)
In [4]: assign = np.digitize(x,b)
In [5]: assign
Out[5]:
array([23, 25, 25, 25, 24, 26, 24, 26, 23, 24, 25, 23, 26, 25, 27, 25, 25,
25, 25, 26, 26, 25, 25, 26, 24, 23, 25, 26, 26, 24, 24, 26, 27, 24,
25, 24, 23, 23, 26, 25, 24, 25, 25, 27, 26, 25, 27, 26, 26, 24])
In [6]: uid = np.unique(assign)
In [7]: adict = {}
In [8]: for ii in uid:
...: adict[ii] = np.where(assign == ii)[0]
...:
In [9]: adict
Out[9]:
{23: array([ 0, 8, 11, 25, 36, 37]),
24: array([ 4, 6, 9, 24, 29, 30, 33, 35, 40, 49]),
25: array([ 1, 2, 3, 10, 13, 15, 16, 17, 18, 21, 22, 26, 34, 39, 41, 42, 45]),
26: array([ 5, 7, 12, 19, 20, 23, 27, 28, 31, 38, 44, 47, 48]),
27: array([14, 32, 43, 46])}
有关处理展平和反展平 numpy 数组的信息,请参阅: http://docs.scipy.org/doc/numpy/reference/generated/numpy.unravel_index.html
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel_multi_index.html
关于python - 快速分类(分箱),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10686847/