🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to NumPy Notes
Topic #10

NumPy Array Reshape


Reshaping arrays

Reshaping means changing the shape of an array.

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

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


Reshape From 1-D to 2-D

Example

  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)

Reshape From 1-D to 3-D

Example

  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)

print(newarr)

Can We Reshape Into any Shape?

Yes, as long as the elements required for reshaping are equal in both shapes.

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

  import numpy as np

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

  newarr = arr.reshape(3, 3)

print(newarr)

Returns Copy or View?

Example

  import numpy as np

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

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

The example above returns the original array, so it is a view.


Unknown Dimension

You are allowed to have one "unknown" dimension.

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

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

Example

  import numpy as np

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

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

print(newarr)

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


Flattening the arrays

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

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

Example

  import numpy as np

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

newarr
  = arr.reshape(-1)

print(newarr)

Note: There are a lot of functions for changing the shapes of arrays in numpy flatten, ravel and also for rearranging the elements rot90, flip, fliplr, flipud etc. These fall under Intermediate to Advanced section of numpy.

Want to go beyond the notes?

Join CodingNow 2.0's NumPy course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

NumPy Array Reshape – FAQs

Quick answers about learning NumPy Array Reshape in NumPy.

This free note from CodingNow 2.0 explains NumPy Array Reshape in NumPy — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every NumPy topic on CodingNow 2.0, including NumPy Array Reshape, is 100% free with no signup required.
With focused practice, most students grasp NumPy Array Reshape in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now