How to Show Type of a NumPy Array

Posted on

Introduction

Another article for showing how to use a NumPy array. Specifically, using an attribute exist in a NumPy array. In this article, the focus is just to show how to use an attribute for showing the type of a NumPy array variable. Actually, a NumPy array is a collection data set which has a certain characteristics. Aside from list collection data type which is allowing to have a multiple various data type exist as elements inside of it, a simple NumPy array does not allow it.

So, a simple NumPy array is a collection data set exist as part of the NumPy library which only allow one data type. Furthermore, the example for providing example is still using the same example from the previous article. Just refer to the article with the title of ‘How to Show Shape of a NumPy Array’ in this link. As a reference, in order to show available data type exist in the NumPy array, just check this link.

How to Show Type of a NumPy Array

In this part, the focus will be for showing the type of a NumPy array. Actually, in order to show the type of the NumPy array, just use the ‘type’ attribute. But before going on further, just read the article with the title of ‘How to Use NumPy’ in this link. That article is useful to give detail preparation for using NumPy library in order to have a NumPy array. The following is an example of the execution :

  1. First of all, off course run a Command Prompt since the command execution will be in the command line interface. The appearance exist 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.
    >>>
    
  2. Next, just import the NumPy library before having a NumPy array 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
    >>>
    
  3. Before getting on the attribute for showing the type, just declare or the define the NumPy array first as follows. Right after the defintion, just execute it along for demonstrating how to use dtype attribute for showing the type of the NumPy array 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.dtype);
    int32
    >>> numpy_array_variable_one_dimension = np.array([1,2,3,4])
    >>> print(numpy_array_variable_one_dimension.dtype);
    int32
    >>> numpy_array_variable_two_dimension = np.array([[1,2,3,4],[5,6,7,8]])
    >>> print(numpy_array_variable_two_dimension.dtype);
    int32
    >>> 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.dtype);
    >>> numpy_array = np.array('Michael')
    >>> print(numpy_array.dtype);
    <U7
    >>> numpy_array = np.array("Michael")
    >>> print(numpy_array.dtype);
    <U7
    >>> numpy_array = np.array(True)
    >>> print(numpy_array.dtype);
    bool
    >>> numpy_array = np.array(False)
    >>> print(numpy_array.dtype);
    bool
    >>> numpy_array = np.array(0)
    >>> print(numpy_array.dtype);
    int32
    >>> numpy_array = np.array(1)
    >>> print(numpy_array.dtype);
    int32
    >>>
    

In the case of a structured NumPy array, it will just print the data type with all of the data type structures 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_structured_array = np.array([('Michael','M',32,70.6,182.35,True),('Michelle','F',19,47.9,168.43,False)],dtype=[('name','U20'),('sex','U1'),('age','i4'),('weight','f4'),('height','f4'),('marriage_status','bool')]);
>>> print(numpy_structured_array.dtype)
[('name', '<U20'), ('sex', '<U1'), ('age', '<i4'), ('weight', '<f4'), ('height', '<f4'), ('marriage_status', '?')]
>>>

Leave a Reply