Say I have the array:
>>> import numpy as np
>>> myarr = np.array([[1],[2],[3],[2]])
and I want to use logical indexing to return the sub-array where myarr is not equal to 2.
>>> subarr = myarr[myarr != 2]
>>> print subarr
[1 3]
Thus, myarr.shape = (4,1) but subarr.shape = (2,).
Why is the size of dimension 2 being changed when the logical indexing doesn’t affect dimension 2? I could easily just reshape the output, but if I need to use logical indexing often in my code, I don’t want to have to always account for extra dimensions (i.e. dimensions that are obviously not relevant to the logical slice I am doing) that may have changed.
This (N,1) –> (K,) example is especially bothersome, since any logical slice of a something-by-one array must also be a something-by-one array.
I feel like there must be a cleaner way to have the NumPy slice operations leave trivial array dimensions alone. Any suggestions?
In numpy indexing with a boolean array is the same as indexing with array.nonzero(). Because array.nonzero() returns two sets of indices for a 2darray, you are in fact indexing on both the first and the second dimension:
What you really want is:
Which you can get by:
or: