HOW TO GET THE SHAPE OF AN ARRAY IN PYTHON NUMPY?

WHAT IS SHAPE OF AN ARRAY IN PYTHON NUMPY?

The shape of an array is the number of elements in each dimension.

Getting the Shape of an Array-

shape attribute of python numpy returns a tuple with each index having the number of corresponding elements.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr.shape)

Output-

(2, 3)

Explanation-

The above example returns shape tuple – (2, 3), which means that the array-arr has 2 dimensions, where the first dimension has 2 elements and the second has 3 elements. Integers in shape tuple at every index tells about the number of elements the corresponding dimension has.


Posted

in

Tags:

Comments

Leave a Reply