逆numpy的bincount函数

时间:2022-04-17 22:01:24

Given an array of integer counts c, how can I transform that into an array of integers inds such that np.all(np.bincount(inds) == c) is true?

给定一个整数计数c的数组,如何将其转换为整数inds数组,使得np.all(np.bincount(inds)== c)为真?

For example:

例如:

>>> c = np.array([1,3,2,2])
>>> inverse_bincount(c)  # <-- what I need

array([0,1,1,1,2,2,3,3])

Context: I'm trying to keep track of the location of multiple sets of data, while performing computation on all of them at once. I concatenate all the data together for batch processing, but I need an index array to extract the results back out.

上下文:我试图跟踪多组数据的位置,同时对所有数据进行计算。我将所有数据连接在一起进行批处理,但我需要一个索引数组来提取结果。

Current workaround:

目前的解决方法:

def inverse_bincount(c):
  return np.array(list(chain.from_iterable([i]*n for i,n in enumerate(c))))

3 个解决方案

#1


11  

using numpy.repeat :

使用numpy.repeat:

np.repeat(np.arange(c.size), c)

#2


1  

no numpy needed :

不需要numpy:

c = [1,3,2,2]
reduce(lambda x,y: x + [y] * c[y], range(len(c)), [])

#3


1  

The following is about twice as fast on my machine than the currently accepted answer; although I must say I am surprised by how well np.repeat does. I would expect it to suffer a lot from temporary object creation, but it does pretty well.

以下在我的机器上的速度是目前接受答案的两倍;虽然我必须说我对np.repeat的表现感到惊讶。我希望它可以从临时对象创建中受到很大影响,但它确实很好。

import numpy as np
c = np.array([1,3,2,2])
p = np.cumsum(c)
i = np.zeros(p[-1],np.int)
np.add.at(i, p[:-1], 1)
print np.cumsum(i)

#1


11  

using numpy.repeat :

使用numpy.repeat:

np.repeat(np.arange(c.size), c)

#2


1  

no numpy needed :

不需要numpy:

c = [1,3,2,2]
reduce(lambda x,y: x + [y] * c[y], range(len(c)), [])

#3


1  

The following is about twice as fast on my machine than the currently accepted answer; although I must say I am surprised by how well np.repeat does. I would expect it to suffer a lot from temporary object creation, but it does pretty well.

以下在我的机器上的速度是目前接受答案的两倍;虽然我必须说我对np.repeat的表现感到惊讶。我希望它可以从临时对象创建中受到很大影响,但它确实很好。

import numpy as np
c = np.array([1,3,2,2])
p = np.cumsum(c)
i = np.zeros(p[-1],np.int)
np.add.at(i, p[:-1], 1)
print np.cumsum(i)