What are NumPy Array Attributes ?
NumPy Array Attributes
A). The shape of a NumPy array:
The number of elements in each dimension is termed as the shape of an array. NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements. This can also be used to resize the array.
Integers at every index tell about the number of elements the corresponding dimension has.
Example1:
import numpy as np
arr= np.array([ [[7, 8, 9],[4, 5, 6],[1, 2, 3]],
[[10, 11, 12] , [16, 17, 18], [22, 23, 24]],
[[13, 14, 15] , [19, 20, 21], [25,26,27]]] )
print(arr.shape)
#output:(3,3,3)
# the outermost dimension will have 3 arrays that contains 3 arrays , each with 3 elements.
Example2:
import numpy as np
arr1 = np.array([ [11.5, 12.9, 13.8], [11,12,13] ])
print(arr1.shape)
#output: (2,3)
#this represents 2 dimensional array and in each dimension there are three elements.
B). Reshaping an array:
Reshaping of an array means changing the shape of an array. By this, we can add or remove dimensions or change the number of elements in each dimension.
We can reshape from one dimension to another dimension for example:
From 1-D to 2-D or from 1-D to 3-D.
For Example:
1. reshaping 1-D to 2-D
Converting 1-D array with 10 elements into a 2-D array. We want the resultant array of shape (5,2) i.e the outermost dimension will have 5 arrays with 2 elements in each.
Code:
import numpy as np
arr1 = np.array([10,20,30,40,50,60,70,80,90,100])
newarr1= arr1.reshape(5,2)
print(newarr1)
Output:
[[ 10 20]
[ 30 40]
[ 50 60]
[ 70 80]
[ 90 100]]
2. reshaping 1-D to 3-D
Converting 1-D array with 12 elements into a 3-D array. We want the resultant array of shape (2,3,2) i.e the outermost dimension will have 2 arrays that contain 3 arrays, each with 2 elements.
Code:
import numpy as np
arr1 = np.array([10,20,30,40,50,60,70,80,90,100,110,120])
newarr1= arr1.reshape(2,3,2)
print(newarr1)
Output:
[[[ 10 20]
[ 30 40]
[ 50 60]]
[[ 70 80]
[ 90 100]
[120 130]]]
C). NumPy size() function:
numpy.size function returns the count of the number of elements along a given axis.
Syntax:
numpy.size(arr, axis=None)
It takes two parameters first one is arr, this is the input data or the array.
And the second one is axis[int, optional], this represents the axis along which the elements are counted, by default gives the total number of elements.
Code:
import numpy as np
arr1 = np.array([[10,20,30,40,50],[60,70,80,90,100]])
#by default it will give the total number of elements in the array.
print(np.size(arr1))
#output: 10
#count the number of elements along the axis
print(np.size(arr1,0))
#output : 2
print(np.size(arr1,1))
#output : 5
D). NumPy ndim() :
numpy.ndarray.ndim() function returns the number of dimensions of an array.
Syntax:
numpy.ndarray.ndim(arr)
It takes the input array as a parameter and returns the number of dimensions of the arr.
Example1:
import numpy as np
arr1 = np.array([10,20,30,40,50])
newarr1= arr1.ndim
print(newarr1)
#output: 1
Example2:
import numpy as np
arr= [[10,20], [30,40]]
arr1=np.ndim(arr)
print(arr1)
#output: 2
E). Data type object(dtype):
Every ndarray has an associated data type (dtype) object. For more details on the numpy data type, I would suggest a reading article on NumPy Data Type.
- It returns the data type of the array. (integer, float, etc.)
- Size of data(no. of bytes)
Example 1:
import numpy as np
arr1 = np.array([10,20,30,40,50])
print(arr1.dtype)
#output: int32
Example 2:
import numpy as np
arr1 = np.array(['a','b', 'c'])
print(arr1.dtype)
#output: <U1
F). Itemsize() method:
numpy.ndarray.itemsize() function returns the size(in bytes) of each array element.
Syntax:
numpy.ndarray.itemsize(arr)
it takes the input array as a parameter and returns the integer length of one array element in bytes.
For Example:
import numpy as np
arr1 = np.array([10,20,30,40])
print(arr1.itemsize)
#output:4
G). nbytes :
numpy.ndarray.nbytes returns the total size (in bytes) of the array.
For Example:
import numpy as np
arr1 = np.array([10,20,30,40])
print(arr1.nbytes)
#output:16
Thanks for reading this article, I hope you like it.. stay tuned with us for more articles like this.
Till now, happy learning


