concatenate Numeric
Robert Kern
robert.kern at gmail.com
Sun Jul 9 16:34:10 EDT 2006
Sheldon wrote:
> Hi,
>
> I am trying to build a large array using concatenate function in
> python.
> So as I loop over the number of arrays, of which there are 12 (4 down
> and 3 across), I create 3 long arrays by concatenating them at the
> bottom and then concatenating them side by side:
[snip]
> print 'Max lat', max(max(ppslat_all)), '\t','Min lat',
> min(min(ppslat_all))
> print 'Max lon', max(max(ppslon_all)), '\t','Min lon',
> min(min(ppslon_all))
>
> *****************
> Now this works, the array size is correct but the longitude values
> given for max and min are wrong. What is even stranger to me is that
> when I write the array in binary format to a file and read it with
> Matlab, the max and min are correct but when I read it back with python
> the max and min are again incorrect for only the longitude data. I
> saved the max and min for the longitude for each array and then check
> it in the python program and they are correct at the end but the
> max(max(ppslon)) values is incorrect. Does anyone knows why this is
> so?
> If I was doing something wrong then Matlab would not have returned
> correct values.
Don't use min() and max() on multidimensional arrays. They won't give sensible
answers.
In [11]: a = RA.random([3,5])
In [12]: a
Out[12]:
array([[ 0.01721657, 0.64291363, 0.33210659, 0.89887972, 0.24437849],
[ 0.88205348, 0.00839329, 0.35999039, 0.9966411 , 0.54957126],
[ 0.59983864, 0.18983323, 0.13727718, 0.8987289 , 0.05425076]])
In [13]: min(a)
Out[13]: array([ 0.59983864, 0.18983323, 0.13727718, 0.8987289 , 0.05425076])
The builtin min() and max() compare the values in the sequence. In this case,
those values are the rows of the arrays. Numeric uses rich comparisons, so the
result of a comparison is a boolean array. Numeric also has the convention that
if any of the elements of an array are considered to be True, then the whole
array is.
In [16]: a[1] < a[2]
Out[16]: array([0, 1, 0, 0, 0])
In [17]: bool(_)
Out[17]: True
In [18]: a[2] < a[1]
Out[18]: array([1, 0, 1, 1, 1])
In [19]: bool(_)
Out[19]: True
This makes min(a) incorrect when len(a.shape) > 1. Instead, use the minimum and
maximum ufuncs provided with Numeric:
In [21]: N.minimum.reduce(a.flat)
Out[21]: 0.0083932917161983426
In [22]: N.maximum.reduce(a.flat)
Out[22]: 0.99664110397663608
--
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 Python-list
mailing list