Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
NumPy - Array Manipulation
Identify the error in this code snippet:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
transposed = arr.transpose(axes=(1, 0, 2))
print(transposed)
AThe transpose method cannot be called on 2D arrays.
BThe axes tuple length does not match the array's number of dimensions.
CThe axes argument should be a list, not a tuple.
DThe array must be flattened before transposing.
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    Array shape is (2, 2), so it has 2 dimensions.
  2. Step 2: Check axes argument

    Axes tuple has length 3: (1, 0, 2).
  3. Step 3: Identify mismatch

    Axes length must equal number of dimensions; here it does not.
  4. Final Answer:

    The axes tuple length does not match the array's number of dimensions. -> Option B
  5. Quick Check:

    Axes length == array.ndim? [OK]
Quick Trick: Axes length must match array dimensions [OK]
Common Mistakes:
  • Passing axes with incorrect length
  • Using invalid axes parameter names
  • Assuming transpose requires flattening

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes