Table of Contents



Introduction

The NumPy ndarray object has a number of important attributes. In this section, we will discuss several important ones:

Attribute Description
ndim Number of array dimensions.
shape A tuple of integers representing the size of the array in each dimension.
size Total number of elements in the array.
T Transpose of the array.
dtype Data type of the elements of the array.
itemsize Size (in bytes) of each element of the array.
nbytes Total size (in bytes) of the array:

The NumPy Array ndim Attribute

The ndim attribute of an ndarray object returns the number of array dimensions of the array.

Example

Using the ndim attribute for a 1-D array.

1import numpy as np
2x1 = np.zeros((4),dtype=int)
3print(x1)
4print(x1.ndim)
[0 0 0 0]

1

Example

Using the ndim attribute for a 2-D array.

1x2 = np.zeros((3, 4),dtype=int)
2print(x2)
3print(x2.ndim)
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

2

Example

Using the ndim attribute for a 3-D array.

1x3 = np.zeros((2, 3, 4),dtype=int)
2print(x3)
3print(x3.ndim)
[[[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]

 [[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]]

 3

In the last example, we created a $2\times 3\times 4$ NumPy array using the numpy.zeros() function. Notice how the output is printed. The array has 3 rows, 4 columns and 2 frames. The output is printed frame by frame. The number of dimensions as computed by the ndim attribute is 3.

The NumPy Array shape Attribute

The shape attribute of an ndarray object returns the shape of an array - in the form of a tuple that gives the lengths of the corresponding array dimensions.

Example

Using the shape attribute.

1print(x1.shape)
2print(x2.shape)
3print(x3.shape)
(4,)
(3, 4)
(2, 3, 4)

The NumPy Array size Attribute

The size attribute of an ndarray object returns the total number of elements in the array which is equal to the product of the array’s dimensions.

Example

Using the size attribute.

1print(x1.size)
2print(x2.size)
3print(x3.size)
4
12
24

Or more illustratively, using f-strings:

1print(f'the size of x1{x1.shape} is {np.size(x1)}')
2print(f'the size of x2{x2.shape} is {x2.shape[0]}X{x2.shape[1]}={x2.size}')
3print(f'the size of x3{x3.shape} is {x3.shape[0]}X{x3.shape[1]}X{x3.shape[2]}={x3.size}')
the size of x1(4,) is 4
the size of x2(3, 4) is 3X4=12
the size of x3(2, 3, 4) is 2X3X4=24

ADVERTISEMENT



The NumPy Array T Attribute

The T attribute of an ndarray object returns the tranpose of the array.

Example

Using the T attribute to compute the transpose of an array.

1x = np.array([[1.,2.],[3.,4.]])
2print(x)
3print(x.T)
[[1. 2.]
 [3. 4.]]

[[1. 3.]
 [2. 4.]]
Tip

The transpose attribute has no effect on a 1-D array.

The NumPy Array dtype Attribute

The dtype attribute of an ndarray object returns the data type of the array’s elements.

Example

Elements are entered as floats.

1a = np.array([[1.,2.],[3.,4.]])
2a.dtype
dtype('float64')

Example

Elements are entered as integers.

1b = np.array([[1,2],[3,4]])
2b.dtype
dtype('int32')

Example

Specify the dtype explicity.

1c = np.array([[1,2],[3,4]], dtype=np.float32)
2c.dtype
dtype('float32')

The NumPy Array nbytes Attribute

The nbytes attribute of an ndarray object returns the total bytes consumed by the elements of the array.

Example

Computing the total memory consumed by an array using nbytes attribute.

1y1 = np.ones((10,10,10), dtype=np.int64)
2y1.nbytes
8000

On the other hand, if dtype is now specified as np.int32, the memory consumed will be halved.

1y2 = np.ones((10,10,10), dtype=np.int32)
2y2.nbytes
4000

The NumPy Array itemsize Attribute

The itemsize attribute of an ndarray object returns the bytes consumed by an element of the array. This quantity can also be computed as $$\boxed{\text{numpy.ndarray.itemsize} = \frac{\text{numpy.ndarray.nbytes}}{\text{numpy.ndarray.size}}}$$

Example

Using the itemsize attribute.

1y2 = np.ones((10,10,10), dtype=np.int32)
2
3print(f'The total size of the array is {y2.nbytes} bytes. ')
4print(f'Each element consumes  {y2.nbytes}/{y2.size} = {y2.itemsize} bytes.')
The total size of the array is 4000 bytes.
Each element consumes  4000/1000 = 4 bytes.