NumPy broadcasting: arrays with different shapes
Answer: The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is broadcast across the larger array so that they have compatible shapes. (S1)
Why it matters
- Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. (S1)
- It does this without making needless copies of data and usually leads to efficient algorithm implementations. (S1)
- There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation. (S1)
The simplest case
- NumPy operations are usually done on pairs of arrays on an element-by-element basis. (S1)
- In the simplest example of broadcasting, the scalar b is stretched to become an array of same shape as a so the shapes are compatible for element-by-element multiplication. (S1)
- NumPy is smart enough to use the original scalar value without actually making copies. (S1)
Shape vocabulary
- ndarray.ndim is the number of axes (dimensions) of the array. (S2)
- The shape is a tuple of integers indicating the size of the array in each dimension. (S2)
- The length of the shape tuple is therefore the number of axes, ndim. (S2)
Claims
- id: C1 claim: Broadcasting covers arithmetic on arrays with different shapes. source: S1 evidence: "The term broadcasting describes how NumPy treats arrays with different"
- id: C2 claim: Broadcasting moves looping into C. source: S1 evidence: "array operations so that looping occurs in C instead of Python."
- id: C3 claim: Broadcasting can be a bad idea for memory use. source: S1 evidence: "There are, however, cases where"
- id: C4 claim: ndim is the number of axes. source: S2 evidence: "the number of axes (dimensions) of the array."
Sources
- [S1] Broadcasting — NumPy v2.5 Manual. NumPy docs. https://numpy.org/doc/stable/user/basics.broadcasting.html . License: BSD-3-Clause (numpy/numpy LICENSE.txt; Copyright (c) 2005-2026, NumPy Developers; https://github.com/numpy/numpy/blob/main/LICENSE.txt). Accessed 2026-11-04.
- [S2] NumPy quickstart — NumPy v2.5 Manual. NumPy docs. https://numpy.org/doc/stable/user/quickstart.html . License: BSD-3-Clause (numpy/numpy LICENSE.txt; Copyright (c) 2005-2026, NumPy Developers; https://github.com/numpy/numpy/blob/main/LICENSE.txt). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: NumPy docs, Broadcasting — NumPy v2.5 Manual, BSD-3-Clause (numpy/numpy LICENSE.txt; Copyright (c) 2005-2026, NumPy Developers; https://github.com/numpy/numpy/blob/main/LICENSE.txt); NumPy docs, NumPy quickstart — NumPy v2.5 Manual, BSD-3-Clause (numpy/numpy LICENSE.txt; Copyright (c) 2005-2026, NumPy Developers; https://github.com/numpy/numpy/blob/main/LICENSE.txt).
Cite this
iinventedlight. 2026. "NumPy broadcasting: arrays with different shapes." iinventedlight Library, iil:note:txf6kncx, version of 2026-11-04. https://iinventedlight.com/library/notes/numpy-broadcasting/. License: CC-BY-4.0.
@misc{iil_note_txf6kncx,
author = {{iinventedlight}},
title = {{NumPy broadcasting: arrays with different shapes}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/numpy-broadcasting/},
note = {Permanent ID iil:note:txf6kncx. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/numpy-broadcasting.md.