numpy array IndexError: 'index out of bound' when a mask is inverted or negated

numpy array IndexError: 'index out of bound' when a mask is inverted or negated

我有 2 个数组,一个是掩码,另一个是标签:

两个数组的形状相同:

(Pdb) L.shape
(178, 201, 101)
(Pdb) MASK.shape
(178, 201, 101)

当它到达这一行时:

L[~MASK] = 0 
IndexError: 'index 255 is out of bounds for axis 0 with size 178'

它显示了一个错误,我找不到任何原因。你能帮我解决这个问题吗?

尝试:

L[np.logical_not(MASK)]

您使用的 ~(波浪号)运算符是按位补码运算符,而不是逻辑否定运算符。