Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multidimensional boolean array indexing in numpy

I have a two 2D arrays, one of numbers and one of boolean values:

x = 
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.],
       [ 5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.],
       [ 6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.],
       [ 7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.],
       [ 8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.],
       [ 9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.,  9.]])

idx = 
array([[False, False, False, False, False, False, False, False, False, False],
       [False,  True,  True,  True,  True,  True, False, False, False, False],
       [False,  True,  True,  True,  True,  True, False, False, False, False],
       [False,  True,  True,  True,  True,  True, False, False, False, False],
       [False, False, False,  True,  True,  True,  True, False, False, False],
       [False, False, False, False,  True,  True,  True, False, False, False],
       [False, False, False, False, False, False,  True, False, False, False],
       [False, False, False, False, False, False, False,  True, False, False],
       [False, False, False, False, False, False, False, False, False, False],
       [False, False, False, False, False, False, False, False, False, False]], dtype=bool)

When I index the array it returns a 1D array:

x[idx]
array([ 1.,  1.,  1.,  1.,  1.,  2.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,
    3.,  3.,  4.,  4.,  4.,  4.,  5.,  5.,  5.,  6.,  7.])

How do I index the array and return a 2D array with the expected output:

x[idx]
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  4.],
       [ 5.,  5.,  5.],
       [ 6.],
       [ 7.]])
like image 956
camdenl Avatar asked Sep 29 '22 16:09

camdenl


People also ask

Does NumPy arrays support boolean indexing?

We can also index NumPy arrays using a NumPy array of boolean values on one axis to specify the indices that we want to access. This will create a NumPy array of size 3x4 (3 rows and 4 columns) with values from 0 to 11 (value 12 not included).

Can multi dimensional arrays be indexed?

Indexing multi-dimensional arraysMulti-dimensional arrays are indexed in GAUSS the same way that matrices are indexed, using square brackets [] . Scanning above, you can see that the value of the element at the intersection of the third row and second column of x1 is 8.

How do you use boolean indexing in NumPy?

You can index specific values from a NumPy array using another NumPy array of Boolean values on one axis to specify the indices you want to access. For example, to access the second and third values of array a = np. array([4, 6, 8]) , you can use the expression a[np.

Can NumPy arrays be indexed?

ndarrays can be indexed using the standard Python x[obj] syntax, where x is the array and obj the selection. There are different kinds of indexing available depending on obj: basic indexing, advanced indexing and field access.


1 Answers

Your command returns a 1D array since it's impossible to fulfill without (a) destroying the column structure, which is usually needed. e.g., the 7 in your requested output originally belonged to column 7, and now it's on column 0; and (b) numpy does not, afaik, support high dimensional array with different sizes on the same dimension. What I mean is that numpy can't have an array whose first three rows are of length 5, 4th row of length 4, etc. - all the rows (same dimension) need to have the same length.

I think the best result you could hope for is an array of arrays (and not a 2D array). This is how I would construct it, though there are probably better ways I don't know of:

In [9]: from itertools import izip
In [11]: array([r[ridx] for r, ridx in izip(x, idx) if ridx.sum() > 0])
Out[11]: 
array([array([ 1.,  1.,  1.,  1.,  1.]), array([ 2.,  2.,  2.,  2.,  2.]),
       array([ 3.,  3.,  3.,  3.,  3.]), array([ 4.,  4.,  4.,  4.]),
       array([ 5.,  5.,  5.]), array([ 6.]), array([ 7.])], dtype=object)
like image 141
Korem Avatar answered Oct 05 '22 08:10

Korem