Indexing multidimensional boolean arrays in numpy
I have two 2D arrays, one of numbers and one of booleans:
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 to index an array and return a 2D array with 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.]])
source to share
Your command is returning a 1D array as it is impossible to accomplish without (a) destroying the column structure that is usually needed. for example, 7
in the requested release it originally belonged to column 7 and is now in column 0; and (b) numpy
does not support afike, supports a large array of different sizes in the same dimension. I mean numpy cannot have an array, the first three lines of which are 5 long, the fourth line of 4 length, etc. - all lines (the same size) must have the same length.
I think the best result you could hope for is an array of arrays (not a 2D array). This is how I built it, although there are probably better ways that I am not aware 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)
source to share
EDIT: An array of lists is being created
np.array([val[idx[i]].tolist() for i,val in enumerate(x) if len(val[idx[i]].tolist()) > 0])
array([[1.0, 1.0, 1.0, 1.0, 1.0],
[2.0, 2.0, 2.0, 2.0, 2.0],
[3.0, 3.0, 3.0, 3.0, 3.0],
[4.0, 4.0, 4.0, 4.0],
[5.0, 5.0, 5.0],
[6.0],
[7.0]], dtype=object)
source to share