简体   繁体   中英

Multidimensional boolean indexing into multidimensional array

I am trying to use a multi-dimensional boolean index into a multi-dimensional array. Here is a simple version of what I am trying to do.

import numpy as np
pf=np.array([[[-67.  ,-20.35, 1],
              [-46.35, 33.25, 2],
              [  1   ,  2   , 1],
              [  4   ,  4   , 4]],
             [[-64.  ,-70.35, 8],
              [-46.35, 33.25, 7],
              [  3   ,  7   , 8],
              [  9   ,  3   , 2]]])
booly=np.array([[False, True, False, False],
                [False, False, False, True]])

I would like to use my boolean array to select the true values from my initial (2,4,3) array to obtain the final (2,3) array without a loop:

truth=np.array([[-46.35, 33.25, 2],
                [  9   ,  3   , 2]])

I have tried and failed. Thanks guys.

numpy supports boolean indexing :

res = pf[booly]
print(res)
# [[-46.35  33.25   2.  ]
# [  9.     3.     2.  ]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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