How to Show Shape of a NumPy Array

Posted on

Introduction

Another article which is focusing on how to use NumPy library. Specifically, it is about how to use NumPy array. Whereas the NumPy array will be available from a certain method available from using the NumPy library. After defining the NumPy array variable using NumPy library, there are many attributes exist which is very useful for many specific purposes. Those attributes available within the NumPy array variable. Actually, there is already an article which is focusing on how to show the dimension of a NumPy array. That article exist in this link with the title of ‘How to Show Dimension of a NumPy Array’. In this article, the focus is to show the shape of a NumPy array. Basically, shape is the attribute exist in the NumPy array variable for showing the number of the elements exist in each arrays. In other words, it will show the number of the elements in each dimension of the NumPy arrays.

How to Show Shape of a NumPy Array

In this part, there will be a demonstration for showing the shape of a NumPy array. Before getting into it, there is a specific definition about shape for NumPy array. But before going on further, just read an article with the title of ”How to Use NumPy’ in this link. That article will be focus on how to use NumPy in general. After getting to be able to use NumPy, just follow the steps or sequences below :

  1. As usual, in this context since it is using device running Microsoft Windows and the process exist in a command line interface, just run Command Prompt. Below is the appearance of the Command Prompt :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Personal>
    
  2. Continue on after, just execute ‘python’ in the Command Prompt. Just execute it as follows :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Personal>
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  3. And then, the first source code in the Python command console, just import he NumPy library as follows :
    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Personal>
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy as np
    >>>
    
  4. Then, just define a new NumPy array variable by doing the following line of source code execution :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Personal>
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy as np
    >>> numpy_array_variable = np.array([1,2,3,4])
    >>> print(numpy_array_variable(shape);
    >>> numpy_array_variable_one_dimension = np.array([1,2,3,4])
    >>> print(numpy_array_variable_one_dimension.shape);
    (4,)
    >>> numpy_array_variable_two_dimension = np.array([[1,2,3,4],[5,6,7,8]])
    >>> print(numpy_array_variable_two_dimension.shape);
    (2, 4)
    >>> numpy_array_variable_three_dimension = np.array([[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]])
    >>> print(numpy_array_variable_three_dimension.shape);
    (2, 2, 4)
    >>>
    

Leave a Reply