使用2d布尔值从3d numpy数组中提取1d数组

时间:2020-11-27 21:19:09

Say I have a 3d numpy array:

说我有一个3d numpy数组:

i, j, k = 10, 3, 4

arr = np.arange(120).reshape(i, j, k)

and a 2d boolean array:

和一个2d布尔数组:

mask = np.random.random((j, k)) > 0.5

n = mask.sum()

I want to be able to extract the 1d arrays from arr along its 1st dimension which correspond with the True values of mask. The result should have shape, (i, n). How could this be done?

我希望能够从arr沿着它的第一维提取1d数组,这些数组对应于mask的True值。结果应该有形状,(i,n)。怎么可以这样做?

I pulling up some old code and for some reason I was doing arr[mask] but this gives a shape of (n, k) (I'm not sure why) and a warning:

我提取了一些旧的代码,由于某种原因,我正在做arr [mask],但这给出了(n,k)的形状(我不知道为什么)和警告:

VisibleDeprecationWarning: boolean index did not match indexed array along dimension 0; dimension is 10949 but corresponding boolean dimension is 11

1 个解决方案

#1


1  

Simply mask along the last two axes -

简单地沿最后两个轴掩盖 -

arr[:,mask]

#1


1  

Simply mask along the last two axes -

简单地沿最后两个轴掩盖 -

arr[:,mask]