How To Use Custom Data Types In Python Numpy Arrarys

As an extension package of python, NumPy provides richer data types than python. You can read the NumPy official website to see all the NumPy data types. Data type object ( which is also known as dtype object ) is mainly used to describe the data type, size, and byte order of array elements. It can also be used to create structured data. For example, Int64 and float32 are common instances of dtype objects.

1. How To Create NumPy dtype Object.

  1. Syntax format for creating NumPy dtype objects.
    import numpy as np
    
    np.dtype(object)
  2. Create NumPy dtype object example.
    >>> import numpy as np # import numpy package.
    >>>
    >>> custom_dtype = np.dtype(np.float16) # call the numpy package's dtype() function to create a custom data type, the custom data type extends the np.float16 data type.
    >>>
    >>> print(custom_dtype) # print out the custom data type.
    float16
  3. Each data type in NumPy has a unique character code which is called data type identification code. You can find them in the URL https://numpy.org/doc/stable/reference/arrays.dtypes.html.

2. How To Use Custom NumPy dtype Object Examples.

  1. The below example will use the data type identification code to create a NumPy data type object, and then apply the data type object to a NumPy ndarray.
    >>> import numpy as np # import the python numpy library
    >>>
    >>> dt_score = np.dtype([('score','f2')]) # define a custom numpy data type to save scores, the first element is the score number, the second element is the score number format.
    >>>
    >>> print(dt_score) # print out the customezed numpy data type.
    [('score', '<f2')]
    >>>
    >>> ndarr = np.array([(88,),(99,),(100,)], dtype = dt_score) # create an numpy ndarray, and set the array element data type to the above custom data type.
    >>>
    >>> print(ndarr) # print out the ndarray, you can find that the score number for each element has been changed to floating number.
    [( 88.,) ( 99.,) (100.,)]
    >>>
    >>> print(ndarr.dtype) # check the ndarray data type.
    [('score', '<f2')]
    >>>
    >>> print(ndarr['score']) # get the score in the ndarray. 
    [ 88.  99. 100.]
  2. Generally, structured data uses fields to describe the characteristics of an object. The following example describes the characteristics of the name, age, and salary of a software development engineer.
    >>> import numpy as np
    >>>
    >>> engineer = np.dtype([('name','S20'), ('age', 'i1'), ('salary', 'f4')]) # define a structured data type, name is string type, age is integer type, salary is float type.
    >>>
    >>> print(engineer) # print out the custom data type.
    [('name', 'S20'), ('age', 'i1'), ('salary', '<f4')] 
    >>>
    >>> ndarr = np.array([('jerry', 36, 10000.99),('tom', 28, 3000.98)], dtype = engineer)  # apply the custom data type to the numpy array element.
    >>>
    >>> print(ndarr) # print out the numpy ndarray object
    [(b'jerry', 36, 10000.99) (b'tom', 28,  3000.98)]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.