Numpy, 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 more efficient calculations of matrices and arrays.
NumPy provides familiarity with almost all mathematical functions. In numpy, these functions are called universal function ufunc.
Check Data Type in NumPy
Here, are various ways to check NumPy dtype attributes. here we are discussing some generally used methods for checking NumPy dtype attributes those are following.
- Using dtype
- Using array function array().
Check NumPy dtype Attributes Using dtype
Example 1: Check Integer Datatype
In this example, the code snippet utilizes the NumPy library to create an array, ‘arr,’ containing integers. The array is then printed along with its datatype, showcasing the flexibility and simplicity of NumPy for numerical operations in Python.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 23 , 56 , 100 ])
print ( 'Array:' , arr)
print ( 'Datatype:' , arr.dtype)
|
Output:
Array: [ 1 2 3 23 56 100]
Datatype: int32
Example 2 : Check String Datatype
In this example, the NumPy library is used to create an array (`arr_1`) containing strings like ‘apple’, ‘ball’, ‘cat’, and ‘dog’. The script then prints the array and its data type, revealing that the elements are of type Unicode string.
Python3
import numpy as np
arr_1 = np.array([ 'apple' , 'ball' , 'cat' , 'dog' ])
print ( 'Array:' , arr_1)
print ( 'Datatype:' , arr_1.dtype)
|
Output:
Array: ['a' 'b' 'c' 'd']
Datatype: <U1
Using array function array().
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: In this example code utilizes the NumPy library to create an array ‘arr’ containing elements [1, 2, 3, 8, 7, 5], with a specified datatype ‘S’ (string). The script then prints both the array and its datatype, showcasing the flexibility of NumPy arrays to handle various data types.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 8 , 7 , 5 ], dtype = 'S' )
print ( "Array:" , arr)
print ( "Datatype:" , arr.dtype)
|
Output:
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.
Array: [b'1' b'2' b'3' b'8' b'7' b'5']
Datatype: |S1
Example 2: In this example a NumPy array named ‘arr’ is created and initialized with values [1, 2, 3, 4], specifying a 32-bit integer datatype (‘i4’). The array and its datatype are then printed using the NumPy library.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 4 ], dtype = 'i4' )
print ( 'Array:' , arr)
print ( 'Datatype:' , arr.dtype)
|
Output:
The size of integer elements is 4 i.e. 32bytes
Array: [1 2 3 4 8 9 5]
Datatype: int32
Example 3: In this example code utilizes NumPy to create and initialize an array named ‘arr’ with elements [1, 2, 3, 4], specifying a data type of 64-bit integers (‘i8’). The print statements then display the array and its data type, showcasing the resulting array and the specified data type, which is 64-bit integers.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 4 ], dtype = 'i8' )
print ( 'Array:' , arr)
print ( 'Datatype:' , arr.dtype)
|
Output:
And in this example the size of elements is 64bytes.
Array: [1 2 3 4 8 9 7]
Datatype: int64
Example 4 : In this example, a NumPy array named arr
is created and initialized with the values 1, 2, 3, 4, 8, 9, 7. The array is explicitly assigned a data type of 32-bit floating-point (‘f4’). The resulting array and its data type are then printed using the print
function
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 4 , 8 , 9 , 7 ], dtype = 'f4' )
print ( 'Array:' , arr)
print ( 'Datatype:' , arr.dtype)
|
Output:
In this example, the data type is float and the size is 32bytes.
Array: [1. 2. 3. 4. 8. 9. 7.]
Datatype: float32
Example 5: In this example code utilizes the NumPy library to create an array named ‘arr’ containing integers [1, 2, 3, 4, 8, 9, 7] with a specified datatype ‘S2’, indicating a string of length 2. The code then prints the array and its datatype, showcasing the result as ‘Array: [b’1′ b’2′ b’3′ b’4′ b’8′ b’9′ b’7′]’ and ‘Datatype: |S2’, respectively.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 4 , 8 , 9 , 7 ], dtype = 'S2' )
print ( 'Array:' , arr)
print ( 'Datatype:' , arr.dtype)
|
Output:
In this example, the datatype is a string and the size is 2.
Array: [b'1' b'2' b'3' b'4' b'8' b'9' b'7']
Datatype: |S2
Last Updated :
10 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...