Bird
0
0

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📝 Application Q15 of 15
NumPy - Array Manipulation
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?
Aarr.reshape(2, 3, 4)
Barr.reshape(-1, 3, 4)
Carr.reshape(3, -1, 4)
Darr.reshape(2, 4, -1)
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes