I am running a very simple example -
a = np.array([1,2,3,4,5])
mask= np.array([True, False])
a[mask]
This produces IndexError: boolean index did not match indexed array along dimension 0; dimension is 5 but corresponding boolean dimension is 2.
The confusion arises from my understanding (or lack thereof) of this part of Numpy doc: -
If
obj.ndim == x.ndim, x[obj]returns a 1-dimensional array filled with the elements ofxcorresponding to theTruevalues ofobj. The search order will be row-major, C-style. IfobjhasTruevalues at entries that are outside of the bounds ofx, then an index error will be raised. Ifobjis smaller thanxit is identical to filling it withFalse. (emphasis mine)
I thought the mask array would be transformed to [True, False, False, False, False] but seemingly that is not the case. Also, both a and mask have same ndim value, so why would error message say a has dimension 5 but boolean dimension is 2.
What am I missing? How do I interpret the doc?