Broadcasting
Broadcasting is a NumPy feature that allows you to perform operations between arrays with different shapes. NumPy automatically expands the smaller array when their shapes are compatible.
Broadcasting lets NumPy work with different-sized arrays.
Instead of manually copying values to make two arrays the same size, NumPy can automatically apply the smaller array across the larger array when their shapes are compatible.
What Is Broadcasting?
Start with a NumPy array:
import numpy as np numbers = np.array([10, 20, 30]) print(numbers + 5)
[15 25 35]
Here, numbers contains three values, but
5 is only one value.
NumPy automatically applies 5 to every
element:
10 + 5 = 15 20 + 5 = 25 30 + 5 = 35
This automatic behavior is called broadcasting.
Why Is Broadcasting Useful?
Without broadcasting, you might need to manually create another array:
numbers = np.array([10, 20, 30]) values = np.array([5, 5, 5]) print(numbers + values)
[15 25 35]
This works, but creating [5, 5, 5] manually
is unnecessary.
Broadcasting lets you simply write:
numbers + 5
NumPy handles the expansion automatically.
Think of Broadcasting Like This
Imagine these two values:
[10 20 30]
+
5
NumPy behaves as though the smaller value were applied to every position:
[10 20 30] [ 5 5 5] ------------ [15 25 35]
You don't actually have to create the second array. NumPy handles it internally.
Broadcasting With Multiplication
Broadcasting works with multiplication too.
numbers = np.array([10, 20, 30]) result = numbers * 2 print(result)
[20 40 60]
NumPy effectively applies 2 to every
element:
10 × 2 = 20 20 × 2 = 40 30 × 2 = 60
Broadcasting With Subtraction
prices = np.array([100, 200, 300]) discount = 20 result = prices - discount print(result)
[ 80 180 280]
The value 20 is automatically applied to
every element.
Broadcasting With Division
numbers = np.array([10, 20, 30]) result = numbers / 10 print(result)
[1. 2. 3.]
Again, NumPy broadcasts the single value across the entire array.
Broadcasting Between Arrays
Broadcasting is not only about arrays and numbers. NumPy can also broadcast smaller arrays against larger arrays when their shapes are compatible.
Example:
numbers = np.array([
[10, 20, 30],
[40, 50, 60]
])
values = np.array([1, 2, 3])
result = numbers + values
print(result)
[[11 22 33] [41 52 63]]
The shape of numbers is:
(2, 3)
The shape of values is:
(3,)
NumPy applies the three values to each row:
[10 20 30] [1 2 3] → [11 22 33] [40 50 60] [1 2 3] → [41 52 63]
Broadcasting in AI
Broadcasting is extremely useful when working with datasets.
Imagine a dataset containing temperatures:
temperatures = np.array([
[20, 25, 30],
[22, 27, 32],
[18, 24, 29]
])
Suppose we want to increase every temperature by 2:
adjusted = temperatures + 2 print(adjusted)
[[22 27 32] [24 29 34] [20 26 31]]
We didn't have to create another 3 × 3 array containing the number 2.
NumPy automatically broadcasts the value across the entire dataset.
Broadcasting and Feature Data
Imagine an AI dataset with three features:
data = np.array([
[10, 100, 1000],
[20, 200, 2000],
[30, 300, 3000]
])
Suppose we want to divide each feature by a different scaling value:
scale = np.array([10, 100, 1000]) result = data / scale print(result)
[[1. 1. 1.] [2. 2. 2.] [3. 3. 3.]]
The three scaling values are automatically applied to every row.
10 / 10 = 1 100 / 100 = 1 1000 / 1000 = 1
Then NumPy performs the same operation for every row.
This type of operation appears frequently when preparing features for Machine Learning models.
Broadcasting Requires Compatible Shapes
Broadcasting does not mean that NumPy can combine any two shapes.
The shapes must be compatible.
For example:
A = np.array([
[1, 2, 3],
[4, 5, 6]
])
B = np.array([10, 20, 30])
print(A + B)
This works because:
A shape = (2, 3) B shape = (3,)
The second dimension matches.
When Broadcasting Does Not Work
Consider:
A = np.array([
[1, 2, 3],
[4, 5, 6]
])
B = np.array([10, 20])
result = A + B
The shapes are:
A = (2, 3) B = (2,)
The dimensions do not match in a compatible way. NumPy cannot determine how to broadcast the two arrays.
This produces a broadcasting-related ValueError.
The Basic Broadcasting Rule
When comparing array shapes, NumPy works from the rightmost dimension.
Two dimensions are compatible when:
1. They are equal OR 2. One of them is 1
Example:
(2, 3) (3,)
Compare from the right:
3 = 3 Compatible
Therefore broadcasting works.
Broadcasting With a Dimension of 1
A dimension of 1 can also be broadcast.
A = np.array([
[1],
[2],
[3]
])
B = np.array([
[10, 20, 30]
])
result = A + B
print(result)
[[11 21 31] [12 22 32] [13 23 33]]
Here:
A shape = (3, 1) B shape = (1, 3)
NumPy can broadcast these into a compatible
(3, 3) operation.
Broadcasting vs Manually Copying Values
Without broadcasting, you might write:
numbers = np.array([10, 20, 30]) values = np.array([5, 5, 5]) result = numbers + values
With broadcasting:
numbers = np.array([10, 20, 30]) result = numbers + 5
Broadcasting makes array operations shorter, cleaner, and more efficient.
Broadcasting With Multiple Operations
You can combine broadcasting with several operations.
prices = np.array([100, 200, 300]) prices = prices * 0.9 prices = prices + 5 print(prices)
[ 95. 185. 275.]
First, every value is multiplied by
0.9.
Then 5 is added to every value.
Common Broadcasting Mistake
A common mistake is assuming that NumPy will always automatically make two arrays work together.
A = np.array([
[1, 2, 3],
[4, 5, 6]
])
B = np.array([
[10, 20],
[30, 40]
])
A + B
The shapes are:
A = (2, 3) B = (2, 2)
These shapes are not compatible, so NumPy raises a broadcasting error.
The important lesson is: always check the shapes when broadcasting behaves unexpectedly.
Check Shapes Before Broadcasting
The shape attribute is one of the most useful
debugging tools when working with NumPy.
A = np.array([
[1, 2, 3],
[4, 5, 6]
])
B = np.array([10, 20, 30])
print(A.shape)
print(B.shape)
(2, 3) (3,)
Once you understand these shapes, the broadcasting behavior becomes much easier to predict.
Complete AI-Style Example
Imagine three products with three features:
data = np.array([
[10, 100, 1000],
[20, 200, 2000],
[30, 300, 3000]
])
Suppose each feature needs a different scaling factor:
scale = np.array([10, 100, 1000])
We can divide the entire dataset by those values:
normalized = data / scale print(normalized)
[[1. 1. 1.] [2. 2. 2.] [3. 3. 3.]]
Broadcasting automatically applies the three scaling values to every row.
This is much cleaner than manually creating a separate scaling array for every row.
Broadcasting Quick Reference
| Example | What Happens |
|---|---|
array + 5 |
5 is applied to every element |
array * 2 |
Every element is multiplied by 2 |
array - 10 |
10 is subtracted from every element |
array / 10 |
Every element is divided by 10 |
(2, 3) + (3,) |
Compatible broadcasting |
(2, 3) + (2,) |
Not compatible in this arrangement |
Complete Example
import numpy as np
# Dataset
data = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
# Values applied to every row
adjustment = np.array([1, 2, 3])
# Broadcasting
result = data + adjustment
print(result)
[[11 22 33] [41 52 63] [71 82 93]]
The array has shape (3, 3), while the
adjustment has shape (3,).
NumPy automatically applies
[1, 2, 3] to every row.
Broadcasting lets NumPy perform operations on different-sized arrays.
NumPy automatically expands a smaller value or array
across a larger array when their shapes are compatible.
This means you can write simple operations such as
data + 5 or
data / scale without manually copying
values. The most important thing to remember is that
broadcasting depends on compatible shapes. When an
operation gives an unexpected error, check
array.shape first.