[SciPy-user] Results of squeeze function on a [1,1] size array

Robert Kern robert.kern@gmail....
Mon Oct 13 12:16:10 CDT 2008


On Mon, Oct 13, 2008 at 12:12, Michael Hearne <mhearne@usgs.gov> wrote:
> Using numpy version 1.1.0.dev5077
> Using scipy version 0.7.0.dev4174
>
> on Mac OS X
>
> If I do the following:
> x = zeros([1,1])
> x[0] = 3.4756
> y = squeeze(x)
>
> What is y?  When I print or do arithmetic with y, it seems like it's a
> scalar.

Why do you say that? It looks like a 0-dim array to me.

In [1]: from numpy import *

In [2]: x = zeros([1,1])

In [3]: x
Out[3]: array([[ 0.]])

In [4]: x[0] = 3.4756

In [5]: x
Out[5]: array([[ 3.4756]])

In [6]: y = squeeze(x)

In [7]: y
Out[7]: array(3.4756)

In [8]: y.shape
Out[8]: ()

> However, the type() function seems to indicate that it's a
> numpy.ndarray,  but with 0 dimensionality.

Yup.

> Questions:
> How do I detect when I'm in this state?

isinstance(y, numpy.ndarray) and y.shape == ()

> How can I convert this from a (sort of) scalar into an array with length
> of one?

atleast_1d(y)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list