Bird
Raised Fist0
NumPydata~20 mins

Why reshaping arrays matters in NumPy - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Reshaping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of reshaping a 1D array to 2D
What is the output of this code when reshaping a 1D array into a 2D array with 2 rows?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
reshaped = arr.reshape(2, 2)
print(reshaped)
A
[[1 2 3]
 [4]]
B
[[1 3]
 [2 4]]
C[1 2 3 4]
D
[[1 2]
 [3 4]]
Attempts:
2 left
💡 Hint
Think about how reshape arranges elements row-wise.
❓ data_output
intermediate
2:00remaining
Shape after reshaping a 3D array
Given a numpy array with shape (2, 3, 4), what is the shape after reshaping it to (3, 8)?
NumPy
import numpy as np
arr = np.zeros((2, 3, 4))
reshaped = arr.reshape(3, 8)
print(reshaped.shape)
A(2, 12)
B(6, 4)
C(3, 8)
D(4, 6)
Attempts:
2 left
💡 Hint
Total elements must remain the same after reshape.
🔧 Debug
advanced
2:00remaining
Identify the error in reshaping
What error does this code raise and why?
NumPy
import numpy as np
arr = np.arange(10)
reshaped = arr.reshape(3, 4)
print(reshaped)
AValueError: cannot reshape array of size 10 into shape (3,4)
BTypeError: reshape argument must be tuple
CIndexError: index out of range
DNo error, prints a 3x4 array
Attempts:
2 left
💡 Hint
Check if total elements match the new shape.
🚀 Application
advanced
2:00remaining
Why reshape is important for matrix multiplication
You have a 1D array of length 6 representing a 2x3 matrix. Which reshaping is needed before multiplying with a 3x2 matrix?
NumPy
import numpy as np
A = np.array([1, 2, 3, 4, 5, 6])
B = np.array([[1, 0], [0, 1], [1, 1]])
# What reshape should be applied to A before np.dot?
AA.reshape(2, 3)
BA.reshape(3, 2)
CA.reshape(6, 1)
DA.reshape(1, 6)
Attempts:
2 left
💡 Hint
Matrix multiplication requires matching inner dimensions.
🧠 Conceptual
expert
3:00remaining
Effect of reshaping on data interpretation
Why is reshaping arrays important when working with image data in machine learning?
AIt converts color images to grayscale.
BIt changes the data layout to match model input requirements without altering pixel values.
CIt compresses the image data to reduce file size.
DIt normalizes pixel values to a 0-1 range automatically.
Attempts:
2 left
💡 Hint
Think about how models expect input shapes.

Practice

(1/5)
1. Why is reshaping an array important in numpy?
easy
A. It sorts the array elements.
B. It deletes elements to reduce size.
C. It converts arrays into lists.
D. It changes the shape without altering the data content.

Solution

  1. Step 1: Understand reshaping purpose

    Reshaping changes how data is organized in rows and columns but keeps all elements intact.
  2. Step 2: Compare options

    Only It changes the shape without altering the data content. correctly states reshaping changes shape without changing data content.
  3. Final Answer:

    It changes the shape without altering the data content. -> Option D
  4. Quick Check:

    Reshape = Change shape, keep data [OK]
Hint: Reshape changes shape, not data values [OK]
Common Mistakes:
  • Thinking reshaping deletes or adds elements
  • Confusing reshaping with sorting
  • Believing reshaping converts data types
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(3, 2)
C. arr.reshape(2, 3)
D. arr.reshape([2, 3])

Solution

  1. Step 1: Recall numpy reshape syntax

    The reshape method takes dimensions as separate arguments, e.g., reshape(2, 3).
  2. Step 2: Check options

    arr.reshape(2, 3) uses correct syntax with comma-separated integers. arr.reshape(3, 2) would create 3 rows and 2 columns instead. The option using a semicolon (;) is invalid syntax. The option passing a list [2, 3] is incorrect as reshape expects separate arguments.
  3. Final Answer:

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

    reshape(rows, cols) = reshape(2, 3) [OK]
Hint: Use commas, not semicolons or lists in reshape() [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Passing a list instead of separate arguments
  • Swapping row and column order
3. What is the shape of the array after executing this code?
import numpy as np
arr = np.arange(12)
reshaped = arr.reshape(3, 4)
print(reshaped.shape)
medium
A. (3, 4)
B. (2, 6)
C. (12,)
D. (4, 3)

Solution

  1. Step 1: Understand the code

    arr is created with 12 elements from 0 to 11. Then reshaped into 3 rows and 4 columns.
  2. Step 2: Determine the shape

    The reshape(3, 4) changes shape to (3, 4). Printing shape outputs this tuple.
  3. Final Answer:

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

    reshape(3,4) shape = (3, 4) [OK]
Hint: reshape(3,4) means 3 rows, 4 columns [OK]
Common Mistakes:
  • Confusing rows and columns order
  • Expecting original shape (12,)
  • Mixing up shape tuple format
4. What error will occur when running this code?
import numpy as np
arr = np.arange(10)
reshaped = arr.reshape(3, 4)
medium
A. ValueError: cannot reshape array of size 10 into shape (3,4)
B. TypeError: reshape arguments must be integers
C. SyntaxError: invalid syntax
D. No error, reshaping works fine

Solution

  1. Step 1: Check array size and reshape target

    arr has 10 elements. Reshape tries to make shape (3,4) which needs 12 elements.
  2. Step 2: Identify error type

    Since total elements mismatch, numpy raises ValueError about impossible reshape.
  3. Final Answer:

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

    Mismatch size causes ValueError [OK]
Hint: Total elements must match for reshape [OK]
Common Mistakes:
  • Ignoring total element count mismatch
  • Expecting reshape to truncate or pad data
  • Confusing ValueError with SyntaxError
5. Given a numpy array arr = np.arange(24), which reshape command correctly reshapes it into a 3D array with shape (2, 3, 4) using automatic dimension calculation?
hard
A. arr.reshape(2, 3, 4)
B. arr.reshape(-1, 3, 4)
C. arr.reshape(3, -1, 4)
D. arr.reshape(2, 4, -1)

Solution

  1. Step 1: Understand array size and target shape

    arr has 24 elements. Target shape is (2, 3, 4) which multiplies to 24.
  2. Step 2: Use -1 to let numpy calculate dimension

    Using -1 lets numpy infer that dimension. arr.reshape(-1, 3, 4) uses -1 in first dimension, so shape becomes (2,3,4) automatically.
  3. Step 3: Check other options

    arr.reshape(2, 3, 4) works but does not use automatic calculation. arr.reshape(3, -1, 4) results in (3, 2, 4). arr.reshape(2, 4, -1) results in (2, 4, 3).
  4. Final Answer:

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

    Use -1 for auto dimension: reshape(-1, 3, 4) [OK]
Hint: Use -1 where you want numpy to infer dimension [OK]
Common Mistakes:
  • Placing -1 in wrong dimension causing errors
  • Not matching total elements after reshape
  • Assuming multiple -1s allowed in reshape
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