Broadcasting Basics in NumPy
Broadcasting is one of the most powerful features of NumPy. It allows NumPy to perform operations on arrays of different shapes without explicitly copying data.
In this lesson, you will understand how broadcasting works, why it is useful, and how to apply it safely.
What Is Broadcasting?
Broadcasting is a mechanism that allows NumPy to:
- Perform arithmetic operations on arrays with different shapes
- Avoid unnecessary memory usage
- Write clean and efficient vectorized code
Instead of looping manually, NumPy automatically expands smaller arrays to match larger ones when possible.
Simple Example: Array and Scalar
Let’s start with the most common broadcasting case: an array and a single number.
import numpy as np
numbers = np.array([1, 2, 3, 4])
result = numbers + 10
print(result)
Output:
[11 12 13 14]
Here, NumPy treats the number 10 as if it were:
[10 10 10 10]
This expansion happens automatically through broadcasting.
Broadcasting with Two Arrays
Broadcasting also works between two arrays if their shapes are compatible.
a = np.array([1, 2, 3])
b = np.array([[10], [20], [30]])
result = a + b
print(result)
Output:
[[11 12 13]
[21 22 23]
[31 32 33]]
NumPy expanded both arrays to match a common shape without copying data.
Understanding the Shapes
Let’s look at the shapes of both arrays:
print(a.shape)
print(b.shape)
Output:
(3,)
(3, 1)
NumPy aligns dimensions from right to left and checks compatibility.
Broadcasting Rules
Two dimensions are compatible when:
- They are equal, or
- One of them is 1
If these rules are satisfied, broadcasting is possible. Otherwise, NumPy raises an error.
Example of Invalid Broadcasting
Here is an example where broadcasting fails:
x = np.array([1, 2, 3])
y = np.array([10, 20])
x + y
Error explanation:
- Shapes (3,) and (2,) are not compatible
- No dimension equals 1
Broadcasting in Mathematical Operations
Broadcasting is heavily used in:
- Normalization
- Scaling values
- Statistical computations
data = np.array([100, 200, 300])
mean = 200
normalized = data - mean
print(normalized)
Output:
[-100 0 100]
Performance Advantage
Broadcasting avoids explicit loops, making NumPy code:
- Faster
- Cleaner
- More readable
This is one of the reasons NumPy is widely used in data science and machine learning.
Practice Exercise
Exercise
Create an array of values and:
- Add a scalar using broadcasting
- Add a column vector to a row vector
- Observe the output shapes
Expected Outcome
You should clearly understand how NumPy expands arrays during operations.
What’s Next?
In the next lesson, you will learn about universal functions (ufuncs) and how NumPy performs fast element-wise operations.