How To Change The Shape Of A Numpy Array By Array Reshaping In Python

How To Change The Shape Of A Numpy Array By Array Reshaping In Python

What is the meaning of Array Reshaping

By Reshaping of the array we actually can change the dimension or the shape of an array.

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

By reshaping we can add or remove dimensions or change number of elements in each dimension.

If we want to add or remove dimensions or change the number of elements in each dimension of the array then we can reshape the array to achieve this.


How To Reshape a 1-D Array to 2-D Array  in Numpy Python

We can reshape a 1-D array to 2-D array using the Array Reshape function

Example

In this example we will convert 1-D array which will have 12 elements into a 2-D array

By this array reshaping the outermost dimension will have 4 arrays, which will have 3 elements each

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = arr.reshape(4, 3)

print(newarr)

Output:

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

How to Convert a 1-D Array to 3-D Array By Array Reshaping In Numpy Python

We can easily convert a 1-D Array to 3-D Array by the Array Reshape function

Let's have a look at the example below for this

Example:

In the below example we will the following 1-D array with 12 elements into a 3-D array.

The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = arr.reshape(2, 3, 2)

Output:

[[[ 1  2]
  [ 3  4]
  [ 5  6]]

 [[ 7  8]
  [ 9 10]
  [11 12]]]

Can We Reshape The Array Into any Shape Using Reshape Function In Numpy Python?

You can reshape the elements as per your requirement but the only criteria is that the elements required for reshaping should be eqaul in both shapes.

As an example we can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.

Example:

Let's have a look at the example below

In the example we are trying to convert a 1D array with 8 elements to a 2D array with 3 elements in each dimension and thi will raise an error

import numpy as np

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

newarr = arr.reshape(3, 3)

print(newarr)

Output:

Traceback (most recent call last):

File "demo_numpy_array_reshape_error.py", line 5, in <module>

ValueError: cannot reshape array of size 8 into shape (3,3)

How To Check If The Reshaping Of An Array Returns Copy Or View Of The Array

Example

Check if the returned array is a copy or a view:

import numpy as np

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

print(arr.reshape(2, 4).base)

Output:

[1 2 3 4 5 6 7 8]


Explanation:

As in the above code we have used the base function so after reshaping also it returns the original version of the array only and that means it gives the view of the array.


What Is Unknown Dimension Of An Array

For Numpy array you are allowed to have one dimension as "unknown".

It means that you do not have to specify an exact number for one of the dimensions in the reshape method for the array.

Pass -1 as the value in the reshape function, and NumPy will calculate this number for you.

Example
Convert 1D array with 8 elements to 3D array with 2x2 elements:

import numpy as np

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

newarr = arr.reshape(2, 2, -1)

print(newarr)

Output:

[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

Note: We can not pass -1 to more than one dimension.

How To Convert A Multi Dimensioanl Array Into 1-D Array In Numpy Python

With Flattening of the array you can actually convert a multi dimensional array into a 1D array.

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

Example

Let's try to convert a 2-D array into 1-D array

import numpy as np

arr = np.array([[6, 9, 1], [8, 5, 6]])

newarr = arr.reshape(-1)

print(newarr)

Output

[6 9 1 8 5 6]





Comments