Question #6939   Submitted by Answiki on 10/16/2022 at 07:38:03 PM UTC

In Python, how to add the values of a single row of a NumPy array?

Answer   Submitted by Answiki on 10/16/2022 at 07:38:49 PM UTC

In Python, to add the values of a row of a NumPy array, the easiest way is to isolate the concerned row and to calculate the sum with the numpy.sum() function:

>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4], [5, 6]])
>>> a[0,:].sum()
3
>>> a[1,:].sum()
7




3 events in history
Answer by Answiki on 10/16/2022 at 07:38:49 PM

In Python, to add the values of a row of a NumPy array, the easiest way is to isolate the concerned row and to calculate the sum with the numpy.sum() function:

>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4], [5, 6]])
>>> a[0,:].sum()
3
>>> a[1,:].sum()
7




Question by Answiki 10/16/2022 at 07:38:03 PM
In Python, how to add the values of a single row of a NumPy array?
Question by Answiki 10/16/2022 at 07:37:56 PM
In Python, how to add the values of a row of a NumPy array?
# ID Query URL Count

Icons proudly provided by Friconix.