[Numpy-discussion] numpy scalars and savez -- bug?

Robert Kern robert.kern at gmail.com
Fri Apr 19 13:32:24 EDT 2013

On Fri, Apr 19, 2013 at 9:40 PM, Sebastian Berg
<sebastian at sipsolutions.net> wrote:

> Fun fact, array[()] will convert a 0-d array to a scalar, but do nothing
> (or currently create a view) for other arrays. Which is actually a good
> question. Should array[()] force a view or not?

Another fun fact: scalar[()] gives you a rank-0 array. :-)

I think the array[()] behavior follows logically as a limiting form of
multidimensional indexing. Given rank-3 array,

  array[(i, j, k)] gives a scalar
  array[(i, j)]    gives a rank-1 view of the last axis
  array[(i,)]      gives a rank-2 view of the last 2 axes
  array[()]        gives a rank-3 view of the last 3 axes (i.e. all of them)

The rank-N-general rules look like so: For a rank-N array, an N-tuple
gives a scalar. Subsequent (N-M)-tuples gives appropriate rank-M views
picked out by the tuple.

I can't explain the scalar[()] behavior, though. :-)

Robert Kern

More information about the NumPy-Discussion mailing list