๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ generated โ€บ numpy.sum.html
numpy.sum โ€” NumPy v2.3 Manual
Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.sum.html
numpy.sum โ€” NumPy v2.5 Manual
Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.
๐ŸŒ
Medium
medium.com โ€บ @whyamit101 โ€บ understanding-numpy-sum-with-axis-parameter-1fc543fe9fa2
Understanding numpy.sum() with Axis Parameter | by why amit | Medium
February 9, 2025 - So, if you donโ€™t specify the axis, the function doesnโ€™t care about rows or columns. It simply flattens the array and adds everything. ... Yes, you can! If you provide a tuple of axes, numpy.sum() will sum across all specified axes.
๐ŸŒ
Medium
medium.com โ€บ intuitionmath โ€บ numpy-sum-axis-intuition-6eb94926a5d1
Numpy Sum Axis Intuition
March 6, 2023 - >>> np.sum([[0, 1], [0, 5]], axis=0) ... this? The way to understand what โ€œaxisโ€ means in numpy sum is that it collapses the specified axis....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.sum.html
numpy.sum โ€” NumPy v2.1 Manual
Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-sum-in-python
numpy.sum() in Python - GeeksforGeeks
January 30, 2026 - np.sum(arr, axis=1, keepdims=True) preserves the reduced dimension, returning a column-shaped result.
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ sum
NumPy sum()
axis: Axis or axes along which a sum is performed. By default, it sums all elements.
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.sum.html
numpy.sum โ€” NumPy v2.6.dev0 Manual
Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.
๐ŸŒ
Sharp Sight
sharpsight.ai โ€บ blog โ€บ numpy-sum
How to Use the Numpy Sum Function - Sharp Sight
February 6, 2024 - That means that in addition to ... that are โ€œarray like.โ€ ยท axis (optional) The axis parameter specifies the axis or axes upon which the sum will be performed....
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:numpy โ€บ ndarray โ€บ .sum()
Python:NumPy | ndarray | .sum() | Codecademy
October 31, 2025 - In this example, the .sum() method computes sums along specific axes, by columns (axis=0) and by rows (axis=1): import numpy as np ยท
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ methods โ€บ sum
NumPy sum() (With Examples)
If axis = 1, the sum is calculated row-wise. import numpy as np array = np.array([[10, 17, 25], [15, 11, 22]]) # return the sum of elements of the flattened array result1 = np.sum(array) print('The sum of flattened array: ', result1) # return the column-wise sum result2 = np.sum(array, axis ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.0 โ€บ reference โ€บ generated โ€บ numpy.sum.html
numpy.sum โ€” NumPy v2.0 Manual
Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: Sum, mean, max, min for entire array, column/row-wise | note.nkmk.me
January 20, 2024 - By default, like np.sum(), these functions return the maximum and minimum values of the entire array. Specifying the axis argument changes this behavior to return the maximum and minimum values for each column or row.
Top answer
1 of 3
95

Setup

consider the numpy array a

a = np.arange(30).reshape(2, 3, 5)
print(a)

[[[ 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]]]

Where are the dimensions?

The dimensions and positions are highlighted by the following

            p  p  p  p  p
            o  o  o  o  o
            s  s  s  s  s

     dim 2  0  1  2  3  4

            |  |  |  |  |
  dim 0     โ†“  โ†“  โ†“  โ†“  โ†“
  ----> [[[ 0  1  2  3  4]   <---- dim 1, pos 0
  pos 0   [ 5  6  7  8  9]   <---- dim 1, pos 1
          [10 11 12 13 14]]  <---- dim 1, pos 2
  dim 0
  ---->  [[15 16 17 18 19]   <---- dim 1, pos 0
  pos 1   [20 21 22 23 24]   <---- dim 1, pos 1
          [25 26 27 28 29]]] <---- dim 1, pos 2
            โ†‘  โ†‘  โ†‘  โ†‘  โ†‘
            |  |  |  |  |

     dim 2  p  p  p  p  p
            o  o  o  o  o
            s  s  s  s  s

            0  1  2  3  4

Dimension examples:

This becomes more clear with a few examples

a[0, :, :] # dim 0, pos 0

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

a[:, 1, :] # dim 1, pos 1

[[ 5  6  7  8  9]
 [20 21 22 23 24]]

a[:, :, 3] # dim 2, pos 3

[[ 3  8 13]
 [18 23 28]]

sum

explanation of sum and axis
a.sum(0) is the sum of all slices along dim 0

a.sum(0)

[[15 17 19 21 23]
 [25 27 29 31 33]
 [35 37 39 41 43]]

same as

a[0, :, :] + \
a[1, :, :]

[[15 17 19 21 23]
 [25 27 29 31 33]
 [35 37 39 41 43]]

a.sum(1) is the sum of all slices along dim 1

a.sum(1)

[[15 18 21 24 27]
 [60 63 66 69 72]]

same as

a[:, 0, :] + \
a[:, 1, :] + \
a[:, 2, :]

[[15 18 21 24 27]
 [60 63 66 69 72]]

a.sum(2) is the sum of all slices along dim 2

a.sum(2)

[[ 10  35  60]
 [ 85 110 135]]

same as

a[:, :, 0] + \
a[:, :, 1] + \
a[:, :, 2] + \
a[:, :, 3] + \
a[:, :, 4]

[[ 10  35  60]
 [ 85 110 135]]

default axis is -1
this means all axes. or sum all numbers.

a.sum()

435
2 of 3
4

I use a nested loop operation to explain it.

import numpy as np

n = np.array(
[[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]],

 [[2, 4, 6],
 [8, 10, 12],
 [14, 16, 18]],

 [[1, 3, 5],
 [7, 9, 11],
 [13, 15, 17]]])

print(n)

print("============ sum axis=None=============")

sum = 0
for i in range(3):
  for j in range(3): 
    for k in range(3):
      sum += n[k][i][j]
print(sum) # 216

print('------------------')
print(np.sum(n))  # 216
print("============ sum axis=0 =============") 
for i in range(3):
  for j in range(3):
    sum = 0
    for axis in range(3):
      sum += n[axis][i][j]
    print(sum,end=' ')
  print()

print('------------------')
print("sum[0][0] = %d" % (n[0][0][0] + n[1][0][0] + n[2][0][0]))
print("sum[1][1] = %d" % (n[0][1][1] + n[1][1][1] + n[2][1][1]))
print("sum[2][2] = %d" % (n[0][2][2] + n[1][2][2] + n[2][2][2]))
print('------------------')
print(np.sum(n, axis=0)) 
print("============ sum axis=1 =============") 
for i in range(3):
  for j in range(3):
    sum = 0
    for axis in range(3):
      sum += n[i][axis][j]
    print(sum,end=' ')
  print()
print('------------------')
print("sum[0][0] = %d" % (n[0][0][0] + n[0][1][0] + n[0][2][0]))
print("sum[1][1] = %d" % (n[1][0][1] + n[1][1][1] + n[1][2][1]))
print("sum[2][2] = %d" % (n[2][0][2] + n[2][1][2] + n[2][2][2]))
print('------------------')
print(np.sum(n, axis=1))  
print("============ sum axis=2 =============") 
for i in range(3):
  for j in range(3):
    sum = 0
    for axis in range(3):
      sum += n[i][j][axis]
    print(sum,end=' ')
  print()
print('------------------')
print("sum[0][0] = %d" % (n[0][0][0] + n[0][0][1] + n[0][0][2]))
print("sum[1][1] = %d" % (n[1][1][0] + n[1][1][1] + n[1][1][2]))
print("sum[2][2] = %d" % (n[2][2][0] + n[2][2][1] + n[2][2][2]))
print('------------------')
print(np.sum(n, axis=2))
print("============ sum axis=(0,1)) =============") 
for i in range(3):
  sum = 0
  for axis1 in range(3):   
    for axis2 in range(3):
      sum += n[axis1][axis2][i]
  print(sum,end=' ')

print()
print('------------------')
print("sum[1] = %d" % (n[0][0][1] + n[0][1][1] + n[0][2][1] +
              n[1][0][1] + n[1][1][1] + n[1][2][1] +
              n[2][0][1] + n[2][1][1] + n[2][2][1] ))
print('------------------')
print(np.sum(n, axis=(0,1)))

result๏ผš

[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[ 2  4  6]
  [ 8 10 12]
  [14 16 18]]

 [[ 1  3  5]
  [ 7  9 11]
  [13 15 17]]]
============ sum axis=None=============
216
------------------
216
============ sum axis=0 =============
4 9 14 
19 24 29 
34 39 44 
------------------
sum[0][0] = 4
sum[1][1] = 24
sum[2][2] = 44
------------------
[[ 4  9 14]
 [19 24 29]
 [34 39 44]]
============ sum axis=1 =============
12 15 18 
24 30 36 
21 27 33 
------------------
sum[0][0] = 12
sum[1][1] = 30
sum[2][2] = 33
------------------
[[12 15 18]
 [24 30 36]
 [21 27 33]]
============ sum axis=2 =============
6 15 24 
12 30 48 
9 27 45 
------------------
sum[0][0] = 6
sum[1][1] = 30
sum[2][2] = 45
------------------
[[ 6 15 24]
 [12 30 48]
 [ 9 27 45]]
============ sum axis=(0,1)) =============
57 72 87 
------------------
sum[1] = 72
------------------
[57 72 87]
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.22 โ€บ โ€บ reference โ€บ generated โ€บ numpy.sum.html
numpy.sum โ€” NumPy v1.22 Manual
Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array.