Easy Ways to Numpy Reshape 3d to 2d Array

Numpy is one of the efficient libraries in python. This module is useful to perform various performances in an array. We can use a NumPy module to traverse an array, iterate an array, etc. Now we are going to learn how to reshape an array using the NumPy module in python. To reshape the NumPy array, we have a built-in function in python called numpy.reshape. We can reshape a one-dimensional to a two-dimensional array, 2d to 3d, 3d to 2d, etc. Here we are only focusing on numpy reshape 3d to 2d array.

Changing the shape of the array without changing the data is known as reshaping. We can add or remove the dimensions in reshaping. numpy.reshape() is an inbuilt function in python to reshape the array. We can reshape into any shape using reshape function. This function gives a new shape to the array.

Syntax for numpy.reshape()

numpy.reshape(a, newshape, order='C')

Parameters

  • a: input array
  • newshape: int or tup of ints
  • order: optional

Returns

reshaped array

Examples

Now we are going to see how to reshape 3d to 2d array. Before that, we will have an example for 3d array and 2d array.

Example for 3d array

[[[ 5  4]
  [ 6  9]]

 [[ 1  0]
  [ 9  5]]

 [[ 4  9]
  [13 19]]

 [[ 8 10]
  [ 1  5]]]
Shape: (4, 2, 2)

Example for 2d array

[[ 5  4  6  9]
 [ 1  0  9  5]
 [ 4  9 13 19]
 [ 8 10  1  5]]
Shape: (4, 4)

Implementation of Numpy reshape 3d to 2d

I hope now you are all clear with 3d and 2d array. Let us move on to the implementation.

Example 1

import numpy as np
array=np.array( [[[ 5,  4],
      [ 6,  9]],

      [[ 1,  0],
      [ 9,  5]],

      [[ 4,  9],
      [13, 19]],
                 
      [[ 8, 10],
      [ 1, 5]]])
reshaped_array=np.reshape(array,(4,4))
print("Original array:\n", array)
print("Reshaped array:\n", reshaped_array)

Import numpy module. Declare a three-dimensional array. Using numpy.reshape function to change the shape of the array. Giving the shape of the reshaped array inside a reshape function.

Output

Original array:
 [[[ 5  4]
  [ 6  9]]

 [[ 1  0]
  [ 9  5]]

 [[ 4  9]
  [13 19]]

 [[ 8 10]
  [ 1  5]]]
Reshaped array:
 [[ 5  4  6  9]
 [ 1  0  9  5]
 [ 4  9 13 19]
 [ 8 10  1  5]]

Example 2

import numpy as np
array=np.array( [[[ 5,  4],
      [ 6,  9]],

      [[ 1,  0],
      [ 9,  5]],

      [[ 4,  9],
      [13, 19]],
                 
      [[ 8, 10],
      [ 1, 5]]])
reshaped_array= array.reshape(array.shape[0], (array.shape[1]*array.shape[2]))
print("Original array:\n", array)
print("Reshaped array:\n", reshaped_array)

Import numpy module. Declare a three-dimensional array. Using numpy.reshape function to change the shape of the array.

Output

Original array:
 [[[ 5  4]
  [ 6  9]]

 [[ 1  0]
  [ 9  5]]

 [[ 4  9]
  [13 19]]

 [[ 8 10]
  [ 1  5]]]
Reshaped array:
 [[ 5  4  6  9]
 [ 1  0  9  5]
 [ 4  9 13 19]
 [ 8 10  1  5]]

Example 3

import numpy as np
array=np.array( [[[ 5,  4],
      [ 6,  9]],

      [[ 1,  0],
      [ 9,  5]],

      [[ 4,  9],
      [13, 19]],
                 
      [[ 8, 10],
      [ 1, 5]]])
reshaped_array=array.reshape(2*2,2*2)
print("Original array:\n", array)
print("Reshaped array:\n", reshaped_array)

Import numpy module. Declare a three-dimensional array. Using numpy.reshape function to change the shape of the array. Giving the shape of the array inside the reshape function. Here we are giving the shape 4 as 2*2.

Output

Original array:
 [[[ 5  4]
  [ 6  9]]

 [[ 1  0]
  [ 9  5]]

 [[ 4  9]
  [13 19]]

 [[ 8 10]
  [ 1  5]]]
Reshaped array:
 [[ 5  4  6  9]
 [ 1  0  9  5]
 [ 4  9 13 19]
 [ 8 10  1  5]]

Example 4: Reshape an array using order ‘F’

import numpy as np
array=np.array( [[[ 5,  4],
      [ 6,  9]],

      [[ 1,  0],
      [ 9,  5]],

      [[ 4,  9],
      [13, 19]],
                 
      [[ 8, 10],
      [ 1, 5]]])
print("Original array:\n",array)
reshaped_array=array.reshape(4,4,order='F')
print("Reshaped array:\n",reshaped_array)

Output

Original array:
 [[[ 5  4]
  [ 6  9]]

 [[ 1  0]
  [ 9  5]]

 [[ 4  9]
  [13 19]]

 [[ 8 10]
  [ 1  5]]]
Reshaped array:
 [[ 5  6  4  9]
 [ 1  9  0  5]
 [ 4 13  9 19]
 [ 8  1 10  5]]

Example 5: Reshape an array using order ‘C’

import numpy as np
array=np.array( [[[ 5,  4],
      [ 6,  9]],

      [[ 1,  0],
      [ 9,  5]],

      [[ 4,  9],
      [13, 19]],
                 
      [[ 8, 10],
      [ 1, 5]]])
print("Original array:\n",array)
reshaped_array=array.reshape(4,4,order='C')
print("Reshaped array:\n",reshaped_array)

Output

Original array:
 [[[ 5  4]
  [ 6  9]]

 [[ 1  0]
  [ 9  5]]

 [[ 4  9]
  [13 19]]

 [[ 8 10]
  [ 1  5]]]
Reshaped array:
 [[ 5  4  6  9]
 [ 1  0  9  5]
 [ 4  9 13 19]
 [ 8 10  1  5]]

Some errors we will face while reshaping an array

In previous programs, you can see that I have given 4,4 as the shape of the new array. Many of us will try to change that value as 3,3. That won’t be possible. Because our array elements are 16 and the size of 3,3 is 9. So we can’t do that.

Let us implement it in an IDE for better understanding.

import numpy as np
array=np.array( [[[ 5,  4],
      [ 6,  9]],

      [[ 1,  0],
      [ 9,  5]],

      [[ 4,  9],
      [13, 19]],
                 
      [[ 8, 10],
      [ 1, 5]]])
reshaped_array=array.reshape(3,3)
print("Original array:\n", array)
print("Reshaped array:\n", reshaped_array)

The Output for this program is

Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python39\shape.py", line 13, in <module>
    reshaped_array=array.reshape(3,3)
ValueError: cannot reshape array of size 16 into shape (3,3)

Bonus: How to calculate the shape of an array

import numpy as np
array=np.array( [[[ 5,  4],
      [ 6,  9]],

      [[ 1,  0],
      [ 9,  5]],

      [[ 4,  9],
      [13, 19]],
                 
      [[ 8, 10],
      [ 1, 5]]])
print("Shape of an array:\n", array.shape)

Output

Shape of an array:
 (4, 2, 2)

1. The given array elements are 16. I am trying to reshape it with the shape(3,3). Is it possible?

No, It is not possible to try to implement that. It will show a value error.

2. Is there any condition to reshape an array? If yes, What is the condition?

Yes, The no of elements and the given shape must be equal to each other.

Conclusion: Numpy reshape 3d to 2d

Now we have completely got an idea about reshaping. Reshaping is nothing but changing the shape of the elements without changing the original data. We hope this article is beneficial to you. In case of any queries, do let us know in the comment section. We will reply to you as soon as possible.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments