1.

How do you find the data type of the elements stored in the NumPy arrays?

Answer»

NumPy supports the following datatypes:


  • i - integer

  • S - string

  • b - boolean

  • f - float

  • u - unsigned integer

  • c - complex float

  • m - timedelta

  • M - datetime

  • O - object

  • U - unicode string

  • V - fixed memory chunk for types such as void
    We can make use of the dtype property that returns the type of the elements stored in the NumPy array. Let us consider the below code snippet. We create some sample arrays and we see what the data types of these arrays are.

import numpy as np

arr1 = np.array([1, 2, 3, 4])
arr2 = np.array(['I', 'love', 'Interviewbit']) # Stored as Unicode characters with length of characters ranging from 1 to 12
arr3 = np.array([1, 2, 3, 4], dtype='S') # Creating numpy array of defined type string

print(arr1.dtype)
print(arr2.dtype)
print(arr3.dtype)

The output will be:

int64
<U12
|S1


Discussion

No Comment Found