AI engineering glossary · NumPy pandas visualization
What is broadcasting?
Broadcasting: The rule NumPy uses to combine arrays of different shapes without copying data. Shapes are compared from the last dimension backwards; two dimensions are compatible if they are equal or one of them is 1, and the size-1 dimension is stretched to match. It removes explicit loops and saves memory.
Broadcasting: an example
Subtracting a column-means array of shape (3,) from a table of shape (100, 3) works because the means are applied to every one of the 100 rows.
Broadcasting in Python
A short example you can run (Python 3.11, NumPy). The output below is what it really prints.
import numpy as np
prices = np.array([[100, 250], [400, 80]]) # 2 shops x 2 items
gst = np.array([1.18, 1.05]) # one rate per item (column)
print(prices * gst) # (2,2) * (2,) -> each row times the ratesOutput
[[118. 262.5] [472. 84. ]]
Related terms
More NumPy pandas visualization terms
- C order
- Categorical dtype
- Chunked reading
- Colormap
- Colour-blind-safe palette
- Concatenate
- Copy-on-write
- crosstab
Where you learn it
Broadcasting is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.