numpy的bincount函数的反函数 [英] Inverse of numpy's bincount function
本文介绍了numpy的bincount函数的反函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个整数计数c
的数组,如何将其转换为整数inds
的数组,以使np.all(np.bincount(inds) == c)
为真?
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 = 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.
当前解决方法:
def inverse_bincount(c):
return np.array(list(chain.from_iterable([i]*n for i,n in enumerate(c))))
推荐答案
np.repeat(np.arange(c.size), c)
这篇关于numpy的bincount函数的反函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文