This is a detailed tutorial of the NumPy Array Shape. A number of Illustrative examples are given to give you better clarity of the idea of Array Shape.

As an array mainly contains elements in any dimension. The dimension in which array can have elements can be a single dimension, 2-D or 3-D and also many other dimensions. So the shape of the array is the number of elements that are present in each dimension of the array.

In order to get the shape of the array, we have an attribute in NumPy. This attribute is known as `shape`

. This will help us return a tuple with every possible index. It will also return each index with a number of Correlation elements. It usually helps in getting information about the ongoing shape of the array.

Also, it may help in reshaping the array as per our specific requirements. This is possible by giving values for the tuple of an array dimension.

Reshaping an array can, in some cases, fail. The case in which it can fail is when we need to have a copy of the array.

### Example 1

Let us go through an example to have more clarity on this topic:

# importing the numpy package by making an alias as np import numpy as np # Creating a 2-D array array1=np.array([[9,8,7,6,5],[0,1,2,3,4]]) # Now we will get the shape of the array by using shape attribute print(array1.shape)

**Output:**

(2, 5)

The output is (2, 5) in which the number 2 is defining the number of elements in the first dimension, and number 4 is defining the number of elements in the second dimension of the array.

### Example 2

Another example with a 3-D array:

# importing the numpy package by making an alias as np import numpy as np # Creating a 3-D array array1=np.array([[[9,8,7,6,5],[0,1,2,3,4]],[[9,8,7,6,5],[0,1,2,3,4]],[[9,8,7,6,5],[0,1,2,3,4]]]) # Now we will get the shape of the array by using shape attribute print(array1.shape)

**Output:**

(3, 2, 5)

Here the output is (3, 2, 5) where the first number represents the dimension of the array with three elements in it, the second number represents the number of elements present in the second dimension, and the third element presents the inside third dimension array the number of elements.

### Example 3

We will take another example where will first define the dimension of the array and then get the shape of the array:

# importing the numpy package by making an alias as np import numpy as np # Creating a array and also define number of dimensions. array1=np.array([9,8,7,6,5],ndmin=4) # Now we will get the shape of the array by using shape attribute. print(array1) print(array1.shape)

Output:

[[[[9 8 7 6 5]]]] (1, 1, 1, 5)

Now in this example, we know that we have only one element in every dimension except the innermost dimension, which has five elements.

### Example 4

Let us go through another example:

# importing the numpy package by making an alias as np import numpy as np # Creating an array and also define number of dimensions. array1=np.array([[9,8,7,6,5],[0,1,2,3,4]],ndmin=5) # Now we will get the shape of the array by using shape attribute. print(array1) print(array1.shape)

**Output:**

[[[[[9 8 7 6 5] [0 1 2 3 4]]]]] (1, 1, 1, 2, 5)

In this output, we have one element in every dimension except the second last and last, where we have two elements in the second last and five elements in the last dimension.

### Example 5

let us go through another example with 3-D arrays:

# importing the numpy package by making an alias as np import numpy as np # Creating a 3-D array array1=np.array([[[9,8,7,6,5],[0,1,2,3,4]],[[9,8,7,6,5],[0,1,2,3,4]],[[9,8,7,6,5],[0,1,2,3,4]]],ndmin=7) # Now we will get the shape of the array by using shape attribute \ print(array1) print('Shape of the Array=',array1.shape)

Output:

[[[[[[98765] [01234]] [[98765] [01234]] [[98765] [01234]]]]]]] Shape of the Array= (1,1,1,1,3,2,5)

In this example, we took a 3-D array, and we have given its dimension to be seven. And then we are trying to get the shape of the array.

So as per the result, we see that up till the fourth dimension we have only one element, but in the third last dimension we have three elements, and also in the second last dimension, we have two elements. Also, in the last dimension, we have five elements.

This is how we can give shape to the arrays. Also, we can define the shape of the array by going through the dimensions and the number of the elements.

### Tuple Shape represents What?

While defining the shape of the arrays, we get certain integers. And each integer tells us about the number of elements present in every dimension. These numbers present the capacity of each dimension of the array.

### Example 6

Let us take an example:

# importing the numpy package by making an alias as np import numpy as np # Creating a 2-D array array1=np.array([9,8,7,6,5], ndmin=2) # Now we will get the shape of the array by using shape attribute print(array1.shape)

Output:

(1, 5)

In this example, we see that we have two dimensions. The first dimension has one element, and the second dimension has five elements in it, so we can say that every tuple decides the number of element presents in that dimension.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!