Bird
Raised Fist0
NumPydata~10 mins

reshape() for changing dimensions in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - reshape() for changing dimensions
Start with 1D or ND array
↓
Call reshape(new_shape)
↓
Check if total elements match
↓
Create new array
↓
Return reshaped array
reshape() changes the shape of an array if the total number of elements stays the same. If not, it gives an error.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
new_arr = arr.reshape((2, 3))
print(new_arr)
This code changes a 1D array of 6 elements into a 2x3 2D array.
Execution Table
StepActionInput ShapeNew ShapeCheck Total ElementsResult
1Create array(6,)--Array with elements [1 2 3 4 5 6]
2Call reshape((2,3))(6,)(2,3)6 == 2*3 (6)Pass
3Reshape array(6,)(2,3)-New array: [[1 2 3] [4 5 6]]
4Print new array(2,3)--[[1 2 3] [4 5 6]]
5Try invalid reshape(6,)(3,4)6 != 3*4 (12)Error: cannot reshape array of size 6 into shape (3,4)
💡 Execution stops after error because reshape dimensions do not match total elements.
Variable Tracker
VariableStartAfter reshape((2,3))After failed reshape((3,4))
arr[1 2 3 4 5 6][1 2 3 4 5 6][1 2 3 4 5 6]
new_arrN/A[[1 2 3] [4 5 6]]Error - no change
Key Moments - 2 Insights
Why does reshape((3,4)) cause an error when the original array has 6 elements?
Because 3*4=12 which is not equal to the original 6 elements. reshape() requires total elements to match exactly (see execution_table row 5).
Does reshape() change the data or just the shape?
reshape() only changes the shape, not the data order or values (see execution_table row 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the shape of new_arr after step 3?
A(2, 3)
B(3, 2)
C(6,)
D(3, 4)
💡 Hint
Check the 'New Shape' and 'Result' columns at step 3 in the execution_table.
At which step does the reshape operation fail due to incompatible dimensions?
AStep 2
BStep 5
CStep 3
DStep 4
💡 Hint
Look for the 'Error' message in the 'Result' column of the execution_table.
If the original array had 12 elements, which reshape would NOT cause an error?
Areshape((2, 3))
Breshape((4, 4))
Creshape((3, 4))
Dreshape((1, 5))
💡 Hint
Check the total elements needed for each shape and compare with 12.
Concept Snapshot
reshape() changes the shape of a numpy array without changing data.
Syntax: array.reshape(new_shape)
Total elements must match: product of new_shape == original size.
Raises error if sizes mismatch.
Useful to convert 1D arrays to 2D or higher dimensions.
Full Transcript
The reshape() function in numpy changes the shape of an array while keeping the same data. It requires that the total number of elements before and after reshaping is the same. For example, a 1D array with 6 elements can be reshaped into a 2x3 2D array. If you try to reshape into a shape that does not match the total elements, numpy raises an error. The data order stays the same; only the shape changes.

Practice

(1/5)
1. What does the reshape() function do in NumPy?
easy
A. Creates a new array with random values
B. Changes the shape of an array without changing its data
C. Sorts the elements of an array
D. Deletes elements from an array

Solution

  1. Step 1: Understand the purpose of reshape()

    The reshape() function changes the shape (dimensions) of an existing array but keeps the data intact.
  2. Step 2: Compare with other options

    Options A, B, and C describe different functions or actions unrelated to reshaping.
  3. Final Answer:

    Changes the shape of an array without changing its data -> Option B
  4. Quick Check:

    reshape() = change shape only [OK]
Hint: reshape() changes shape, data stays same [OK]
Common Mistakes:
  • Thinking reshape() changes data values
  • Confusing reshape() with sorting or deleting
  • Assuming reshape() creates new random arrays
2. Which of the following is the correct syntax to reshape a NumPy array arr into 2 rows and 3 columns?
easy
A. arr.reshape(2[3])
B. arr.reshape{2, 3}
C. arr.reshape(2, 3)
D. reshape(arr, 2, 3)

Solution

  1. Step 1: Recall correct reshape syntax

    The correct syntax is array.reshape(dim1, dim2, ...) with dimensions as separate arguments or a tuple.
  2. Step 2: Evaluate options

    arr.reshape(2, 3) uses correct method call with separate arguments. arr.reshape(2[3]) has invalid syntax (missing comma). arr.reshape{2, 3} uses curly braces which is wrong syntax. reshape(arr, 2, 3) uses a function call without object which is invalid.
  3. Final Answer:

    arr.reshape(2, 3) -> Option C
  4. Quick Check:

    Use dot and parentheses with dims [OK]
Hint: Use dot and parentheses with dims, no brackets [OK]
Common Mistakes:
  • Forgetting comma between dimensions
  • Using curly braces instead of parentheses
  • Calling reshape as a standalone function
3. What is the output shape of the following code?
import numpy as np
arr = np.arange(12)
new_arr = arr.reshape(3, 4)
print(new_arr.shape)
medium
A. (3, 4)
B. (4, 3)
C. (12,)
D. (2, 6)

Solution

  1. Step 1: Understand the original array

    np.arange(12) creates a 1D array with 12 elements from 0 to 11.
  2. Step 2: Reshape to (3, 4)

    The reshape changes the array to 3 rows and 4 columns, so the shape becomes (3, 4).
  3. Final Answer:

    (3, 4) -> Option A
  4. Quick Check:

    reshape(3,4) = (3,4) shape [OK]
Hint: reshape dims become shape tuple [OK]
Common Mistakes:
  • Confusing rows and columns order
  • Thinking reshape changes total elements
  • Expecting original 1D shape after reshape
4. What error will occur when running this code?
import numpy as np
arr = np.arange(10)
new_arr = arr.reshape(3, 4)
medium
A. ValueError: cannot reshape array of size 10 into shape (3,4)
B. TypeError: reshape() takes exactly 1 argument
C. SyntaxError: invalid syntax
D. No error, reshaping works fine

Solution

  1. Step 1: Check original array size

    np.arange(10) creates an array with 10 elements.
  2. Step 2: Check reshape dimensions

    Trying to reshape into (3, 4) means 12 elements needed, but only 10 exist.
  3. Step 3: Identify error type

    This mismatch causes a ValueError about incompatible shape.
  4. Final Answer:

    ValueError: cannot reshape array of size 10 into shape (3,4) -> Option A
  5. Quick Check:

    Elements must match for reshape [OK]
Hint: Total elements must match for reshape [OK]
Common Mistakes:
  • Ignoring total element count mismatch
  • Expecting reshape to auto-fill missing elements
  • Confusing ValueError with SyntaxError
5. Given a 1D NumPy array arr = np.arange(24), which of the following reshapes it into a 3D array with shape (2, 3, 4) using automatic dimension calculation with -1 in the first position?
hard
A. arr.reshape(2, 3, 4)
B. arr.reshape(2, -1, 4)
C. arr.reshape(2, 3, -1)
D. arr.reshape(-1, 3, 4)

Solution

  1. Step 1: Understand total elements and target shape

    The array has 24 elements. The target shape is 3D with dimensions (2, 3, 4), total 24 elements.
  2. Step 2: Use -1 for automatic dimension

    Using -1 lets NumPy calculate that dimension automatically. For (2, 3, 4), if we replace one dimension with -1, NumPy calculates it as 24 divided by product of other dims.
  3. Step 3: Check each option

    arr.reshape(2, 3, 4) is explicit (2,3,4) - works but no automatic calculation.
    arr.reshape(2, -1, 4): (2, -1, 4) means -1 = 24/(2*4) = 3, shape (2,3,4) correct.
    arr.reshape(-1, 3, 4): (-1, 3, 4) means -1 = 24/(3*4) = 2, shape (2,3,4) correct.
    arr.reshape(2, 3, -1): (2, 3, -1) means -1 = 24/(2*3) = 4, shape (2,3,4) correct.
    All B, C, D produce correct shape, but question asks for reshaping into (2,3,4) using automatic dimension calculation. Only arr.reshape(-1, 3, 4) uses -1 as first dimension, matching question wording.
  4. Final Answer:

    arr.reshape(-1, 3, 4) -> Option D
  5. Quick Check:

    -1 auto calculates dimension [OK]
Hint: Use -1 for one dimension to auto calculate [OK]
Common Mistakes:
  • Using multiple -1 values in reshape
  • Not matching total elements with new shape
  • Confusing which dimension -1 replaces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard