NumPy - BroadcastingYou have a 3D array with shape (4, 1, 3) and a 2D array with shape (1, 5). How can you add them without broadcasting error?AReshape the 2D array to (5, 1) and then addBReshape the 3D array to (4, 5, 3) and then addCAdd them directly without reshapingDReshape the 2D array to (1, 1, 5) and then addCheck Answer
Step-by-Step SolutionSolution:Step 1: Analyze shapes for broadcasting3D array shape is (4,1,3), 2D array shape is (1,5). They differ in last two dims.Step 2: Reshape 2D array to 3D with shape (1,1,5)This allows broadcasting along all dimensions.Final Answer:Reshape the 2D array to (1, 1, 5) and then add -> Option DQuick Check:Match dimensions by adding singleton dims for broadcasting [OK]Quick Trick: Add singleton dimensions to align shapes for broadcasting [OK]Common Mistakes:Reshaping arrays incorrectly causing errorsTrying to add without reshapingMisunderstanding dimension order
Master "Broadcasting" in NumPy9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More NumPy Quizzes Array Data Types - Why dtypes matter for performance - Quiz 13medium Array Data Types - Float types (float16, float32, float64) - Quiz 13medium Array Manipulation - np.newaxis for adding dimensions - Quiz 3easy Array Operations - Comparison operations - Quiz 8hard Creating Arrays - Why array creation matters - Quiz 14medium Creating Arrays - np.ones() for one-filled arrays - Quiz 7medium Creating Arrays - np.ones() for one-filled arrays - Quiz 6medium Creating Arrays - np.linspace() for evenly spaced arrays - Quiz 2easy Indexing and Slicing - Slicing with start:stop:step - Quiz 10hard Indexing and Slicing - Boolean indexing - Quiz 8hard