Course Content
Ultimate NumPy
Ultimate NumPy
Broadcasting
Before diving into mathematical operations in NumPy, it's important to understand a key concept — broadcasting.
When NumPy works with two arrays, it checks their shapes for compatibility to determine whether they can be broadcasted together.
Note
If two arrays already have the same shape, broadcasting is not needed.
Same Number of Dimensions
Suppose we have two arrays for which we want to perform addition, with the following shapes: (2, 3)
and (1, 3)
. NumPy compares the shapes of the two arrays starting from the rightmost dimension and moving to the left. That is, it first compares 3 and 3, then 2 and 1.
Two dimensions are considered compatible if they are equal or if one of them is 1:
- For the dimensions 3 and 3, they are compatible because they are equal;
- For the dimensions 2 and 1, they are compatible because one of them is 1.
Since all dimensions are compatible, the shapes are considered compatible. Therefore, the arrays can be broadcasted, resulting in a standard addition operation between matrices of the same shape, which is performed element-wise.
import numpy as np array_1 = np.array([[1, 2, 3], [4, 5, 6]]) print(array_1.shape) # Creating a 2D array with 1 row array_2 = np.array([[11, 12, 13]]) print(array_2.shape) # Broadcasting and element-wise addition result = array_1 + array_2 print(result)
Note
array_1
is created as a 2D array containing only one row, which is why its shape is(1, 3)
.
But what would happen if we created it as a 1D array with a shape of (3,)
?
Different Number of Dimensions
When one array has fewer dimensions than the other, the missing dimensions are treated as having a size of 1. For example, consider two arrays with shapes (2, 3)
and (3,)
. Here, 3 = 3, and the missing left dimension is considered to be 1, so the shape (3,)
becomes (1, 3)
. Since the shapes (2, 3)
and (1, 3)
are compatible, these two arrays can be broadcasted.
import numpy as np array_1 = np.array([[1, 2, 3], [4, 5, 6]]) print(array_1.shape) # Creating a 1D array array_2 = np.array([11, 12, 13]) print(array_2.shape) # Broadcasting and element-wise addition result = array_1 + array_2 print(result)
Broadcasting Scalars
In addition to mathematical operations with arrays, we can also perform similar operations between an array and a scalar (number) thanks to broadcasting. In this case, the array can have any shape, as a scalar essentially has no shape, and all its dimensions are considered to be 1. Therefore, the shapes are always compatible.
import numpy as np array = np.array([[1, 2, 3], [4, 5, 6]]) print(array.shape) # Broadcasting and element-wise addition result = array + 10 print(result)
Incompatible Shapes
Let's also consider an example of incompatible shapes, where an arithmetic operation cannot be performed because broadcasting is not possible:
We have a 2x3
array and a 1D array of length 2
, i.e., a shape of (2,)
. The missing dimension is considered to be 1, so the shapes become (2, 3)
and (1, 2)
.
Moving from left to right: 3 != 2, so we immediately have incompatible dimensions, and therefore incompatible shapes. If we try to run the code, we will get an error:
import numpy as np array_1 = np.array([[1, 2, 3], [4, 5, 6]]) print(array_1.shape) array_2 = np.array([11, 12]) print(array_2.shape) # ValueError result = array_1 + array_2 print(result)
Thanks for your feedback!