Having learned what Numpy is, how to create arrays in Numpy, how to manipulate array through different Numpy functions, now we want to show how you can get information about arrays. This means the properties or attributes of arrays such as size of the array, number of elements of the array, what byte size of elements and so on. This is essential when you are working with algorithms and scientific works that involves array(matrices).

Here are list of attributes that we gonna show here:

numpy array attributes

  • Shape of array
  • Size of array
  • Data type objects of array
  • Dimension of array
  • Number of array elements
  • Number of bytes
In all the examples we will be using the following A array:

$ A
array([[0, 1],
       [2, 3]])

 Shape of an array: shape

The shape of an array can be found out using the shape attribute. The shape of an array means the row x column size.

$ A.shape
> (2, 2)

this means that the array A has 2 rows and 2 columns.

Size of an Array: size

The size of an array like A means how many elements are there inside the array.

$ A.size
> 4

Data Type: dtype

All arrays in Numpy are objects called ndarray. dtype attribute of ndarray returns the data type of the object. For example the array A is an ndarray object in Numpy. To find out the data type object of this array A we use the dtype attribute.

$ A.dtype
> dtype('int32')

Dimension of array: ndim

The ndim attribute provides us the information about the dimension of the array.

$ A.ndim
> 2

Number of array elements: itemsize

To find out the number of elements in an array like A we use the itemsize attribute.

$ A.itemsize
> 4

Number of byte of array: nbytes

To find out the number of bytes in an array we use the nbytes attribute.

$ A.nbytes
> 16

which is the same as number of elements in the array times the byte size of each element-

$ A.itemsize*A.size
> 16


Post a Comment