python - numpy.bincounts 的反转?

标签 python numpy

我正在尝试创建一个 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/

相关文章:

使用不同列表中的相同索引获取最大值的 Pythonic 方法

Python 不太难,只是在变量方面遇到困难

python - Django 表单和在 View /页面之间传递数据

Python scikit-learn : Cannot clone object. .. 因为构造函数似乎没有设置参数

opencv - 将具有图像数据的 numpy 数组转换为 CvMat

python - 写入文件时如何舍入 numpy 堆叠数组?

python - 如何将 pandas 数据透视表转换为 JSON

python - 将 vispy 与 pyqt5 和 uic 集成

python - 下面的Python表达式是如何工作的?

python - 根据间隙大小更改 numpy 数组中的间隙