Why Does The Order Of Dimensions Change With Boolean Indexing?
Solution 1:
According the boolean indexing
section of the docs
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing
Combining multiple Boolean indexing arrays or a Boolean with an integer indexing array can best be understood with the obj.nonzero() analogy.
ind = np.nonzero(val)[0]
# array([ 0, 1, 2, ...., 39], dtype=int32)
M[0, :, ind].shape # (40,20)
So now we go to the section about combining advanced and basic indexing http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing
This is a case of the form: x[arr1, :, arr2]
in the first case, the dimensions resulting from the advanced indexing operation come first in the result array, and the subspace dimensions after that.
So the 0
and ind
part produce a (40,)
selection, while the :
in the middle produces a (20,)
. By putting the :
part last, the resulting dimension is (40,20)
. Basically it does this because there is an ambiguity in this indexing style, so it consistently opts to put the slice part at the end.
A way of selecting these values and maintain the shape you want (more or less) is to use np.ix_
to generate a tuple of indices.
M[np.ix_([0],np.arange(20),ind)].shape # (1, 20, 40)
You can use np.squeeze
to remove the initial 1
dimension.
This use of ix_
is illustrated at the end of the 'purely index array indexing' section
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing
Post a Comment for "Why Does The Order Of Dimensions Change With Boolean Indexing?"