[Numpy-discussion] Index array confusion...

Magnus Lie Hetland magnus at hetland.org
Sun Dec 15 13:17:03 CST 2002


I can see why this happens, to some degree, but it is a bit confusing
still:

>>> a = zeros([5,5])
>>> a[[1,2,3]]
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])
>>> a[[1,2]]
0
>>> a[[1]]
array([[0, 0, 0, 0, 0]])

I was planning to use index arrays to select a subset of the rows in a
two-dimensional array, but because of the special-casing for
one-dimensional arrays of the same length as the dimensionality of the
array being indexed, it seems I can't do that (without using
put/take, which may be quite OK, I guess)... Am I right? (I see why
this behaviour is needed for tuples and slice objects; and I guess
type checking would be sort of "ugly"...)

Of course a[[1,2],] works just as expected.

This may not be important, but perhaps worth a sentence or example in
the manual? I.e. if you are using an index array idx and you don't
want to risk having it cast as a single index lookup, you should do

  a[idx,]

Wouldn't that be sound advice in general?

-- 
Magnus Lie Hetland        Practical Python          The Anygui Project
http://hetland.org        http://ppython.com        http://anygui.org




More information about the Numpy-discussion mailing list