[Numpy-discussion] Which rule makes x[np.newaxis, :] and x[np.newaxis] equivalent?

Sebastian Berg sebastian at sipsolutions.net
Tue Dec 12 08:32:58 EST 2017


On Tue, 2017-12-12 at 14:19 +0100, Joe wrote:
> Ah, ok, now that I knew what to look for I guess I found it:
> 
> "If the number of objects in the selection tuple is less than N ,
> then : 
> is assumed for any subsequent dimensions."
> 
> https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.htm
> l
> 
> This is the one, right?
> 

Yeah, plus if it is not a tuple, it actually behaves the same as a
tuple, e.g. `arr[obj]` is identical to `arr[obj,]` (or `arr[(obj,)]`
which is the same). There are some weird exception when obj is a list a
sequence but not an array.

Note also that while everything has an implicit `, ...` at the end of
indexing, if you have exactly as many integers to index as dimensions
you get a scalar, if you would add the Ellipsis you would get an array
back.

Anyway, too many weird details for day to day stuff :). And all of that
should be covered in the docs?

- Sebastian

> 
> Am 12.12.2017 09:09 schrieb Nathaniel Smith:
> > On Tue, Dec 12, 2017 at 12:02 AM, Joe <solarjoe at posteo.org> wrote:
> > > Hi,
> > > 
> > > question says it all. I looked through the basic and advanced 
> > > indexing,
> > > but I could not find the rule that is applied to make
> > > x[np.newaxis,:] and x[np.newaxis] the same.
> > 
> > I think it's the general rule that all indexing expressions have an
> > invisible "..." on the right edge. For example, x[i][j][k] is an
> > inefficient and IMO somewhat confusing way to write x[i, j, k],
> > because x[i][j][k] is interpreted as:
> > 
> > -> x[i, ...][j, ...][k, ...]
> > -> x[i, :, :][j, :][k]
> > 
> > That this also applies to newaxis is a little surprising, but I
> > guess
> > consistent.
> > 
> > -n
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20171212/1c49d101/attachment.sig>


More information about the NumPy-Discussion mailing list