It’s how you talk that shows who you are.

There is one term that is only known by advanced NumPy coders. If you know the term, consider yourself a NumPy pro. If you don’t, you still have to learn.

The term is “broadcasting”.

The documentation tells us that broadcasting “describes how NumPy treats arrays with different shapes during arithmetic operations.”

Now that’s a bit fuzzy, I’ll show you what it means:

Many NumPy operations such as multiplication are performed “element-wise”. For example: say you multiply two NumPy arrays A and B with the same shape: A * B.

In this case, NumPy performs element-wise matrix multiplication by multiplying cell A[i,j] with cell B[i,j] for every cell in the arrays.

As the arrays have the same shape, that’s not a problem.

import numpy as np salary = np.array([2000, 4000, 8000]) salary_bump = np.array([1.1, 1.1, 1.1]) print(salary * salary_bump)

**What’s the output of the puzzle?**

The puzzle shows a scenario where the company decides that all employees get a salary bump by 10% after a successful year.

Thus, the result of the puzzle is the NumPy array:

# [2200. 4400. 8800.]

So far so good. But what happens if you multiply two arrays with different shapes?

The answer is again: broadcasting.

Here is the same example but we are a bit lazy now and try to save a few bits.

salary = np.array([2000, 4000, 8000]) salary_bump = 1.1 print(salary * salary_bump)

**What’s the output of this code snippet?**

As all three values of the salary_bump are exactly the same numbers, you try to shorten it by multiplying a NumPy array with a number.

Thus, it produces exactly the same output.

Internally, NumPy creates a second implicit array that is filled with the salary bump value 1.1. This procedure is called “broadcasting”.

Not only is the code using broadcasting more concise and readable, but it is also more efficient! In the first example, the salary_bump array contains redundancies. But in the second example, NumPy gets rid of these redundancies — it does not really copy the data into a second NumPy array (this is only the conceptual idea but not the implementation).

## When can you apply broadcasting to two NumPy arrays?

Broadcasting is only possible if,

Missing dimensions are not a problem in this scenario. Here is a nice visualization from the documentation of how NumPy arrays will be broadcasted together:

A (2d array): 5 x 4

B (1d array): 1

Result (2d array): 5 x 4

A (2d array): 5 x 4

B (1d array): 4

Result (2d array): 5 x 4

A (3d array): 15 x 3 x 5

B (3d array): 15 x 1 x 5

Result (3d array): 15 x 3 x 5

A (3d array): 15 x 3 x 5

B (2d array): 3 x 5

Result (3d array): 15 x 3 x 5

A (3d array): 15 x 3 x 5

B (2d array): 3 x 1

Result (3d array): 15 x 3 x 5

An important observation is the following: **For any dimension where the first array has a size of one, NumPy conceptually copies its data until the size of the second array is reached.** Moreover, if the dimension is completely missing for array B, it is simply copied as well along the missing dimension.

Task: try to understand what is happening here in the first example of the given visualization.

In summary, broadcasting automatically matches two arrays with incompatible shape — a beautiful feature of the NumPy library!

## Where to go from here?

If you feel that your Python basics could need a quick refresher, download my various Python cheat sheets (high-resolution PDF) by subscribing to my “Coffee Break Python” email series!

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.