简体   繁体   English

使用相同掩码的 ndarray 的布尔掩码内部维度

[英]boolean masking inner dimensions of ndarray using same mask

I have a 3d ndarray and I want to remove all values of lower dimensions given the same mask efficiently and cleanly.我有一个 3d ndarray,我想有效和干净地删除给定相同掩码的所有较低维度的值。 Hopefully code below explains what I'm looking for better:希望下面的代码可以更好地解释我正在寻找的内容:

arr   = np.random.randint(0,3, (5, 3,4))
bmask = arr[0] > 0 

#######
#Naive way
########
result = []
for i in range(arr.shape[0]):
    result.append(arr[i][bmask])
result = np.stack(result)

####### 
# Better Way
#######
better_way = ???????


assert np.all(result == better_way)

您可以在第一维使用冒号,其余使用bmask ,例如:

better_way = arr[:, bmask]

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

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