Array Attributes

There are some important attributes we are going to discuss over here. Just don’t confuse or muddle up with some, as some of them almost have similar functions. One or two attributes are already been used in some previous exercises and we are going to discuss them in detail now.
1. ndim: It will return the rank or dimensions of an array. All these attributes will be clear to you through an example that will be discused below.
2. shape: It will return the shape of an array i.e, the number of rows and number of columns present in the ndarray.
3. size: It will return the no. of elements present in the array. It is just nothing it is the product of tuple elements returned by shape attribute i.e. if ndarray.shape returned (3,4) then ndarray.size will return 3*4 =12.
4. dtype: This is one of the amazing attributes of Numpy as it will return the data type of the elements stored in your Numpy array.
5. data: The buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.

Let’s create a numpy array and try all the attributes on that.

import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

It will return the no. of axes so here 2 because it has rows and columns

(4, 3)
<memory at 0x074C8738>

Leave a Comment

Your email address will not be published. Required fields are marked *