Broadcasting Addition - Python Basics for ML
Implement Broadcasting Addition in Python with NumPy. medium exercise with examples, hints, and instant feedback. No setup or signup required.
Broadcasting Addition
What you'll build
A function broadcast_add(matrix, vector) that adds a vector to every row of a matrix with a single +, letting broadcasting do the repetition.
Background
This is exactly how a neural network layer adds its bias: x @ W has shape (batch, d_out) and the bias b has shape (d_out,). Broadcasting stretches b across the batch without ever copying it, so the same code works for any batch size.
Formula
result[i, j] = matrix[i, j] + vector[j] for every row i
Broadcasting rule
NumPy compares shapes from the right. (m, n) and (n,) line up as (m, n) and (1, n); the size-1 dimension is stretched to m, giving (m, n).
Inputs
matrix: 2D array of shape(m, n)vector: 1D array of shape(n,), with1 <= m, n <= 100. Integer or float entries.
Expected Return Format
A 2D array of shape (m, n): vector added to each row.
Constraints
Use broadcasting; no loops, np.tile or np.repeat.
Follow-up
- How would you add a value to every column instead, with one value per row? (Hint: a vector of shape
(m, 1).) - What error do you get for a
(2, 3)matrix plus a(2,)vector, and why?
Examples
Input: broadcast_add(np.array([[1, 2, 3], [4, 5, 6]]), np.array([10, 20, 30]))
Output: array([[11, 22, 33],
[14, 25, 36]])The vector [10, 20, 30] is added to each of the two rows.
Input: broadcast_add(np.array([[1, 2], [3, 4]]), np.array([-1, -2]))
Output: array([[0, 0],
[2, 2]])Column 0 gets -1 and column 1 gets -2 in every row.
Practice in Python
import numpy as np
def broadcast_add(matrix: np.ndarray, vector: np.ndarray) -> np.ndarray:
"""
Add a vector to each row of a matrix using broadcasting.
Args:
matrix: 2D array of shape (m, n)
vector: 1D array of shape (n,)
Returns:
Array of shape (m, n) with result[i, j] = matrix[i, j] + vector[j]
"""
# Your code here
pass
The interactive editor loads with Python and NumPy. Run your implementation against 5 tests, or use the progressive hints.