Search

Introduction to Python NumPy Shape

## In This Article, You Will Learn About Python NumPy Shape & Reshape.

Python Numpy Shape – Before moving ahead, let’s know a little bit about Python Numpy Array Copy vs View

The shape of an Array

An array’s shape is the number of elements within each dimension.

Find the Shape of an Array

NumPy arrays include an attribute called shape, which returns a tuple containing each index and the number of elements that correspond to it.

Example – Get the shape of a 2-D array.

```import numpy as np

Array = np.array([[1, 2, 3, 4], [11, 16, 17, 18]])

print(Array.shape)```
```Output -

(2, 4)```

The above example, returns (2,4), indicating that the array has two dimensions and 4 elements.

Example – Make an array of 6 dimensions with ndmin and a vector with the values 1,2,3,4,5 and check that the last dimension has value 5.

```import numpy as np

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

print(Array)
print('shape of array :', Array.shape)```
```Output -

[[[[[[1 2 3 4 5]]]]]]
shape of array : (1, 1, 1, 1, 1, 5)```

Henceforth, it returned array and shape of the array.

What does the shape-tuple signify?

Each index has integers that tell you how many elements it contains.

As index-4 is the value of the above example, we can see that it has 4 elements.

Reshaping of NumPy Array

Restructuring means changing the form of an array.

An array’s shape is the number of elements within each dimension.

Reshaping allows us to add or subtract dimensions, or alter the number of elements within each dimension.

Reshape From 1-D to 2-D

Example – Converting the following 1-D array with 16 elements into a 2-D array.

Result – Each dimension will have a maximum of 4 arrays, each with 4 elements.

```import numpy as np

Array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
new_Array = Array.reshape(4, 4)

print(new_Array)```
```Output -

[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]```

In short, it returned 4 arrays, containing 1×4 elements each.

Reshape From 1-D to 3-D

Example – Converting the 1-D array with 16 elements into a 3-D array.

Result – Each dimension will have 2 arrays that contain 4 arrays, each with 2 elements.

```import numpy as np

Array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13, 14, 15, 16])
new_Array = Array.reshape(2, 4, 2)

print(new_Array)```
```Output -
[[[ 1    2]
[ 3   4]
[ 5   6]
[ 7   8]]

[[ 9     10]
[11     12]
[13     14]
[15     16]]```

In short, it returned each dimension having 2 arrays that contain 4 arrays, each with 2 elements.

Are we able to reshape into any shape?

Yes, provided that the elements used for reshaping can be matched in both forms.

It is possible to reshape an 8-element 1D array into 4 elements in two rows 2D array, but it is not possible to reshape the array into a 4 element 2 rows 2D array. This would require 4×2 = 8 items.

Example – Converting a 1D array of 8 elements into a 2D array using 4 elements per dimension.

```import numpy as np

Array = np.array([1, 2, 3, 4, 5, 6, 7])
new_Array = Array.reshape(2, 4)

print(new_Array)```
```Output -

[[1 2 3 4]
[5 6 7 8]]
```

As shown above, it returned converted array from 1D to 2D array having 1xx4 elements in each dimensions.

Returns Copy or View?

Example – Verify if the array returned is a copy of a view.

```import numpy as np

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

print(Array.reshape(2, 3).base)```
```This example returns the original array, as it is a view.

Output - [1 2 3 4 5 6]```

In the final analysis, it returned the original array.

Unknown Dimensions

You are allowed to have one “unknown” dimension.

This means that you don’t have to give exact dimensions for the reshaping procedure.

Pass -1 to be the value and NumPy will calculate it itself.

Example – Converting 1D array with 8 elements to a 2D array with 1×4 elements:

```import numpy as np

Array = np.array([1, 2, 3, 4, 5, 6, 7, 8])
new_Array = Array.reshape(2, 4, -1)

print(new_Array)```
```Output -

[[[1]
[2]
[3]
[4]]

[[5]
[6]
[7]
[8]]]```

As shown above, it returned 2D array containing 1×4 elements.

Note: It is not possible to pass -1 more than one dimension.

Flattening arrays

A flattening array is the conversion of a multidimensional array to a 1D array.

We can use reshape(-1) to do this.

Example – Converting the array into a 1D array:

```import numpy as np

Array = np.array([[1, 2, 3], [4, 5, 6]])
new_Array = Array.reshape(-1)

print(new_Array)```

As a result, it returned a converted array i.e., 1D array.

Notice: You can modify the arrays in numpy flatten and ravel, as well as rearranging elements such rot90, fliplr, flipud, flip, etc., These functions are included in the Intermediate to Advanced section.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Share on