Numpy add with multiple arrays
Asked Answered
F

3

6

Is there a way to add (as opposed to sum) multiple arrays together in a single operation? Obviously, np.sum and np.add are different operations, however, the problem I'm struggling with right now is that np.add only takes two arrays at once. I could utilize either

output = 0
for arr in arr_list:
    output = output + array

or

output = 0
for arr in arr_list:
    output = np.add(output, array)

and, yes, this is workable. However, it would be nice if I could simply do some variant of

output = np.add_multiple(arr_list)

Does this exist?

EDIT: I failed to be clear initially. I specifically require a function that does not require an array of arrays to be constructed, as my arrays are not of equal dimensions and require broadcasting, for example:

a = np.arange(3).reshape(1,3)
b = np.arange(9).reshape(3,3)

a, b = a[:,:,None,None], b[None,None,:,:]

These work:

a + b        # Works
np.add(a, b) # Works

These do not, and fail with the same exception:

np.sum([a, b], axis = 0)
np.add.reduce([a, b])

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (3,1,1) into shape (1)
Favored answered 25/9, 2019 at 3:11 Comment(4)
Where does that error come from? I don't see an array with shape (3,1,1) defined anywhere.Moraceous
As far as I can tell, it comes from np.sum and np.add calling np.array on the input array, in this case, [a, b], and it's attempting to cast it into rectangular arrays on every dimension as opposed to broadcasting it.Favored
@Favored Actually I cannot reproduce the error you mentioned in the post. Are you sure the inputs a and b are correctly written?Denby
Hey @GZ0, I made a mistake, yeah - the slicings were transposed. I've corrected it and verified that I get the error expected.Favored
D
16

You can just use the Python's sum built-in:

output = sum(arr_list)

For many other numpy functions, np.<ufunc>.reduce can be used as shown by @hpaulj.

Denby answered 25/9, 2019 at 4:40 Comment(3)
To my immense surprise, this actually does work. Thank you!Favored
Interesting, why is Python's built-in sum function able to handle numpy arrays?Perak
It's able to handle numpy arrays because numpy properly implements the necessary methods for python's arithmetic operations, specifically .__add__().Extramarital
F
3

You can use the sum() to add multiple arrays.

arr = np.array([[6,2,3,5,4,3], 
          [7,7,2,4,6,7],
          [10,6,2,4,5,9]])
np.add(0, arr.sum(axis=0))
Frear answered 25/9, 2019 at 4:45 Comment(2)
Thanks for your response, but this doesn't work! I was askinf for add for a specific reason that I wasn't clear about, my mistake.Favored
As far as I can tell, the line np.add(0, arr.sum(axis=0)) is doing two sums, the outer one (via np.add) doesn't do anything since it's just adding zeros... So, you could get the same result if you just do arr.sum(axis=0).Concertina
M
1
In [18]: alist = [np.arange(4),np.ones(4),np.array([10,1,11,2])]                
In [19]: np.add.reduce(alist)                                                   
Out[19]: array([11.,  3., 14.,  6.])
In [20]: alist[0]+alist[1]+alist[2]                                             
Out[20]: array([11.,  3., 14.,  6.])

And for more fun:

In [21]: np.add.accumulate(alist)                                               
Out[21]: 
array([[ 0.,  1.,  2.,  3.],
       [ 1.,  2.,  3.,  4.],
       [11.,  3., 14.,  6.]])

edit


In [53]: a.shape                                                                
Out[53]: (1, 1, 1, 3)
In [54]: b.shape                                                                
Out[54]: (3, 3, 1, 1)

Addition with broadcasting:

In [63]: sum([a,b]).shape                                                       
Out[63]: (3, 3, 1, 3)
In [64]: (a+b).shape                                                            
Out[64]: (3, 3, 1, 3)
In [66]: np.add.reduce([a,b]).shape                                             
Out[66]: (3, 3, 1, 3)

For what it's worth, I was suggesting add.reduce because I thought you wanted to add more than 2 arrays.

All these work as long as the arrays broadcast together.

Moraceous answered 25/9, 2019 at 4:33 Comment(4)
Thanks for your response, but this doesn't work! I was askinf for add for a specific reason that I wasn't clear about, my mistake.Favored
It looks like the broardcasting fails if the first dimension of a and b match and does NOT need broadcasting, for some unknown reasons.Denby
Given an example. All of these approaches require the same broadcasting - the ability to do a+b.Moraceous
Just a note @hpaulj, I do actually want to add more than two arrays, I'm just using two in my examples since it's the most elementary.Favored

© 2022 - 2024 — McMap. All rights reserved.