Have you ever wondered how NumPy can perform operations on arrays of different shapes and sizes without looping or padding? The answer is broadcasting, a feature that allows NumPy to automatically expand the dimensions of arrays to make them compatible for element-wise operations.
Broadcasting is useful because it saves memory and computation time by avoiding the creation of intermediate arrays. It also makes the code more concise and readable by eliminating the need for explicit loops or reshaping.
In this blog post, I will explain what broadcasting is, how it works, and what are some common operations that use broadcasting. Let’s get started!
What is broadcasting?
Broadcasting is the process of expanding the shape of an array to match another array along one or more axes. For example, suppose we have two arrays A and B with shapes (3, 4) and (4,) respectively. We can add them together using broadcasting as follows:
A = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
B = np.array([10, 20, 30, 40])
C = A + B
print(C)
The output is:
[[11 22 33 44]
[15 26 37 48]
[19 30 41 52]]
How did NumPy do that? It expanded the shape of B from (4,) to (3, 4) by repeating it along the first axis. Then it added the corresponding elements of A and B to produce C. This is illustrated in the figure below:
How does broadcasting work?
Broadcasting works by following two rules:
– Rule 1: If the arrays have different numbers of dimensions, prepend ones to the shape of the smaller array until they have the same number of dimensions.
– Rule 2: If the arrays have the same number of dimensions but different sizes along some axes, expand the smaller array along those axes by repeating its elements until they have the same size.
For example, suppose we have two arrays A and B with shapes (2, 3) and (3,) respectively. To broadcast them together, we first apply rule 1 and prepend a one to the shape of B to make it (1, 3). Then we apply rule 2 and expand B along the first axis to make it (2, 3). Now we can add them together element-wise.
A = np.array([[1, 2, 3],
[4, 5, 6]])
B = np.array([10, 20, 30])
C = A + B
print(C)
The output is:
[[11 22 33]
[14 25 36]]
This is illustrated in the figure below:
![Broadcasting example](https://numpy.org/doc/stable/_images/theory.broadcast_2.gif)
What are some common operations that use broadcasting?
Broadcasting can be used for many operations that involve arrays of different shapes and sizes. Here are some examples:
– Arithmetic: You can perform element-wise addition, subtraction, multiplication, division, exponentiation, etc. on arrays of different shapes and sizes using broadcasting. For example,
A = np.array([[1, 2],
[3, 4]])
B = np.array([10])
C = A * B
print(C)
The output is:
[[10 20]
[30 40]]
– Comparison: You can compare arrays of different shapes and sizes using broadcasting. For example,
A = np.array([[1, 2],
[3, 4]])
B = np.array([2])
C = A > B
print(C)
The output is:
[[False False]
[True True]]
– Aggregation: You can perform aggregation functions such as sum, mean, max, min, etc. on arrays of different shapes and sizes using broadcasting. For example,
A = np.array([[1, 2],
[3, 4]])
B = np.array([10])
C = np.sum(A + B)
print(C)
The output is:
60
Conclusion
Broadcasting is a powerful feature that allows NumPy to perform operations on arrays of different shapes and sizes without looping or padding. It saves memory and computation time by avoiding the creation of intermediate arrays. It also makes the code more concise and readable by eliminating the need for explicit loops or reshaping.
I hope this blog post has helped you understand what broadcasting is, how it works, and what are some common operations that use broadcasting.