Related Articles

Related Articles

Check data type in NumPy
  • Last Updated : 26 Nov, 2020

Numpy is a module in python. It is originally called numerical python, but in short, we pronounce it as numpy. NumPy is a general-purpose array-processing package in python. It provides high-performance multidimensional data structures like array objects and tools for working with these arrays. Numpy provides faster and efficient calculations of matrices and arrays.

NumPy provides familiarity with almost all mathematical functions. In numpy these functions are called universal function ufunc.

Below are various values to check data type in NumPy:

Method #1

Checking datatype using dtype.

Example 1:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing numpy liberary
import numpy as np
  
# creating and initializing an array
arr = np.array([1, 2, 3, 23, 56, 100])
  
# printing the array and checking datatype
print('Array:', arr)
  
print('Datatype:', arr.dtype)

chevron_right


Output:

Array: [  1   2   3  23  56 100]
Datatype: int32

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import numpy as np
  
# craeting and initializing array of string
arr_1 = np.array(['apple', 'ball', 'cat', 'dog'])
  
# printing array and its datatype
print('Array:', arr_1)
  
print('Datatype:', arr_1.dtype)

chevron_right


Output:

Array: ['a' 'b' 'c' 'd']
Datatype: <U1

Method #2

Creating the array with a defined datatype. Creating numpy array by using an array function array(). This function takes argument dtype that allows us to define the expected data type of the array elements:

Example 1:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import numpy as np
  
# Creating and initializing array with datatype
arr = np.array([1, 2, 3, 8, 7, 5], dtype='S')
  
# printing array and its datatype
print("Array:", arr)
print("Datatype:", arr.dtype)

chevron_right


Output:

Array: [b'1' b'2' b'3' b'8' b'7' b'5']
Datatype: |S1

S is used for defining string datatype. We use i, u, f, S and U for defining various other data types along with their size.

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import numpy as np
  
# creating and initialising array along 
# with datatype and its size 4 i.e. 32bytes
arr = np.array([1, 2, 3, 4], dtype='i4')
  
# printing array and datatype
print('Array:', arr)
print('Datatype:', arr.dtype)

chevron_right


Output:

Array: [1 2 3 4 8 9 5]
Datatype: int32

In the above example, the size of integer elements is 4 i.e. 32bytes

Example 3:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import numpy as np
  
# creating and initialising array along 
# with datatype and its size 8 i.e. 64bytes
arr = np.array([1, 2, 3, 4], dtype='i8')
  
# printing array and datatype
print('Array:', arr)
print('Datatype:', arr.dtype)

chevron_right


Output:



Array: [1 2 3 4 8 9 7]
Datatype: int64

And in this example the size of elements is 64bytes.

Example 4:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import numpy as np
  
# creating and initialising array along 
# with datatype and its size 4 i.e. 32bytes
arr = np.array([1, 2, 3, 4, 8, 9, 7], dtype='f4')
  
# printing array and datatype
print('Array:', arr)
print('Datatype:', arr.dtype)

chevron_right


Output:

Array: [1. 2. 3. 4. 8. 9. 7.]
Datatype: float32

In the above example, the data type is float and the size is 32bytes.

Example 5:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import numpy as np
  
# creating and initialising array along 
# with datatype and its size 2
arr = np.array([1, 2, 3, 4, 8, 9, 7], dtype='S2')
  
# printing array and datatype
print('Array:', arr)
print('Datatype:', arr.dtype)

chevron_right


Output:

Array: [b'1' b'2' b'3' b'4' b'8' b'9' b'7']
Datatype: |S2

In the above example, the datatype is a string and the size is 2.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :