Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Array Manipulation
What is the output of the following code?
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.transpose())
A[[1 4] [2 5] [3 6]]
B[[1 2 3] [4 5 6]]
C[[1 4 2] [3 5 6]]
D[[6 5 4] [3 2 1]]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the original array shape

    The array has shape (2, 3): 2 rows and 3 columns.
  2. Step 2: Apply transpose()

    Transpose swaps axes, so shape becomes (3, 2). Rows become columns and vice versa. The first column becomes the first row: [1,4], second: [2,5], third: [3,6].
  3. Final Answer:

    [[1 4] [2 5] [3 6]] -> Option A
  4. Quick Check:

    Transpose swaps rows and columns [OK]
Quick Trick: Transpose flips rows and columns in 2D arrays [OK]
Common Mistakes:
  • Confusing transpose with reversing elements
  • Expecting original shape after transpose
  • Mixing up rows and columns in output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes