简体   繁体   中英

using boolean array for indexing in numpy for 2D arrays

I use boolean indexing to select elements from a numpy array as

x = y[t<tmax]

where ta numpy array with as many elements as y. My question is how can I do the same with 2D numpy arrays? I tried

x = y[t<tmax][t<tmax]

This does not seem to work however since it seems to select first the rows and then complains that the second selection has the wrong dimension.

IndexError: boolean index did not match indexed array along dimension 0; dimension is 50 but corresponding boolean dimension is 200
#

Here is an example

print(x2D[x1D<3])

The second print statement produces an error similar to the error shown above. I use

[[1 2 3]
 [1 2 3]]

I get

[[1 2]
 [1 2]]

but I want

 [[1 2] [1 2]] 
In [28]: x1D = np.array([1,2,3], np.int32) 
    ...: x2D = np.array([[1,2,3],[1,2,3],[1,2,3]], np.int32) 

The 1d mask:

In [29]: x1D<3                                                                                               
Out[29]: array([ True,  True, False])

applied to the 1d array (same size):

In [30]: x1D[_]                                                                                              
Out[30]: array([1, 2], dtype=int32)

applied to the 2d it selects 2 rows:

In [31]: x2D[_29]                                                                                            
Out[31]: 
array([[1, 2, 3],
       [1, 2, 3]], dtype=int32)

It can be used again to select columns - but note the : place holder for the row index:

In [32]: _[:, _29]                                                                                           
Out[32]: 
array([[1, 2],
       [1, 2]], dtype=int32)

If we generate an indexing array from that mask, we can do the indexing with one step:

In [37]: idx = np.nonzero(x1D<3)                                                                             
In [38]: idx                                                                                                 
Out[38]: (array([0, 1]),)
In [39]: x2D[idx[0][:,None], idx[0]]                                                                         
Out[39]: 
array([[1, 2],
       [1, 2]], dtype=int32)

An alternate way of writing this '2d' indexing:

In [41]: x2D[ [[0],[1]], [[0,1]] ]                                                                           
Out[41]: 
array([[1, 2],
       [1, 2]], dtype=int32)

ix_ is a convenient tool for tweaking the indexing dimensions:

In [42]: x2D[np.ix_(idx[0], idx[0])]                                                                         
Out[42]: 
array([[1, 2],
       [1, 2]], dtype=int32)

Or passing the boolean mask to ix_ :

In [44]: np.ix_(_29, _29)                                                                                    
Out[44]: 
(array([[0],
        [1]]), array([[0, 1]]))
In [45]: x2D[np.ix_(_29, _29)]                                                                               
Out[45]: 
array([[1, 2],
       [1, 2]], dtype=int32)

Writing In[32] so it's close to to your try:

In [46]: x2D[x1D<3][:, x1D<3]                                                                                
Out[46]: 
array([[1, 2],
       [1, 2]], dtype=int32)

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