Must Know in NumPy 1: Vectorization and Broadcasting | by João Loss | May, 2025


The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. — NumPy doc.

When performing arithmetic operations on arrays, it’s crucial to pay attention to their dimensions — for example, to add two arrays, they must have the same shape.

But imagine a simple scenario: V is a 1D array with 100 numbers, and we want to double each element. There are two ways to achieve this:
01. Use a for loop to iterate over V, multiplying each element by 2;
02. Create a second 1D array filled with twos and perform an element-wise multiplication.

If we choose the first option, we face the inefficiency of Python loops (as we’ve just seen). On the other hand, if we choose the second option, we end up creating new arrays just to match the shapes, which makes the code longer (with more variables) and can lead to unnecessary memory usage.

Now imagine we have the same problem, but this time for a 1,000,000×1,000,000 matrix . As you can see, this kind of issue can scale quickly and become a serious performance concern!

Thanks to NumPy’s broadcasting! With broadcasting, NumPy handles these situations by allowing operations between arrays of different shapes in an efficient and intuitive way.
Under the hood, NumPy automatically reshapes the smaller array so the operation can be performed as efficiently as possible, both in terms of memory and computation.

So, with broadcasting, we avoid both Python loops and the need to manually create new arrays. All the necessary reshaping and iteration are handled efficiently by NumPy.

If we want to double each element in any V array, we can simply write V * 2.

To reinforce the idea, consider the following operation.

Font

NumPy performs this addition by automatically reshaping the row vector and then carrying out the element-wise sum — just like shown in the image.

The code would be as straightforward and simple as the one shown below. No need for extra arrays or explicit Python loops.

A = np.array([[0, 0, 0],
[10, 10, 10],
[20, 20, 20],
[30, 30, 30]])

B = np.array([[1, 2, 3]])

C = A + B

"""
C:
[[ 1 2 3]
[11 12 13]
[21 22 23]
[31 32 33]]
"""

Note: of course there are some basic rules in order to make broadcasting possible. “ValueError: operands could not be broadcast together with shapes …” is what you get when you don’t obbey the rules.
You can check the rules in
NumPy Doc., but they are quite intuitive if you have some basic knowledge about matrix operations.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here