NumPy - BroadcastingGiven arrays with shapes (2, 1, 4) and (3, 4), what is the broadcasted shape when adding them?AError: shapes not compatibleB(2, 3, 4)C(2, 1, 4)D(3, 4)Check Answer
Step-by-Step SolutionSolution:Step 1: Align shapes right to leftShapes: (2,1,4) and (3,4) align as (2,1,4) and (1,3,4).Step 2: Check compatibility and broadcast shapeDimensions: 4=4 equal, 1 and 3 (one is 1, compatible), 2 and 1 (one is 1, compatible). Broadcasted shape: max(2,1)=2, max(1,3)=3, 4 -> (2,3,4).Final Answer:The broadcasted shape is (2, 3, 4) -> Option BQuick Check:Broadcast shape = (2, 3, 4) [OK]Quick Trick: Align shapes right to left; missing dims treated as 1 [OK]Common Mistakes:Assuming missing leading dims are ignoredConfusing dimension orderAssuming broadcasting always works if some dims match
Master "Broadcasting" in NumPy9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More NumPy Quizzes Aggregation Functions - np.min() and np.max() - Quiz 15hard Array Manipulation - np.vstack() and np.hstack() - Quiz 11easy Array Manipulation - np.newaxis for adding dimensions - Quiz 12easy Broadcasting - Common broadcasting patterns - Quiz 9hard Creating Arrays - np.array() from Python lists - Quiz 13medium Creating Arrays - np.random.rand() and random arrays - Quiz 11easy Indexing and Slicing - Indexing returns views not copies - Quiz 8hard Indexing and Slicing - Why indexing matters - Quiz 13medium Indexing and Slicing - Fancy indexing with integer arrays - Quiz 7medium NumPy Fundamentals - Installing and importing NumPy - Quiz 2easy