python - Bug or meant to be: numpy raises "ValueError: too many boolean indices" for repeated boolean indices -


i doing simulations in experimental cosmology, , encountered problem while working numpy arrays. i'm new numpy, not sure if i'm doing wrong or if it's bug. run:

enthought python distribution -- www.enthought.com version: 7.3-1 (32-bit)  python 2.7.3 |epd 7.3-1 (32-bit)| (default, apr 12 2012, 11:28:34)  [gcc 4.0.1 (apple inc. build 5493)] on darwin type "credits", "demo" or "enthought" more information. >>> import numpy np >>> t = np.arange(10) >>> t[t < 8][t < 5] traceback (most recent call last):   file "<stdin>", line 1, in <module> valueerror: many boolean indices >>>  

i expected return:

array([0, 1, 2, 3, 4]) 

since t[t < 8] should presumably treated ndarray?

the numpy documentation (http://docs.scipy.org/doc/numpy/user/basics.indexing.html) says boolean arrays indices:

as index arrays, returned copy of data, not view 1 gets slices.

running type(t[t < 8]) gives ndarray, guess should have properties of numpy array. should perhaps better list expressions? have not done timed comparison yet, imagine problem large 2d arrays.

t[ t < 8 ] indeed give array, however, doesn't give array of same size started with. t < 8 give boolean array same shape t. when use index t, pull out elements boolean array true leaving shorter array. when again:

result = t[t<8] result[t<5] 

then boolean index array same shape t once again, you're using index smaller array causes error.

the documentation correct. new array isn't view original ... copy of data, doesn't imply new array same shape or size original.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -