4.1 understanding dimensions/axis#

import numpy as np

print(np.__version__)
1.26.3
a = np.array([1.0])

print(a)
[1.]
print(a.shape)
(1,)
print(a.ndim)
1
a = np.array([1,2,3])

print(a)
print(a.shape)
print(a.ndim)
[1 2 3]
(3,)
1
a = np.random.random((3,3))

print(a)
print(a.shape)
print(a.ndim)
[[0.07122984 0.06789335 0.00698459]
 [0.8311694  0.83494065 0.48630464]
 [0.85184618 0.84475092 0.42934961]]
(3, 3)
2
a = np.random.random((3,3, 3))

print(a)
print(a.shape)
print(a.ndim)
[[[0.95462801 0.60512002 0.6047297 ]
  [0.68684633 0.62279337 0.19327975]
  [0.41276365 0.28618163 0.64837841]]

 [[0.3078391  0.70194352 0.42696897]
  [0.31219591 0.36844784 0.98346777]
  [0.1970224  0.75378856 0.6807713 ]]

 [[0.99790151 0.25110461 0.83511172]
  [0.31296324 0.29234685 0.09427992]
  [0.86697958 0.81282579 0.7160296 ]]]
(3, 3, 3)
3
a = np.random.random((3, 3, 3, 3))

print(a)

print(a.shape)
print(a.ndim)
[[[[0.72190031 0.00424768 0.08794787]
   [0.28247108 0.98897146 0.03086842]
   [0.70728915 0.44134899 0.67225261]]

  [[0.6225447  0.58420515 0.67454665]
   [0.11841753 0.70546319 0.41910505]
   [0.40028494 0.09679646 0.43112653]]

  [[0.54465555 0.46139381 0.47459232]
   [0.21229868 0.11488999 0.81087326]
   [0.84323553 0.68149636 0.18485055]]]


 [[[0.47988078 0.91000043 0.36050839]
   [0.87933007 0.56566732 0.00685789]
   [0.36948722 0.76268526 0.24067691]]

  [[0.38849848 0.67225672 0.10785866]
   [0.74273633 0.17520733 0.89278406]
   [0.63835625 0.66410134 0.07812237]]

  [[0.10707876 0.90745983 0.84421807]
   [0.33639011 0.50491774 0.28320406]
   [0.48744334 0.60940229 0.308669  ]]]


 [[[0.8816373  0.4675637  0.63317424]
   [0.95029897 0.56198099 0.0512128 ]
   [0.8249901  0.94535192 0.49122771]]

  [[0.60439925 0.51033548 0.9722273 ]
   [0.37399021 0.80003453 0.75825831]
   [0.2923625  0.65624202 0.75666917]]

  [[0.1290178  0.90519809 0.67352899]
   [0.50860887 0.73005551 0.60302865]
   [0.54641244 0.04824019 0.17343882]]]]
(3, 3, 3, 3)
4

Total running time of the script: ( 0 minutes 0.006 seconds)

Gallery generated by Sphinx-Gallery