Explanation of np.sum

db = (1/m) * np.sum(dZ, axis=1, keepdims=True)

what is the use of axis = 0 and keepdims

1 Like

Let’s say we have a matrix A of shape (3, 4):

[[5 0 3 3]
[7 9 3 5]
[2 4 7 6]]

Sum along axis 0 (sum along the columns) with keepdims=True (retained the dimension):
[[14 13 13 14]]
Shape: (1, 4)

Sum along axis 0 with keepdims=False:
[14 13 13 14]
Shape: (4,)

Sum along axis 1 (along the rows) with keepdims=True:
[[11]
[24]
[19]]
Shape: (3, 1)

Sum along axis 1 with keepdims=False:
[11 24 19]
Shape: (3,)

4 Likes