🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.reshape()

AI & DATA SCIENCE // np-reshape

np.reshape() returns a new view (or, when necessary, a copy) of an array with a different shape, without changing the underlying data or the total number of elements.

Syntax

np.reshape(arr, newshape)
arr.reshape(newshape)

Deep Dive Course

reshape only reorganizes how the same elements are grouped into dimensions — the total element count must stay exactly the same, so a 12-element array can become (3, 4), (4, 3), (2, 6), or (2, 2, 3), but never (3, 5). Passing -1 for one dimension tells NumPy to calculate that dimension automatically from the array's total size and the other specified dimensions, which is convenient when you know some dimensions but not all of them.

1Understanding np.reshape()

reshape only reorganizes how the same elements are grouped into dimensions — the total element count must stay exactly the same, so a 12-element array can become (3, 4), (4, 3), (2, 6), or (2, 2, 3), but never (3, 5). Passing -1 for one dimension tells NumPy to calculate that dimension automatically from the array's total size and the other specified dimensions, which is convenient when you know some dimensions but not all of them.

💡

Use -1 for one dimension in reshape(), e.g. arr.reshape(-1, 1) to make a column vector, instead of computing that dimension yourself — it's less error-prone and adapts automatically if the array's size changes.

editor.html
import numpy as np

arr = np.arange(12)
reshaped = arr.reshape(3, 4)
print(reshaped)
localhost:3000

2Practical Example

Here is a real-world application of np.reshape() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.arange(6)
column = arr.reshape(-1, 1)
print(column)
localhost:3000

3Best Practices

Follow these guidelines when working with np.reshape():

1. Use -1 for exactly one dimension in reshape() to let NumPy infer it, instead of computing and hardcoding that value yourself

2. Remember reshape() returns a view when possible, so mutating the reshaped array can also mutate the original — call .copy() if you need independence

3. Check the total element count matches before reshaping, or catch the resulting ValueError, rather than assuming a reshape will always succeed

⚠️

Tip: Use -1 for one dimension in reshape(), e.g. arr.reshape(-1, 1) to make a column vector, instead of computing that dimension yourself — it's less error-prone and adapts automatically if the array's size changes.

editor.html
import numpy as np

arr = np.arange(12)
reshaped = arr.reshape(3, 4)
print(reshaped)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.arange(12)
reshaped = arr.reshape(3, 4)
print(reshaped)
Example 02Advanced Example
import numpy as np

arr = np.arange(6)
column = arr.reshape(-1, 1)
print(column)

Best Practices

  • Use -1 for exactly one dimension in reshape() to let NumPy infer it, instead of computing and hardcoding that value yourself
  • Remember reshape() returns a view when possible, so mutating the reshaped array can also mutate the original — call .copy() if you need independence
  • Check the total element count matches before reshaping, or catch the resulting ValueError, rather than assuming a reshape will always succeed

Interview Question

What error does NumPy raise if you try to reshape a 10-element array into a shape of (3, 4), and why?

Hint: Think about the constraint reshape() must preserve.

NumPy raises a ValueError, because reshape() requires the new shape to have exactly the same total number of elements as the original array — 3 times 4 is 12, which doesn't match the original 10 elements, so there's no way to redistribute the existing data into that shape without losing or fabricating values. reshape() only ever reorganizes existing elements; it never adds or removes any.

Exercises

MediumPractice using np.reshape() in a real scenario.
View Solution
import numpy as np

arr = np.arange(12)
reshaped = arr.reshape(3, 4)
print(reshaped)

Frequently Asked Questions

What error does NumPy raise if you try to reshape a 10-element array into a shape of (3, 4), and why?

NumPy raises a ValueError, because reshape() requires the new shape to have exactly the same total number of elements as the original array — 3 times 4 is 12, which doesn't match the original 10 elements, so there's no way to redistribute the existing data into that shape without losing or fabricating values. reshape() only ever reorganizes existing elements; it never adds or removes any.

Related Functions

ndarray-shapenp-ravelndarray-flatten