Numpy axis intuiation
Axis play a key role in Numpy array operations, it indicate the dimension of an array(or a tensor), for example, if an array have shape (2,4,6),
the first dimension is axis=0 coresponding to shape 2, second dimension is axis=1 coresponding to shape 4, etc, and for the convience, Numpy also can use axis=-1 to indicate the last dimension.
I made an animation for intuiation.
Functions in Numpy to operate array can be classified by how to change dimensions.
- keep dimensions
- collapse dimensions
- expand dimensions
- switch axis
Here is a animation shows how to operate array along different axis when keep/collapse dimensions.
For expand and sitch axis it is almost the same idea. Numpy doc example expain this very well.
expand_dims (a, axis) |
Expand the shape of an array. |
swapaxes (a, axis1, axis2) |
Interchange two axes of an array. |