[英]NumPy - Vectorizing bincount over 2D array column wise with 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.