numpy array sum code example

Example 1: sum along axis python

import numpy as np
matrix=np.ones((10,10))
print(matrix.sum(axis=0))
print(matrix.sum(axis=1))

Example 2: sum of a numpy array

>>> np.sum([0.5, 1.5])
2.0
>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
1
>>> np.sum([[0, 1], [0, 5]])
6
>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])
>>> np.sum([[0, 1], [np.nan, 5]], where=[False, True], axis=1)
array([1., 5.])

Example 3: python sum of list axes

>>> np.sum([[0, 1], [0, 5]])
6
>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])

Example 4: python np.sum

npsum = np.sum(array)

Example 5: sum elements array with step numy

b = [sum(a[current: current+5]) for current in xrange(0, len(a), 5)]

Tags:

Misc Example