---
source: https://iinventedlight.com/library/notes/numpy-broadcasting.md
title: "NumPy broadcasting: arrays with different shapes"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (numpy/numpy LICENSE.txt; Copyright (c) 2005-2026, NumPy Developers; https://github.com/numpy/numpy/blob/main/LICENSE.txt); prose restates or closely paraphrases cited source lines"
license_family: numpy-bsd3
rule_check: "needs rule check (family not in auto-publish rule; check.mjs auto-approves only usgov and arxiv-ccby)"
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: NumPy"
tags: ['numpy', 'broadcasting', 'arrays']
sources:
  - id: S1
    title: "Broadcasting — NumPy v2.5 Manual"
    publisher: NumPy docs
    url: https://numpy.org/doc/stable/user/basics.broadcasting.html
    accessed: 2026-11-04
    license: "BSD-3-Clause (numpy/numpy LICENSE.txt; Copyright (c) 2005-2026, NumPy Developers; https://github.com/numpy/numpy/blob/main/LICENSE.txt)"
  - id: S2
    title: "NumPy quickstart — NumPy v2.5 Manual"
    publisher: NumPy docs
    url: https://numpy.org/doc/stable/user/quickstart.html
    accessed: 2026-11-04
    license: "BSD-3-Clause (numpy/numpy LICENSE.txt; Copyright (c) 2005-2026, NumPy Developers; https://github.com/numpy/numpy/blob/main/LICENSE.txt)"
---
# 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).
