4.1 understanding dimensions/axis
Note
Click here to download the full example code or to run this example in your browser via Binder
4.1 understanding dimensions/axis#
import numpy as np
print(np.__version__)
1.26.4
[1.]
print(a.shape)
(1,)
print(a.ndim)
1
[1 2 3]
length: 3, size: 3, dim: 1, shape: (3,)
a = np.random.random((4, 5))
print(a)
[[0.68774516 0.01337636 0.75400826 0.49343143 0.4939963 ]
[0.08632976 0.6957455 0.73373984 0.62194291 0.21954515]
[0.94578633 0.49263777 0.04760352 0.91677918 0.14989685]
[0.85153808 0.04470019 0.96771737 0.4060343 0.74804867]]
length: 4, size: 20, dim: 2, shape: (4, 5)
A numpy array should not be understood as rows and columns. Rather a numpy array of shape (4,5) can be understood as four arrays each of shape (5,)
a = np.random.random((3, 4, 5))
print(a)
[[[0.88114156 0.94551498 0.87447065 0.18324594 0.86468033]
[0.14502003 0.04338091 0.42498342 0.41149532 0.20784856]
[0.70422598 0.26468361 0.27597679 0.65746609 0.64250484]
[0.6805446 0.39168738 0.8782638 0.8244213 0.04990689]]
[[0.94175663 0.78209756 0.43855161 0.81264944 0.71512493]
[0.69473642 0.82183769 0.18241473 0.39476053 0.55910042]
[0.95050405 0.80077031 0.43362877 0.56216161 0.99439135]
[0.5275721 0.48162737 0.7209351 0.15786668 0.23150258]]
[[0.95733644 0.63250961 0.65327453 0.92265222 0.0265852 ]
[0.68770844 0.12812105 0.54696806 0.22534968 0.45518995]
[0.98719484 0.29457375 0.92321891 0.84456688 0.1592207 ]
[0.74668852 0.1891188 0.51362366 0.18832479 0.59061075]]]
length: 3, size: 60, dim: 3, shape: (3, 4, 5)
An array of shape (3, 4, 5) can be understood as three arrays each of shape (4, 5) while each of these (4, 5) arrays can be understood as four arrays each of shape (5,).
a = np.random.random((2, 3, 4, 5))
print(a)
[[[[0.31863692 0.71738861 0.76105663 0.61084862 0.38806879]
[0.41055761 0.71452401 0.06608765 0.20951174 0.02047859]
[0.79327674 0.1153045 0.8501042 0.59721564 0.09397491]
[0.7218339 0.02540481 0.75597494 0.89932126 0.2223945 ]]
[[0.2916883 0.45929629 0.56370209 0.57743229 0.31527259]
[0.23083012 0.61967167 0.29352827 0.56730033 0.50762771]
[0.31000985 0.23122868 0.28761927 0.75758809 0.11721176]
[0.05285625 0.75273366 0.05839269 0.78643967 0.68167563]]
[[0.79960133 0.96473344 0.56908195 0.40244659 0.13761461]
[0.6117078 0.26990541 0.37468797 0.06828469 0.85098471]
[0.4554712 0.13581179 0.08003213 0.21335316 0.24541492]
[0.85656187 0.74552534 0.19331595 0.76146572 0.5944402 ]]]
[[[0.58536287 0.49045159 0.81340195 0.58324591 0.17198257]
[0.7362916 0.75210336 0.42616194 0.34849187 0.55638684]
[0.59210247 0.10678613 0.69085418 0.52137622 0.5232246 ]
[0.18470485 0.31610335 0.08986639 0.42891534 0.59929573]]
[[0.37392928 0.59488272 0.92963493 0.5766865 0.69280979]
[0.89671657 0.24003033 0.87217393 0.129098 0.14748415]
[0.854386 0.87422081 0.63335493 0.38289666 0.36708582]
[0.28277904 0.28770397 0.23053364 0.14753896 0.0977382 ]]
[[0.69019203 0.01670176 0.51670299 0.33125681 0.97505092]
[0.49547549 0.16774212 0.26981296 0.24810101 0.46333169]
[0.64541692 0.83716289 0.8369878 0.64933061 0.49728444]
[0.29302586 0.79860305 0.74849799 0.81689722 0.52428863]]]]
length: 2, size: 120, dim: 4, shape: (2, 3, 4, 5)
Similarly an array of shape (2,3,4,5) can be understood as two arrays each of shape (3,4,5) and so on.
Thinking in this way, makes it easier to conceptualize higher dimensional arrays.
The naming of axis in numpy is also consistent with this understanding. The axis starts from 0 and goes up to n-1 where n is the number of dimensions. The axis 0 is the outermost axis and the axis n-1 is the innermost axis. Consider the example of mean. If we do np.mean(a), it will calculate the mean of all the elements in the array.
0.47529306450558967
If we do np.mean(a, axis=0), it will calculate the mean along the axis 0.
(3, 4, 5)
If we do np.mean(a, axis=1), it will calculate the mean along the axis 1.
(2, 4, 5)
If we do np.mean(a, axis=2), it will calculate the mean along the axis 2.
(2, 3, 5)
If we do np.mean(a, axis=3), it will calculate the mean along the axis 3.
(2, 3, 4)
But if we specify an axis that does not exist, it will give an AxisError error.
# Uncomment the following line to see the error
#
# print(np.mean(a, axis=4).shape)
Total running time of the script: ( 0 minutes 0.010 seconds)