Checking the Data Type of an Array
-
The NumPy array object has a property called dtype that returns the data type of the array:
-
Example
-
Get the data type of an array object:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr.dtype)
-
Get the data type of an array containing strings:
import numpy as np
arr = np.array([‘apple’, ‘banana’, ‘cherry’])
print(arr.dtype)
Check Number of Dimensions?
-
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.
-
Example
-
Check how many dimensions the arrays have:
-
Program:
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
Difference Between List and Numpy
A common beginner question is what is the real difference here. The answer is performance. Numpy data structures perform better in:
Size:- Numpy data structures take up less space
Performance:- they have a need for speed and are faster than lists
Functionality:- SciPy and Numpy have optimized functions such as linear algebra operations built in.