Bird
0
0

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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes