Broadcasting in NumPy denotes the ability to guess a common, compatible shape between two arrays. For instance, when adding a vector (one-dimensional array) and a scalar (zero-dimensional array), the scalar is extended to a vector, in order to allow for the addition. The general mechanism is called broadcasting. We will first review that mechanism from a mathematical point of view, and then proceed to give the precise rules for broadcasting in NumPy.

Mathematical view

Broadcasting is often performed in mathematics, mainly implicitly. Examples are expressions such as f(x) + C or f(x) + g(y). We'll give an explicit description of that technique in this section.

We have in mind the very close relationship between functions and NumPy arrays, ...

Get Scientific Computing with Python 3 now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.