I think this is better:
>>> x=[[1, 2],[3, 4],[5, 6]]
>>> sum(sum(x,[]))
21
Answer from hit9 on Stack OverflowI think this is better:
>>> x=[[1, 2],[3, 4],[5, 6]]
>>> sum(sum(x,[]))
21
You could rewrite that function as,
def sum1(input):
return sum(map(sum, input))
Basically, map(sum, input) will return a list with the sums across all your rows, then, the outer most sum will add up that list.
Example:
>>> a=[[1,2],[3,4]]
>>> sum(map(sum, a))
10
python - How to calculate the sum of all columns of a 2D numpy array (efficiently) - Stack Overflow
python - How is Numpy sum adding up elements of a 2d array? - Stack Overflow
Can you explain why/how this works? 2D array and sum()
Sum 2-D arrays in Python - Stack Overflow
Check out the documentation for numpy.sum, paying particular attention to the axis parameter. To sum over columns:
>>> import numpy as np
>>> a = np.arange(12).reshape(4,3)
>>> a.sum(axis=0)
array([18, 22, 26])
Or, to sum over rows:
>>> a.sum(axis=1)
array([ 3, 12, 21, 30])
Other aggregate functions, like numpy.mean, numpy.cumsum and numpy.std, e.g., also take the axis parameter.
From the Tentative Numpy Tutorial:
Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the
ndarrayclass. By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying theaxisparameter you can apply an operation along the specified axis of an array:
Other alternatives for summing the columns are
numpy.einsum('ij->j', a)
and
numpy.dot(a.T, numpy.ones(a.shape[0]))
If the number of rows and columns is in the same order of magnitude, all of the possibilities are roughly equally fast:

If there are only a few columns, however, both the einsum and the dot solution significantly outperform numpy's sum (note the log-scale):

Code to reproduce the plots:
import numpy
import perfplot
def numpy_sum(a):
return numpy.sum(a, axis=1)
def einsum(a):
return numpy.einsum('ij->i', a)
def dot_ones(a):
return numpy.dot(a, numpy.ones(a.shape[1]))
perfplot.save(
"out1.png",
# setup=lambda n: numpy.random.rand(n, n),
setup=lambda n: numpy.random.rand(n, 3),
n_range=[2**k for k in range(15)],
kernels=[numpy_sum, einsum, dot_ones],
logx=True,
logy=True,
xlabel='len(a)',
)
state=[[3,2,3],[23,4,4],[5,43,3]] s=sum(state, [])
This code transforms a 2D array to 1D array (all elements of the 2D array are now elements of an 1D array). I know that sum() first parameter sums all the elements of the iterable and the second is the starting point. But i really dont get how this leads to 2D--->1D
What you are looking for is numpy.add
import numpy as np
arr1 = np.array([[-0.31326169, -0., -3.23995333],[-0.26328247, -0., -0.64439666]])
arr2 = np.array([[-0., -0.28733533, -0.],[-0., -2.12692801, -0.]])
arr3=np.add(arr1,arr2)
print(arr3)
Output
[[-0.31326169 -0.28733533 -3.23995333]
[-0.26328247 -2.12692801 -0.64439666]]
This happens because A + B is a 2 by 3 array, and it's then summed using the built-in sum function (np.sum would've returned a single number).
__builtins__.sum will iterate over the given array, and the iteration happens to be row-wise, so individual rows will be added up (I called your arrays X and Y):
>>> X + Y
array([[-0.31326169, -0.28733533, -3.23995333],
[-0.26328247, -2.12692801, -0.64439666]])
Then, sum(X + Y) will do the following:
__sum = 0
for row in (X + Y):
__sum += row
return __sum
So, individual rows will be summed:
>>> X + Y
array([[-0.31326169, -0.28733533, -3.23995333],
[-0.26328247, -2.12692801, -0.64439666]])
>>> _[0] + _[1]
array([-0.57654416, -2.41426334, -3.88434999])
If you want to sum X and Y element-wise, then... just sum them: result = X + Y.