NumPy - BroadcastingWhich of the following is the correct syntax to add a 1D array to each row of a 2D array using broadcasting?Aresult = arr2d + arr1dBresult = arr2d + arr1d.reshape(-1, 1)Cresult = arr2d + arr1d.TDresult = arr2d + arr1d.flatten()Check Answer
Step-by-Step SolutionSolution:Step 1: Understand shapes of arraysA 2D array plus a 1D array with matching trailing dimension broadcasts correctly.Step 2: Identify correct syntaxAdding arr1d directly to arr2d adds arr1d to each row by broadcasting.Final Answer:result = arr2d + arr1d -> Option AQuick Check:Direct addition with compatible shapes broadcasts correctly [OK]Quick Trick: Add 1D array directly to 2D array rows if shapes align [OK]Common Mistakes:Using reshape incorrectly causing shape mismatchTransposing 1D arrays which has no effectFlattening arrays unnecessarily
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