[Numpy-discussion] concatenating 1-D arrays to 2D

Bill Baxter wbaxter@gmail....
Fri Mar 23 13:57:09 CDT 2007


On 3/24/07, Sebastian Haase <seb.haase@gmx.net> wrote:
> >
> > Then of course, there's r_ and c_:
> >
> > c = numpy.c_[a,b]
> >
> > c = numpy.r_[a[None],b[None]].T
> >
> > --bb
> So,
> None is the same as newaxis - right?

Yes, newaxis is None.  None is newaxis.  Same thing.  I just don't see
much advantage in spelling it numpy.newaxis, since it's pretty common
and not likely to ever change.

> But what is    a[None]   vs.  a[:,N.newaxis] ?

a[None] is the same as a[None,:].  It prepends the new axis, so a
shape of (5,) becomes (1,5), a "row vector"

a[:,None] puts the new axis after the first axis, so shape of (5,)
becomes (5,1) a "column vector"

a[None,:,None] puts a new axis both before and after, so (5,) becomes (1,5,1).

If 'a' is higher dimensional, the same kind of thing goes.  Wherever
None/newaxis appears in the index, insert a new axis there in the
result.

Say A is shape (2,3,4), then
A[:,None,:,None] is shape (2,1,3,1,4).  (Same as A[:,None,:,None,:]
since unspecified trailing indices are always taken to be ':')

--bb


More information about the Numpy-discussion mailing list