我正在尝试创建一个 Numpy 优化版本的逆 numpy.bincounts
.我意识到 bincounts
不是一对一的,所以我们来谈谈最简单的版本。
import numpy as np
def bincounts_inverse(counts):
list = []
dtype = np.min_scalar_type(counts.shape[0] - 1)
for bin, count in enumerate(counts):
ar = np.empty(count, dtype=dtype)
ar[:] = bin
list.append(ar)
return np.concatenate(list)
这可能是我目前对 Numpy 和 Python 的了解所能获得的最好的。当计数高且 bin 低时,它将非常快,但反之则慢。它是渐近最优的,但可能不是您能做的最好的。
有没有更快的方法来做到这一点?
这是一个示例输入/输出。
counts = np.array([3, 1, 0, 2, 5], np.uint8)
bincounts_inverse(counts) = np.array([0, 0, 0, 1, 3, 3, 4, 4, 4, 4, 4],
dtype=np.uint8)
最佳答案
bincount
的倒数是 repeat
-
np.repeat(np.arange(len(counts)), counts)
sample 运行-
In [22]: counts = np.array([3,0,2,1,0,2])
In [23]: list = []
...: dtype = np.min_scalar_type(counts.shape[0] - 1)
...: for bin, count in enumerate(counts):
...: ar = np.empty(count, dtype=dtype)
...: ar[:] = bin
...: list.append(ar)
...: out = np.concatenate(list)
In [24]: out
Out[24]: array([0, 0, 0, 2, 2, 3, 5, 5], dtype=uint8)
In [25]: np.repeat(np.arange(len(counts)), counts)
Out[25]: array([0, 0, 0, 2, 2, 3, 5, 5])
另一个使用非零索引,使用 sparsey counts
可能更有效 -
idx = np.flatnonzero(counts!=0)
out = np.repeat(idx, counts[idx])
关于python - numpy.bincounts 的反转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47798079/