.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/numpy/dimensions_axis.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_numpy_dimensions_axis.py: =================================== 4.1 understanding dimensions/axis =================================== .. GENERATED FROM PYTHON SOURCE LINES 7-12 .. code-block:: default import numpy as np print(np.__version__) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.26.4 .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: default a = np.array([1.0]) print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none [1.] .. GENERATED FROM PYTHON SOURCE LINES 19-21 .. code-block:: default print(a.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (1,) .. GENERATED FROM PYTHON SOURCE LINES 22-24 .. code-block:: default print(a.ndim) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: default a = np.array([1,2,3]) print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none [1 2 3] .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: default print(f'length: {len(a)}, size: {a.size}, dim: {a.ndim}, shape: {a.shape}') .. rst-class:: sphx-glr-script-out .. code-block:: none length: 3, size: 3, dim: 1, shape: (3,) .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: default a = np.random.random((4, 5)) print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none [[0.35378575 0.84759879 0.41854462 0.91992659 0.74497 ] [0.01923873 0.79927105 0.05573417 0.0260903 0.03302782] [0.69334693 0.38664874 0.78804392 0.99024502 0.91512554] [0.40198701 0.52207574 0.99590104 0.03174812 0.41955784]] .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: default print(f'length: {len(a)}, size: {a.size}, dim: {a.ndim}, shape: {a.shape}') .. rst-class:: sphx-glr-script-out .. code-block:: none length: 4, size: 20, dim: 2, shape: (4, 5) .. GENERATED FROM PYTHON SOURCE LINES 43-45 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,) .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: default a = np.random.random((3, 4, 5)) print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none [[[0.66568551 0.90116763 0.00801929 0.29054792 0.34485401] [0.3366532 0.23826046 0.05233746 0.89061753 0.84255441] [0.56661958 0.01119323 0.73329046 0.3381184 0.07716098] [0.15046486 0.50130673 0.96176383 0.05288099 0.65945255]] [[0.22441803 0.55969909 0.20168638 0.60704426 0.61939829] [0.17417084 0.54190168 0.9349729 0.51025176 0.30921539] [0.43000761 0.37355919 0.31378183 0.93033855 0.16454719] [0.35513501 0.60141758 0.6528549 0.97409538 0.47606376]] [[0.05991674 0.66851269 0.44354118 0.22650048 0.20250279] [0.84341428 0.44802401 0.22461202 0.58928467 0.2097547 ] [0.75339913 0.09154367 0.52222867 0.04363077 0.68649349] [0.52151979 0.7604434 0.81025123 0.33067682 0.23696642]]] .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: default print(f'length: {len(a)}, size: {a.size}, dim: {a.ndim}, shape: {a.shape}') .. rst-class:: sphx-glr-script-out .. code-block:: none length: 3, size: 60, dim: 3, shape: (3, 4, 5) .. GENERATED FROM PYTHON SOURCE LINES 55-57 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,). .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: default a = np.random.random((2, 3, 4, 5)) print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none [[[[1.69359765e-01 5.50819730e-01 5.41460474e-01 1.04482028e-01 1.84001862e-01] [8.12001697e-01 9.13677827e-02 1.73072438e-01 1.27558768e-01 5.61550461e-01] [7.93577028e-01 7.76283275e-01 6.50696796e-01 6.58061346e-01 8.94507310e-01] [5.43754408e-01 5.19716278e-01 3.61511464e-01 5.26015488e-01 1.08638888e-01]] [[7.26204666e-01 5.97159600e-01 2.63489205e-01 6.83840318e-02 3.01083360e-01] [7.89346873e-01 6.39228312e-02 5.81418644e-01 2.45919527e-01 2.55880484e-01] [1.00798631e-01 5.35423391e-01 4.96856892e-01 1.81955836e-01 9.32971982e-01] [3.10477457e-01 4.89779092e-01 3.70458263e-01 9.82036740e-01 8.07933340e-01]] [[8.09800179e-01 5.68229451e-01 8.98368734e-01 1.56472554e-01 1.31098535e-01] [6.70358618e-01 8.07093264e-01 2.15726554e-01 6.30372275e-01 2.30381384e-01] [3.58972980e-01 3.48816486e-01 6.28577265e-01 3.44427240e-01 7.89990271e-01] [7.17795110e-01 9.80600655e-01 8.28270907e-01 5.41211543e-01 3.13045696e-01]]] [[[5.13516067e-04 5.57521065e-01 5.43800772e-01 7.97971550e-01 4.59060779e-01] [6.17071003e-01 6.00208461e-01 9.61822163e-01 6.60204191e-01 4.05914058e-01] [7.41933818e-01 3.49233470e-01 3.81318754e-01 8.03641628e-01 4.32943237e-01] [6.98233292e-01 2.70387837e-01 1.47348166e-01 7.46651779e-01 6.57660205e-01]] [[5.88079775e-01 1.60354864e-01 4.43966351e-01 3.84860454e-01 4.63659441e-01] [5.44312923e-01 1.99684175e-01 7.34406140e-01 9.60603254e-01 1.19749957e-01] [3.08860456e-01 2.58157688e-01 5.15263756e-01 2.09102144e-01 1.15740242e-01] [5.16025048e-01 7.94590735e-02 9.30896109e-01 3.41921896e-01 5.88860746e-01]] [[7.56287507e-01 2.28018001e-01 1.43653648e-01 8.06950049e-01 4.38242068e-02] [9.61808835e-01 2.46091983e-01 7.42262312e-01 8.95480792e-01 2.35714429e-01] [9.01857536e-01 9.29747970e-01 6.54127973e-01 5.03798662e-01 4.28908478e-01] [7.14703348e-01 3.68232939e-01 5.84643438e-01 1.43750937e-01 3.41964591e-01]]]] .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: default print(f'length: {len(a)}, size: {a.size}, dim: {a.ndim}, shape: {a.shape}') .. rst-class:: sphx-glr-script-out .. code-block:: none length: 2, size: 120, dim: 4, shape: (2, 3, 4, 5) .. GENERATED FROM PYTHON SOURCE LINES 67-77 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. .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: default print(np.mean(a)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.49290649748986193 .. GENERATED FROM PYTHON SOURCE LINES 81-82 If we do np.mean(a, axis=0), it will calculate the mean along the axis 0. .. GENERATED FROM PYTHON SOURCE LINES 82-85 .. code-block:: default print(np.mean(a, axis=0).shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (3, 4, 5) .. GENERATED FROM PYTHON SOURCE LINES 86-87 If we do np.mean(a, axis=1), it will calculate the mean along the axis 1. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default print(np.mean(a, axis=1).shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (2, 4, 5) .. GENERATED FROM PYTHON SOURCE LINES 91-92 If we do np.mean(a, axis=2), it will calculate the mean along the axis 2. .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: default print(np.mean(a, axis=2).shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (2, 3, 5) .. GENERATED FROM PYTHON SOURCE LINES 96-97 If we do np.mean(a, axis=3), it will calculate the mean along the axis 3. .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: default print(np.mean(a, axis=3).shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (2, 3, 4) .. GENERATED FROM PYTHON SOURCE LINES 101-102 But if we specify an axis that does not exist, it will give an ``AxisError`` error. .. GENERATED FROM PYTHON SOURCE LINES 102-106 .. code-block:: default # Uncomment the following line to see the error # # print(np.mean(a, axis=4).shape) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.010 seconds) .. _sphx_glr_download_auto_examples_numpy_dimensions_axis.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/numpy/dimensions_axis.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: dimensions_axis.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: dimensions_axis.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_