How to Show Size of a NumPy Array

Posted on

Introduction

In this article, there is another attribute in a NumPy array exist. So, the main content will be to show the usage of that attribute. Actually, there are several articles exist in the previous one which is also discussing about how to use NumPy array’s attribute. In this case, the attribute will have a specific function to be able to show the size of the NumPy array. In order to do that, the attribute is ‘size’ which is actually an original attribute of the NumPy array. Before going on further, in order to use have a NumPy array, just take a look at the article with the title of ‘How to Use NumPy’ in this link.

How to Show the Size of a NumPy Array

This part will going into the detail of the subject. It is describing about how to show the size of a NumPy array. Before going on demonstrating about how to show the size of a NumPy array, focus on to have the NumPy library first. Just read an article in this link with the title of ‘How to Use NumPy’ in order to have it exist in the local device. After that, below is the steps for showing on how to get the size of a NumPy array :

  1. As it always been the first step, execute the Command Prompt. It obvious since the example is using a command execution in the command line interface in a device running using Microsoft Windows operating system. 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. Execute python to get in to the Python command console as follows :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Personal>python
    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. Do not forget to import the NumPy library as it exist below :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Personal>python
    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. Next step, just execute to define the NumPy array first. It is using an example definition from the previous article with the title of ‘How to Show Shape of a NumPy Array’ in this link. Along with that, just execute the command for getting the size of each NumPy array variable as follows :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\Personal>python
    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_zero_dimension = np.array(1)
    >>> print(numpy_array_variable_zero_dimension.size); 
    1
    >>> numpy_array_variable_one_dimension = np.array([1,2,3,4])
    >>> print(numpy_array_variable_one_dimension.size); 
    4
    >>> numpy_array_variable_two_dimension = np.array([[1,2,3,4],[5,6,7,8]])
    >>> print(numpy_array_variable_two_dimension.size); 
    8
    >>> 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.size); 
    16   
    >>>

Leave a Reply