[Numpy-discussion] question on use of newaxis

Friedrich Romstedt friedrichromstedt@gmail....
Wed Feb 9 04:40:21 CST 2011


2011/2/9 Mark Bakker <markbak@gmail.com>:
> a = array([1,2,3])
>
> I want to take the last two terms and make it a column vector:
>
> a[[1,2],newaxis]
> a[[1,2]][:,newaxis]

The former is advanced indexing, while the latter is basic slicing
(after advanced indexing).  See
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html.
There, in the advanced indexing case, "All sequences and scalars in
the selection tuple are converted to intp indexing arrays", and:

>>> numpy.intp
<type 'numpy.int32'>

.  Thus it tries to interpret the None:

>>> numpy.newaxis
>>> print repr(numpy.newaxis)
None

as an ``numpy.intp``, hence the error.  Since advanced indexing
deletes axes of an ndarray and inserts new ones (see
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer,
last point), a ``numpy.newaxis`` might be ambiguous.  In your case, it
would be not, though.

Friedrich


More information about the NumPy-Discussion mailing list