Skip to content
Justice Tech School

Working With Axes

Many functions take axis. Axis 0 runs down rows, axis 1 runs across columns. Use keepdims to keep the dimension for broadcasting.

Example
import numpy as np
m = np.array([[1, 2], [3, 4]])
print(m.sum(axis=0, keepdims=True))
print(m / m.sum(axis=1, keepdims=True))

Result

[[4 6]]
[[0.33333333 0.66666667]
 [0.42857143 0.57142857]]

Exercise

Keep the dimension.

m.sum(axis=0, =True)

Quiz Test

What does keepdims help with?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.