Bird
0
0

Given arrays:

hard📝 Application Q9 of 15
NumPy - Array Manipulation
Given arrays:
a = np.array([[1], [2], [3]])
b = np.array([[4, 5]])

Which code correctly combines a and b into a single 2D array with shape (3, 3)?
Anp.hstack((a, b))
Bnp.vstack((a, b))
Cnp.vstack([a.T, b])
Dnp.hstack([a, b.T])
Step-by-Step Solution
Solution:
  1. Step 1: Check shapes

    a shape is (3, 1), b shape is (1, 2).
  2. Step 2: Determine stacking method

    To get shape (3, 3), horizontally stack a and b.T (transpose of b) to match rows.
  3. Step 3: Apply np.hstack()

    np.hstack([a, b.T]) stacks arrays horizontally with matching rows.
  4. Final Answer:

    np.hstack([a, b.T]) -> Option D
  5. Quick Check:

    hstack adds columns, transpose aligns rows [OK]
Quick Trick: hstack can broadcast rows if compatible [OK]
Common Mistakes:
  • Trying vstack with incompatible shapes
  • Not considering broadcasting rules
  • Using transpose incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes