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.

Level: intermediateTopic: NumPy pandas visualization

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 rates

Output

[[118.  262.5]
 [472.   84. ]]

Related terms

More NumPy pandas visualization terms

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.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.