简体   繁体   中英

Boolean Indexing n-D Numpy Array

I could not apply bool values into given data:

import numpy as np

bool_values = np.array([False, False, True, False, True])
data = np.array([[11.0, 22.0, 0.0, 44.0, 55.0],
                 [10.0, 20.0, 30.0, 40.0, 50.0]])

expected_answer = [[0,55], [30,50]]

I tried it as:

expected_answer = data[bool_values]

I think you wanted to do this:

In [28]:
bool_values = np.array([False, False, True, False, True])
data = np.array([[11.0, 22.0, 0.0, 44.0, 55.0],
 [10.0, 20.0, 30.0, 40.0, 50.0]])
data[:,bool_values]

Out[28]:
array([[  0.,  55.],
       [ 30.,  50.]])

The above applies the mask to every row

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