[Numpy-discussion] logical masking, wrong length mask
John Hunter
jdh2358@gmail....
Wed May 28 09:19:11 CDT 2008
I just spent a while tracking down a bug in my code, and found out the
problem was numpy was letting me get away with using a logical mask of
smaller size than the array it was masking.
In [19]: x = np.random.rand(10)
In [20]: x
Out[20]:
array([ 0.72253623, 0.8412243 , 0.12835194, 0.01595052, 0.62208366,
0.57229259, 0.46099861, 0.44114786, 0.23687212, 0.89507604])
In [21]: y = np.random.rand(11)
In [22]: mask = x>.5
In [23]: x[mask]
Out[23]: array([ 0.72253623, 0.8412243 , 0.62208366, 0.57229259,
0.89507604])
In [24]: y[mask]
Out[24]: array([ 0.13440315, 0.83610533, 0.75390136, 0.79046615,
0.34776165])
In [25]: mask
Out[25]: array([ True, True, False, False, True, True, False,
False, False, True], dtype=bool)
I initially thought line 24 below should raise an error, or coerce
True to 1 and False to 0 and give me either y[0] or y[1] accordingly,
but neither appear to be happening. Instead, I appear to be getting
y[:len(mask)][mask] .
In [27]: y[:10][mask]
Out[27]: array([ 0.13440315, 0.83610533, 0.75390136, 0.79046615,
0.34776165])
In [28]: y[mask]
Out[28]: array([ 0.13440315, 0.83610533, 0.75390136, 0.79046615,
0.34776165])
In [29]: len(y)
Out[29]: 11
In [30]: len(mask)
Out[30]: 10
In [31]: y[:len(mask)][mask]
Out[31]: array([ 0.13440315, 0.83610533, 0.75390136, 0.79046615,
0.34776165])
In [32]: np.__version__
Out[32]: '1.2.0.dev5243'
Bug or feature?
More information about the Numpy-discussion
mailing list