I want to understand how this ndarray.sum(axis=) works. I know that axis=0 is for columns and axis=1 is for rows. But in case of 3 dimensions(3 axes) its difficult to interpret below result.
arr = np.arange(0,30).reshape(2,3,5)
arr
Out[1]:
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]],
[[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]]])
arr.sum(axis=0)
Out[2]:
array([[15, 17, 19, 21, 23],
[25, 27, 29, 31, 33],
[35, 37, 39, 41, 43]])
arr.sum(axis=1)
Out[8]:
array([[15, 18, 21, 24, 27],
[60, 63, 66, 69, 72]])
arr.sum(axis=2)
Out[3]:
array([[ 10, 35, 60],
[ 85, 110, 135]])
Here in this example of 3 axes array of shape(2,3,5), there are 3 rows and 5 columns. But if i look at this array as whole, seems like only two rows (both with 3 array elements).
Can anyone please explain how this sum works on array of 3 or more axes(dimensions).
Use the numpy. sum() Function to Find the Sum of Columns of a Matrix in Python. The sum() function calculates the sum of all elements in an array over the specified axis. If we specify the axis as 0, then it calculates the sum over columns in a matrix.
The numpy. sum() function is available in the NumPy package of Python. This function is used to compute the sum of all elements, the sum of each row, and the sum of each column of a given array. Essentially, this sum ups the elements of an array, takes the elements within a ndarray, and adds them together.
TLDR: axis is the dimension to be collapsed into a single value. axis=0 ==> rows ==> collapse rows and so we perform column sums (sum together all values in each column) leaving us one value per column. axis=1 ==> columns so we do row sums (add up all values in each row) so that we are left with one value per row.
STEP 1: Declare and initialize an array. STEP 2: The variable sum will be used to calculate the sum of the elements. Initialize it to 0. STEP 3: Loop through the array and add each element of the array to the variable sum as sum = sum + arr[i].
numpy.sum (arr, axis, dtype, out) : This function returns the sum of array elements over the specified axis. arr : input array. axis : axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened (works on all the axis). axis = 0 means along the column and axis = 1 means working along the row.
When we used np.sum with axis = 1, the function summed across the columns. Effectively, it collapsed the columns down to a single column!
When we use np.sum with the axis parameter, the function will sum the values along a particular axis. In particular, when we use np.sum with axis = 0, the function will sum over the 0th axis (the rows). It’s basically summing up the values row-wise, and producing a new array (with lower dimensions).
Remember: axes are like directions along a NumPy array. They are the dimensions of the array. Specifically, axis 0 refers to the rows and axis 1 refers to the columns. So when we use np.sum and set axis = 0, we’re basically saying, “sum the rows.” This is often called a row-wise operation.
If you want to keep the dimensions you can specify keepdims
:
>>> arr = np.arange(0,30).reshape(2,3,5)
>>> arr.sum(axis=0, keepdims=True)
array([[[15, 17, 19, 21, 23],
[25, 27, 29, 31, 33],
[35, 37, 39, 41, 43]]])
Otherwise the axis you sum along is removed from the shape. An easy way to keep track of this is using the numpy.ndarray.shape
property:
>>> arr.shape
(2, 3, 5)
>>> arr.sum(axis=0).shape
(3, 5) # the first entry (index = axis = 0) dimension was removed
>>> arr.sum(axis=1).shape
(2, 5) # the second entry (index = axis = 1) was removed
You can also sum along multiple axis if you want (reducing the dimensionality by the amount of specified axis):
>>> arr.sum(axis=(0, 1))
array([75, 81, 87, 93, 99])
>>> arr.sum(axis=(0, 1)).shape
(5, ) # first and second entry is removed
Here is another way to interpret this. You can consider a multi-dimensional array as a tensor, T[i][j][k]
, while i, j, k represents axis 0,1,2
respectively.
T.sum(axis = 0)
mathematically will be equivalent to:
Similary, T.sum(axis = 1)
:
And, T.sum(axis = 2)
:
So in another word, the axis will be summed over, for instance, axis = 0
, the first index will be summed over. If written in a for loop:
result[j][k] = sum(T[i][j][k] for i in range(T.shape[0])) for all j,k
for axis = 1
:
result[i][k] = sum(T[i][j][k] for j in range(T.shape[1])) for all i,k
etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With