Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Summation Over an Axis

By specifying axis=1, NumPy will compute the sum of the numbers across each array.

Example

Execute the summation for the following array along the first axis:

import numpy as np

arr1 = np.array([123])
arr2 = np.array([123])

newarr = np.sum([arr1, arr2], axis=1)

print(newarr)
Returns: [6, 6]

Cummulative Sum

Cumulative sum involves incrementally adding array elements. For example, the cumulative sum of [1, 2, 3, 4] is [1, 3, 6, 10]. This can be done using the cumsum() function.

Example

Calculate the cumulative summation for the following array:

import numpy as np

arr = np.array([123])

newarr = np.cumsum(arr)

print(newarr)