Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Broadcasting | Math with NumPy
Ultimate NumPy
course content

Course Content

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
Broadcasting

Before diving into mathematical operations in NumPy, it's important to understand a key concept — broadcasting.

When NumPy works with two arrays, it checks their shapes for compatibility to determine whether they can be broadcasted together.

Note

If two arrays already have the same shape, broadcasting is not needed.

Same Number of Dimensions

Suppose we have two arrays for which we want to perform addition, with the following shapes: (2, 3) and (1, 3). NumPy compares the shapes of the two arrays starting from the rightmost dimension and moving to the left. That is, it first compares 3 and 3, then 2 and 1.

Two dimensions are considered compatible if they are equal or if one of them is 1:

  • For the dimensions 3 and 3, they are compatible because they are equal;
  • For the dimensions 2 and 1, they are compatible because one of them is 1.

Since all dimensions are compatible, the shapes are considered compatible. Therefore, the arrays can be broadcasted, resulting in a standard addition operation between matrices of the same shape, which is performed element-wise.

123456789
import numpy as np array_1 = np.array([[1, 2, 3], [4, 5, 6]]) print(array_1.shape) # Creating a 2D array with 1 row array_2 = np.array([[11, 12, 13]]) print(array_2.shape) # Broadcasting and element-wise addition result = array_1 + array_2 print(result)
copy

Note

array_1 is created as a 2D array containing only one row, which is why its shape is (1, 3).

But what would happen if we created it as a 1D array with a shape of (3,)?

Different Number of Dimensions

When one array has fewer dimensions than the other, the missing dimensions are treated as having a size of 1. For example, consider two arrays with shapes (2, 3) and (3,). Here, 3 = 3, and the missing left dimension is considered to be 1, so the shape (3,) becomes (1, 3). Since the shapes (2, 3) and (1, 3) are compatible, these two arrays can be broadcasted.

123456789
import numpy as np array_1 = np.array([[1, 2, 3], [4, 5, 6]]) print(array_1.shape) # Creating a 1D array array_2 = np.array([11, 12, 13]) print(array_2.shape) # Broadcasting and element-wise addition result = array_1 + array_2 print(result)
copy

Broadcasting Scalars

In addition to mathematical operations with arrays, we can also perform similar operations between an array and a scalar (number) thanks to broadcasting. In this case, the array can have any shape, as a scalar essentially has no shape, and all its dimensions are considered to be 1. Therefore, the shapes are always compatible.

123456
import numpy as np array = np.array([[1, 2, 3], [4, 5, 6]]) print(array.shape) # Broadcasting and element-wise addition result = array + 10 print(result)
copy

Incompatible Shapes

Let's also consider an example of incompatible shapes, where an arithmetic operation cannot be performed because broadcasting is not possible:

We have a 2x3 array and a 1D array of length 2, i.e., a shape of (2,). The missing dimension is considered to be 1, so the shapes become (2, 3) and (1, 2).

Moving from left to right: 3 != 2, so we immediately have incompatible dimensions, and therefore incompatible shapes. If we try to run the code, we will get an error:

12345678
import numpy as np array_1 = np.array([[1, 2, 3], [4, 5, 6]]) print(array_1.shape) array_2 = np.array([11, 12]) print(array_2.shape) # ValueError result = array_1 + array_2 print(result)
copy
Select arrays with compatible shapes:

Select arrays with compatible shapes:

Select a few correct answers

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt