Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q14 of 15
NumPy - Array Manipulation
What is wrong with this code snippet?
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([5, 6, 7])
result = np.concatenate((x, y), axis=0)
print(result)
AError because y must be reshaped to 3D
BNo error, output is concatenated array
CError because axis=0 is invalid for 2D arrays
DError because arrays have incompatible shapes along axis=0
Step-by-Step Solution
Solution:
  1. Step 1: Check shapes of arrays

    x shape is (2, 2), y shape is (3,). They differ in both dimensions.
  2. Step 2: Understand concatenate axis rules

    Arrays must match in all dimensions except the concatenation axis. Here, axis=0 means rows, but y has no matching columns.
  3. Final Answer:

    Error because arrays have incompatible shapes along axis=0 -> Option D
  4. Quick Check:

    Shapes must match except on concat axis [OK]
Quick Trick: Check array shapes match except on concat axis [OK]
Common Mistakes:
  • Ignoring shape mismatch errors
  • Assuming 1D array can concatenate with 2D without reshape
  • Misunderstanding axis parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes