Broadcasting lets you do math on arrays of different sizes easily. It saves time and code by matching shapes automatically.
Adding a single number to every element in a list of numbers.
Multiplying each row of a table by a different number.
Subtracting a small array from a bigger array without writing loops.
Applying a formula to all data points with different shapes.
Combining data from different sources with compatible sizes.
Syntax
Data Analysis Python
result = array1 + array2
Arrays must have compatible shapes for broadcasting.
Smaller arrays are 'stretched' to match bigger ones without copying data.
Examples
Adds 5 to each element of the array.
Data Analysis Python
import numpy as np
# Add a number to an array
arr = np.array([1, 2, 3])
result = arr + 5print(result)
Each row of arr2d is multiplied element-wise by arr1d.
Data Analysis Python
import numpy as np
# Multiply a 2D array by a 1D array
arr2d = np.array([[1, 2, 3], [4, 5, 6]])
arr1d = np.array([10, 20, 30])
result = arr2d * arr1d
print(result)
Subtracts each element of col_vec from corresponding row in arr2d.
Data Analysis Python
import numpy as np
# Subtract a column vector from a 2D array
arr2d = np.array([[5, 5, 5], [10, 10, 10]])
col_vec = np.array([[1], [2]])
result = arr2d - col_vec
print(result)
Sample Program
This program adds the 1D vector to each row of the 3x3 matrix. Broadcasting stretches the vector to match the matrix shape.
Data Analysis Python
import numpy as np
# Create a 3x3 array
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Create a 1D array with 3 elements
vector = np.array([10, 20, 30])
# Add vector to each row of matrix using broadcasting
result = matrix + vector
print(result)
OutputSuccess
Important Notes
Broadcasting works when trailing dimensions are equal or one of them is 1.
If shapes are not compatible, Python will raise an error.
Broadcasting does not copy data, so it is memory efficient.
Summary
Broadcasting lets you do math on arrays with different shapes easily.
Smaller arrays are stretched to match bigger ones without copying data.
It saves time and makes code simpler for array operations.
Practice
(1/5)
1. What does broadcasting do when working with arrays of different shapes in Python's NumPy?
easy
A. It stretches smaller arrays to match the shape of larger arrays without copying data.
B. It copies smaller arrays to make them the same size as larger arrays.
C. It only works if arrays have exactly the same shape.
D. It deletes extra elements from larger arrays to match smaller ones.
Solution
Step 1: Understand broadcasting concept
Broadcasting allows operations on arrays with different shapes by stretching the smaller array.
Step 2: Identify how broadcasting works
It does not copy data but virtually stretches the smaller array to match the larger one.
Final Answer:
It stretches smaller arrays to match the shape of larger arrays without copying data. -> Option A
Quick Check:
Broadcasting = stretching without copying [OK]
Hint: Broadcasting stretches smaller arrays without copying data [OK]
Common Mistakes:
Thinking broadcasting copies data
Believing arrays must have same shape
Assuming larger arrays get shrunk
2. Which of the following code snippets correctly applies broadcasting to add a 1D array to each row of a 2D array in NumPy?
easy
A. result = arr2d + arr1d[:, np.newaxis]
B. result = arr2d.flatten() + arr1d
C. result = arr2d + arr1d.reshape(-1, 1)
D. result = arr2d + arr1d
Solution
Step 1: Understand shapes of arrays
arr2d is 2D (m,n), arr1d is 1D (n,). Adding arr1d directly to arr2d adds arr1d to each row by broadcasting.
Step 2: Check reshaping effects
Reshaping arr1d to column vector (A,C) causes shape incompatibility (m,n) + (n,1); flattening arr2d (D) leads to incompatible 1D shapes.
Final Answer:
result = arr2d + arr1d -> Option D
Quick Check:
Direct addition broadcasts 1D to rows [OK]
Hint: Add 1D array directly to 2D to broadcast rows [OK]
Hint: Check trailing dimensions for broadcasting compatibility [OK]
Common Mistakes:
Assuming any shapes can broadcast
Thinking reshaping arr2 fixes without correct shape
Believing '+' can't add arrays
5. You have a 3D array arr3d with shape (2, 3, 4) and a 1D array arr1d with shape (4,). You want to add arr1d to each 2D slice of arr3d along the last axis. Which code correctly uses broadcasting to do this?
hard
A. result = arr3d + arr1d.reshape(2, 3, 4)
B. result = arr3d + arr1d.reshape(1, 1, 4)
C. result = arr3d + arr1d.reshape(4, 1, 1)
D. result = arr3d + arr1d.reshape(1, 4, 1)
Solution
Step 1: Understand shapes and broadcasting rules
arr3d shape is (2,3,4), arr1d shape is (4,). To add along last axis, arr1d must broadcast to (1,1,4).
Step 2: Reshape arr1d for correct broadcasting
Reshape arr1d to (1,1,4) so it broadcasts across first two dimensions and aligns with last dimension.
Step 3: Confirm addition
Addition with arr3d + arr1d.reshape(1,1,4) works correctly by broadcasting arr1d along first two axes.
Final Answer:
result = arr3d + arr1d.reshape(1, 1, 4) -> Option B
Quick Check:
Reshape 1D to (1,1,last_dim) for 3D broadcasting [OK]
Hint: Reshape 1D to (1,1,last_dim) to add along last axis [OK]
Common Mistakes:
Not reshaping arr1d or wrong reshape shape
Confusing axis order in reshape
Assuming direct addition always works
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.