Python NumPy provides many attributes for arrays, some examples are given below.
Find the dimensions of array
- We can use ndim attribute to find dimensions of an array
1 2 3 4 5 6 7 8 9 10 |
# import numpy library as np import numpy as np # Create an array of my_array = np.arange(1, 30, 3) # Above will create an array like this [ 1 4 7 10 13 16 19 22 25 28] # Find the number of dimensions my_array.ndim |
The above code will output this result
1
Find the dimensions values
- We can use shape attribute to find the total number of values in each dimension of an array
1 2 |
# how many values in each dimension my_array.shape |
The above code will output this result
(10,)
Find the total elements of array
- We can use size attribute to find total number of values in an array
1 2 |
# total elements in an array my_array.size |
The above code will output this result
10