DEV Community

TechPlygrnd
TechPlygrnd

Posted on

NumPy Tutorial #6: Array Shape

What is "shape" in NumPy array? The shape of an array can be referred to the number of elements in each dimension in the array.


Shape

Luckly, we don't have to perform any iteration process to count the number of elements in each array dimension. NumPy has provided shape method to perform the counting process. Let see how it works through an example

Let create an array (from now on I will refer array an NumPy array)

import numpy as np

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

You can get the shape of arr and print the shape to generate an output

print(arr.shape)
Enter fullscreen mode Exit fullscreen mode

You can see the following output

(2, 3)
Enter fullscreen mode Exit fullscreen mode

The output shows a tuple that described there are two dimensions in the array. In the second dimension, it has two items, and the first dimension it has 3 items.


There you go, that is how you get the shape of an array. Thank you for reading this blog and have a good day!

Top comments (0)