Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
NumPy - Array Manipulation
What will be the output of this code?
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([[5], [6]])
result = np.hstack((x, y))
print(result)
A[[1 2] [3 4] [5] [6]]
B[[1 2 5 6] [3 4 5 6]]
C[[1 2 5] [3 4 6]]
DError due to shape mismatch
Step-by-Step Solution
Solution:
  1. Step 1: Check shapes of arrays

    x shape is (2, 2), y shape is (2, 1).
  2. Step 2: Apply np.hstack()

    Stacks arrays horizontally by adding columns, resulting shape (2, 3).
  3. Final Answer:

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

    hstack adds columns, rows must match [OK]
Quick Trick: hstack adds columns, so rows must be equal [OK]
Common Mistakes:
  • Expecting vertical stacking
  • Confusing shapes causing error
  • Misreading output shape

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes