NumPy Array Reshaping

NumPy array reshaping simply means changing the shape of an array without changing its data.

Let's say we have a 1D array.

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

We can reshape this 1D array into N-d array as

# reshape 1D into 2D array 
# with 2 rows and 4 columns
[[1 3 5 7]
 [2 4 6 8]]

# reshape 1D into 3D array 
# with 2 rows, 2 columns, and 2 layers
[[[1 3]
  [5 7]]

 [[2 4]
  [6 8]]]

Here, we can see that the 1D array has been reshaped into 2D and 3D arrays without altering its data.


Syntax NumPy Array Reshaping

The syntax of NumPy array reshaping is

np.reshape(array, newshape, order = 'C')

Here,

  • array - input array that needs to be reshaped,
  • newshape - desired new shape of the array
  • order (optional) - specifies the order in which the elements of the array should be arranged. By default it is set to 'C'

Note: The order argument can take one of three values: 'C', 'F', or 'A'. We will discuss them later in this tutorial.


Reshape 1D Array to 2D Array in NumPy

We use the reshape() function to reshape a 1D array into a 2D array. For example,

import numpy as np

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

# reshape a 1D array into a 2D array 
# with 2 rows and 4 columns
result = np.reshape(array1, (2, 4))
print(result)

Output

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

In the above example, we have used the reshape() function to change the shape of the 1D array named array1 into the 2D array. Notice the use of the reshape() function,

np.reshape(array1, (2, 4))

Here, reshape() takes two parameters,

  • array1 - array to be reshaped
  • (2, 4) - new shape of array1 specified as a tuple with 2 rows and 4 columns.

Example: Reshape 1D Array to 2D Array in NumPy

import numpy as np

# reshape a 1D array into a 2D array 
# with 4 rows and 2 columns
array1 = np.array([1, 3, 5, 7, 2, 4, 6, 8])
result1 = np.reshape(array1, (4, 2))
print("With 4 rows and 2 columns: \n",result1)


# reshape a 1D array into a 2D array with a single row
result2 = np.reshape(array1, (1, 8))
print("\nWith a single row: \n",result2)

Output

With 4 rows and 2 columns: 
[[1 3]
 [5 7]
 [2 4]
 [6 8]]

With a single row: 
 [[1 3 5 7 2 4 6 8]]

Note:

  • We need to make sure that the new shape in reshape() has the same number of elements as the original array, else a ValueError will be raised.
  • For example, reshaping an 8 element 1D array into a 2D array of 2 rows and 4 columns is possible, but reshaping it into a 2D array of 3 rows and 3 columns is not possible as that would require 3x3 = 9 elements.

Reshape 1D Array to 3D Array in NumPy

In NumPy, we can reshape a 1D NumPy array into a 3D array with a specified number of rows, columns, and layers. For example,

import numpy as np

# create a 1D array
array1 = np.array([1, 3, 5, 7, 2, 4, 6, 8])

# reshape the 1D array to a 3D array
result = np.reshape(array1, (2, 2, 2))

# print the new array
print("1D to 3D Array: \n",result)

Output

1D to 3D Array: 
[[[1 3]
  [5 7]]

 [[2 4]
  [6 8]]]

Here, since there are 8 elements in the array1 array, np.reshape(array1, (2, 2, 2)) reshapes array1 into a 3D array with 2 rows, 2 columns and 2 layers.


Flatten N-d Array to 1-D Array Using reshape()

Flattening an array simply means converting a multidimensional array into a 1D array.

To flatten an N-d array to a 1-D array we can use reshape() and pass "-1" as an argument.

Let's see an example.

import numpy as np

# flatten 2D array to 1D
array1 = np.array([[1, 3], [5, 7], [9, 11]])
result1 = np.reshape(array1, -1)
print("Flattened 2D array:", result1)

# flatten 3D array to 1D
array2 = np.array([[[1, 3], [5, 7]], [[2, 4], [6, 8]]])
result2 = np.reshape(array2, -1)
print("Flattened 3D array:", result2)

Output

Flattened 2D array: [ 1  3  5  7  9 11]
Flattened 3D array: [1 3 5 7 2 4 6 8]

Here, reshape(array1, -1) and reshape(array2, -1) convert 2-D array and 3-D array into a 1-D array by collapsing all dimensions.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges