[Numpy-discussion] Different behaviour of python built sum and addition on ndarrays
Nathaniel Smith
njs@pobox....
Mon Apr 16 17:14:36 CDT 2012
On Mon, Apr 16, 2012 at 11:06 PM, Christopher Mutel <cmutel@gmail.com> wrote:
> So, for both 1.5 and 1.6 (at least), it appears that the builtin sum
> does not add ndarrays the way "+" (and operator.add) do:
>
> a = np.arange(10).reshape((2,5))
> b = np.arange(10, 20).reshape((2,5))
> sum(a,b)
> Out[5]:
> array([[15, 18, 21, 24, 27],
> [20, 23, 26, 29, 32]])
>
> a + b
> Out[6]:
> array([[10, 12, 14, 16, 18],
> [20, 22, 24, 26, 28]])
>
> Is this expected? I couldn't find a description of why this would
> occur in the mailing list or in the documentation. I can't figure out
> what sum does at all, actually, as it doesn't seem to be a case of
> strange broadcasting or any other tricks I tried.
The 'sum' function that comes builtin to the python language does this:
def sum(iterable, start=0):
value = start
for item in iterable:
value = value + item
return value
So your 'b' is acting as an initializer for this sum, which may not be
what you expect. 'sum' is almost always called with only one
argument.[1]
Next, note that if you try to iterate over a Numpy 2-d array, it gives
you each row:
In [15]: for row in a: print "row is:", row
row is: [0 1 2 3 4]
row is: [5 6 7 8 9]
So sum(a, b) is in fact computing this:
In [16]: b + a[0, :] + a[1, :]
Out[16]:
array([[15, 18, 21, 24, 27],
[20, 23, 26, 29, 32]])
Moral of the story: use np.sum, it's less confusing :-)
HTH,
-- Nathaniel
[1] The only exception I've ever run into is that if you want to
concatenate a list-of-lists, then this is a cute and useful trick:
In [13]: sum([["a", "b"], ["c", "d"]], [])
Out[13]: ['a', 'b', 'c', 'd']
More information about the NumPy-Discussion
mailing list