There are many attributes of Numpy array(ndarray) which are discussed below-
1. ndim:- This attribute tells the number of array dimensions. Eg-
import numpy as np x = np.array([1, 2, 3, 4, 5]) print('Number of dimensions in x:', x.ndim) y = np.array([[11, 12, 13, 14], [32, 33, 34, 35]]) print('Number of dimensions in y:', y.ndim) z = np.array([[[11, 12, 13, 14], [32, 33, 34, 35]], [[55, 56, 57, 58], [59, 60, 61, 62]]]) print('Number of dimensions in z:', z.ndim)
Output of the above program
Number of dimensions in x: 1 Number of dimensions in y: 2 Number of dimensions in z: 3
2. size:- This attribute shows the number of elements present in the array. Eg-
import numpy as np x = np.array([1, 2, 3, 4, 5]) print('Number of elements in x:', x.size) y = np.array([[11, 12, 13, 14], [32, 33, 34, 35]]) print('Number of elements in y:', y.size) z = np.array([[[11, 12, 13, 14], [32, 33, 34, 35]], [[55, 56, 57, 58], [59, 60, 61, 62]]]) print('Number of elements in z:', z.size)
Output of the above program
Number of elements in x: 5 Number of elements in y: 8 Number of elements in z: 16
3. dtype:- This attribute gives the data type of Numpy array. Eg-
import numpy as np x = np.array([1, 2, 3, 4, 5]) print('The data type of array x is', x.dtype) y = np.array([[1 + 2j, 14.478], [5 + 11j, 34]]) print('The data type of array y is', y.dtype) z = np.array([[[11.147, 12, 13, 14], [32.984, 33, 34, 35]], [[55, 56.951, 57, 58.369], [59, 60, 61, 62]]]) print('The data type of array z is', z.dtype)
Output of the aove program
The data type of array x is int64 The data type of array y is complex128 The data type of array z is float64
4. itemsize:- This attribute provides the number of bytes required by each array element. Eg-
import numpy as np x = np.array([1, 2, 3, 4, 5], dtype='int8') print('Element byte size of array x is', x.itemsize) y = np.array([[11.5, 12, 13.489, 14.478], [3.2, 33.32, 34, 35]], dtype='float16') print('Element byte size of array y is', y.itemsize) z = np.array([[[11.147, 12, 13, 14], [32.984, 33, 34, 35]], [[55, 56.951, 57, 58.369], [59, 60, 61, 62]]]) print('Element byte size of array z is', z.itemsize)
Output of the above program
Element byte size of array x is 1 Element byte size of array y is 2 Element byte size of array z is 8
The dtype of array x is int8 so each element will need 1 byte.
The dtype of array y is float16 so each element will need 2 bytes.
The dtype of array z is float64 so each element will need 8 bytes.
5. nbytes:- It provides the total number of bytes occupied by array. It is simply a product of itemsize and size attributes.
nbytes = itemsize * size
import numpy as np x = np.array([1, 2, 3, 4, 5], dtype='int8') print('Memory allocated to array x is', x.nbytes) y = np.array([[11.5, 12, 13.489, 14.478], [3.2, 33.32, 34, 35]], dtype='float16') print('Memory allocated to array y is', y.nbytes) z = np.array([[[11.147, 12, 13, 14], [32.984, 33, 34, 35]], [[55, 56.951, 57, 58.369], [59, 60, 61, 62]]]) print('Memory allocated to array z is', z.nbytes)
Output of the above program
Memory allocated to array x is 5 Memory allocated to array y is 16 Memory allocated to array z is 128
The dtype of array x is int8 so total number of bytes occupied is 1*5 = 5 bytes.
The dtype of array y is float16 so total number of bytes occupied is 2*8 = 16 bytes.
The dtype of array z is float64 so total number of bytes occupied is 8*16 = 128 bytes.
6. T:- This attribute returns the transpose of a matrix. It is similar to numpy.transpose()
function.
import numpy as np x = np.array([[12, 14, 43], [511, 34, 2], [21, 5, 90]]) print('Array x:\n', x) print('The transpose of array x is\n', x.T)
Output of the above program
Array x: [[ 12 14 43] [511 34 2] [ 21 5 90]] The transpose of array x is [[ 12 511 21] [ 14 34 5] [ 43 2 90]]