odd behavior for numpy.ndarray index?
In the following output (see below), why would x[1,None] work, but
x[1,None,2] or even x[1,2,None] not work?
Incidentally, I would be very interested in a solution that allows me to
index numpy arrays using a list/iterable that might contain None values. Is
there a straightforward way to do so, or should I just use list
comprehensions (i.e. [x[a] for a in indices if a])?
Thanks!
Orest
In [122]: x = arange(5)
In [123]: x[1]
Out[123]: 1
In [124]: x[None]
Out[124]: array([[0, 1, 2, 3, 4]])
In [125]: x[1,None]
Out[125]: array([1])
In [126]: x[1,None,2]
---------------------------------------------------------------------------
On 9/9/07, Orest Kozyar
In the following output (see below), why would x[1,None] work, but x[1,None,2] or even x[1,2,None] not work?
None is the same thing as newaxis (newaxis is just an alias for None). Armed with that tidbit, a little perusing of the docs should quickly explain the behaviour you are seeing. Incidentally, I would be very interested in a solution that allows me to
index numpy arrays using a list/iterable that might contain None values. Is there a straightforward way to do so, or should I just use list comprehensions (i.e. [x[a] for a in indices if a])?
You could try something using compress x[compress(indices, indices)] for example. Whether that's superior to a iterator solution would probably depend on the problem. I'd probably use fromiter instead of a list comprehension if I went that route though. There are probably other ways too. It very much depends on the details and performance requirements of what you are trying to do. -- . __ . |-\ . . tim.hochberg@ieee.org
participants (2)
-
Orest Kozyar
-
Timothy Hochberg