[SciPy-User] ValueError: setting and array element with a sequence

Christopher Barker Chris.Barker at noaa.gov
Mon Jan 11 14:41:31 EST 2010


Robert Kern wrote:
> On Mon, Jan 11, 2010 at 10:40, Bruce Ford <bruce at clearscienceinc.com> wrote:
>> I'm new at this and I'm getting this error.  It looks straightforward
>> enough.  Any ideas?
>>
>> ynew = numpy.linspace (0,360,360)/2.5
>> xnew = numpy.linspace (0,180, 180)/2.5
>> coords = numpy.array([xnew, ynew])
>>
>> yeilds:  ValueError:  setting and array element with a sequence
> 
> ynew has 360 elements. xnew has 180. They need to be the same if you
> want to make an (2,N)-shape array from them.

if you want a 360x180 (or 180x360) arrays, then you can do:

In [22]: X,Y = np.meshgrid(xnew, ynew)

In [23]: X.shape
Out[23]: (360, 180)

In [24]: Y.shape
Out[24]: (360, 180)

or, better yet rely on numpy broadcasting:

In [25]: xnew.shape = (1, -1) # make x a single row

In [26]: ynew.shape = (-1, 1) # make y a single column

In [27]: z = xnew * ynew**2

# they are then broadcast when combined
In [28]: z.shape
Out[28]: (360, 180)


-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the SciPy-User mailing list