In [1]:
import numpy as np

In [2]:
a = np.arange(12).reshape(3, 4)
print(a.shape)
print(a)


(3, 4)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

In [3]:
print(np.sum(a))


66

In [4]:
print(np.sum(a, axis=0))
print(np.sum(a, axis=1))


[12 15 18 21]
[ 6 22 38]

In [5]:
print(a.sum())


66

In [6]:
print(a.sum(axis=0))
print(a.sum(axis=1))


[12 15 18 21]
[ 6 22 38]

In [7]:
print(np.mean(a))


5.5

In [8]:
print(np.mean(a, axis=0))
print(np.mean(a, axis=1))


[ 4.  5.  6.  7.]
[ 1.5  5.5  9.5]

In [9]:
print(a.mean())


5.5

In [10]:
print(a.mean(axis=0))
print(a.mean(axis=1))


[ 4.  5.  6.  7.]
[ 1.5  5.5  9.5]

In [11]:
print(np.min(a))
print(np.min(a, axis=0))


0
[0 1 2 3]

In [12]:
print(a.max())
print(a.max(axis=1))


11
[ 3  7 11]

In [13]:
b = np.arange(24).reshape(2, 3, 4)
print(b.shape)
print(b)


(2, 3, 4)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

In [14]:
print(b.sum(axis=0))


[[12 14 16 18]
 [20 22 24 26]
 [28 30 32 34]]

In [15]:
print(b[0, :, :] + b[1, :, :])


[[12 14 16 18]
 [20 22 24 26]
 [28 30 32 34]]

In [16]:
print(b.sum(axis=1))


[[12 15 18 21]
 [48 51 54 57]]

In [17]:
print(b[:, 0, :] + b[:, 1, :] + b[:, 2, :])


[[12 15 18 21]
 [48 51 54 57]]

In [18]:
print(b.sum(axis=2))


[[ 6 22 38]
 [54 70 86]]

In [19]:
print(b[:, :, 0] + b[:, :, 1] + b[:, :, 2] + b[:, :, 3])


[[ 6 22 38]
 [54 70 86]]

In [20]:
print(b.sum(axis=(0, 1)))


[60 66 72 78]

In [21]:
print(b.sum(axis=(0, 2)))


[ 60  92 124]

In [22]:
print(b.sum(axis=(1, 2)))


[ 66 210]