When NumPy is given two arrays with different shapes and is asked to perform an operation that would require the two shapes to be the same, both arrays are broadcast to a common shape.
Suppose the two arrays have shapes and . Broadcasting consists of the two steps:
- If the shape is shorter than the shape , that is, len(s1) < len(s2), then ones are added on the left of the shape . This is reshaping.
- When the shapes have the same length, the first array is extended to match the shape s2 (if possible).
Suppose we want to add a vector of shape to a matrix of shape . The vector needs to be broadcast. The first operation is reshaping; the shape of the vector is converted from (3, ) to (1, 3). The second operation is an extension; the shape is converted from (1, 3) to (4, 3).
For instance, suppose a vector of size n is to be broadcast to the shape (m, n):
- is automatically reshaped to (1,...