假设我有一个布尔值的(m,)
-array a
和一个(n, m)
-array b
。对于b
的每一行b[i]
,我希望使用np.sum(a, where=b[i])
,它应该返回一个(n,)
-array。我可以执行以下操作:
a = np.array([1,2,3])
b = np.array([[True, False, False], [True, True, False], [False, True, True]])
c = np.array([np.sum(a, where=r) for r in b])
# c is [1,3,5]
但对我来说,这似乎很不雅观。我原本希望广播魔术能让你
c = np.sum(a, where=b)
# c is 9
工作,但显然,np.sum
然后对b
的行求和,这是我不想要的。在np.sum
(或任何ufunc.reduce)中,有没有一种固有的方法来实现所需的行为?
转载请注明出处:http://www.cxd6.com/article/20230512/2439711.html