我正在尝试获取浮点类型的 numpy 数组的 bincount:
w = np.array([0.1, 0.2, 0.1, 0.3, 0.5])
print np.bincount(w)
如何将 bincount() 用于浮点值而不是整数?
最佳答案
在使用bincount
之前,您需要使用numpy.unique
。否则你在计算什么是模棱两可的。对于 numpy 数组,unique
应该比 Counter 快得多。
>>> w = np.array([0.1, 0.2, 0.1, 0.3, 0.5])
>>> uniqw, inverse = np.unique(w, return_inverse=True)
>>> uniqw
array([ 0.1, 0.2, 0.3, 0.5])
>>> np.bincount(inverse)
array([2, 1, 1, 1])
关于python - 带有 float 的 Numpy bincount(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10119441/