Python - 掩盖多维

use*_*754 0 python numpy mask

我想掩盖一些数组的值.阵列是3D,掩模是2D.

我想掩饰所有的coordonates方向frametemperature_reshape.shape[0].

我尝试了以下循环:

for i in range(frametemperature_reshape.shape[0]):
    frames_BPnegl = np.ma.array(frametemperature_reshape[i,:,:], mask=mask2)
Run Code Online (Sandbox Code Playgroud)

ali*_*i_m 6

您可以针对3D阵列广播 2D蒙版,以便沿第3维扩展其大小,而不会在内存中实际复制它:

import numpy as np

x = np.random.randn(10, 20, 30)
mask = np.random.randn(10, 20) > 0

# broadcast `mask` along the 3rd dimension to make it the same shape as `x`
_, mask_b = np.broadcast_arrays(x, mask[..., None])

xm  = np.ma.masked_array(x, mask_b)
Run Code Online (Sandbox Code Playgroud)

  • 是的,这不是我经常使用的功能,因为您几乎总是可以隐式广播。老实说,我认为masked_array构造函数应该在内部针对该数组广播掩码-我可能会考虑发出拉取请求。 (2认同)