繁体   English   中英

NumPy - 用权重在二维数组列上向量化 bincount

[英]NumPy - Vectorizing bincount over 2D array column wise with weights

我一直在查看此处此处的解决方案,但没有看到如何将其应用到我的结构中。

我有 3 个数组:一个(M, N)的零,和(P,)的索引(一些重复)和一个(P, N)的值。

我可以用一个循环来完成它:

# a: (M, N)
# b: (P, N)
# ix: (M,)
for i in range(N):
    a[:, i] += np.bincount(ix, weights=b[:, i], minlength=M)

我还没有看到任何以这种方式或使用weights关键字使用索引的示例。 我知道我需要将所有内容都放入一维数组中以对其进行矢量化,但是我正在努力弄清楚如何实现这一点。

基本思想与那些链接帖子中详细讨论的相同,即创建一个2D数组,每个要处理的“一维数据”都有偏移量(在这种情况下是每个列)。 所以,考虑到这些,我们最终会得到这样的东西 -

# Extent of bins per col
n = ix.max()+1

# 2D bins for per col processing
ix2D = ix[:,None] + n*np.arange(b.shape[1])

# Finally use bincount with those 2D bins as flattened and with
# flattened b as weights. Reshaping is needed to add back into "a".
a[:n] += np.bincount(ix2D.ravel(), weights=b.ravel(), minlength=n*N).reshape(N,-1).T

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM