Saturday, September 21, 2024
Google search engine
HomeLanguagesCheck data type in NumPy

Check data type in NumPy

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




# importing numpy library
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)


Output:

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

Example 2:

Python3




import numpy as np
 
# creating 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)


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




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)


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




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)


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




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)


Output:

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

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

Example 4:

Python3




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)


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




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)


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.

RELATED ARTICLES

Most Popular

Recent Comments