Welcome to Engineering Python.
This is a Python programming course for engineers.
In this video, I'll talk about NumPy array broadcasting.
Broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations.
The smaller array is broadcast across the larger array so the shapes match.
I'll demonstrate this using examples.
First, we need to import numpy as np.
Let's create a 1D array with three elements.
b is an integer number, 1.
When we calculate a plus b, because their shapes don't match, we need to broadcast or
stretch b horizontally so it has three elements as well.
That means, this number, 1, will be repeated two more times.
Now the shapes match, we will do the element-wise addition.
Another example, a is a 2 by 3 array.
b is a 1 by 3 array.
When we calculate a plus b, array b will be stretched vertically to match the shape of
a.
Then we will do the element-wise addition.
This is the result.
The third example.
a is still the 2 by 3 array.
b is now a 2 by 1 array.
When we calculate a plus b, array b will be stretched horizontally to match the shape
of a.
Then we will do the element-wise addition.
This is the result.
If b is changed to just one integer number like this, when we calculate a plus b, array
b will be stretched both horizontally and vertically to match the shape of a.
Then we will do the element-wise addition.
This is the result.
If a is changed to a 2 by 1 array, and b is a 1 by 3 array, when we calculate a plus b,
array a will be stretched horizontally to match the shape of b, and b will be stretched
vertically to match the shape of a.
Then we will do the element-wise addition.
This is the result.
If the dimensions mismatch, there will be an error.
For example, a is a 2 by 3 array, b is a 1 by 4 array.
NumPy will not be able to broadcast b in any direction to match the shape of a.
So, this operation is not allowed in NumPy.
Here is a more realistic application of array broadcasting.
We want to travel from Binghamton to New York City.
There are several cities on the route.
We know that it's 0 miles from Binghamton to Binghamton, 43 miles from Binghamton to
Hancock, 120 miles from Binghamton to Middletown, 153 miles from Binghamton to Sloatsburg, and
190 miles from Binghamton to New York City.
Such information is stored in a NumPy array named miles.
We want to calculate the distances between any two cities on the route.
For example, what is the distance from Hancock to New York City?
Or from Hancock to Sloatsburg?
To solve this problem, we only need to reshape miles as a 5 by 1 array, and then subtract
it from the original miles.
The result is an array of the distances between any two cities.
For example, this 110 is the distance from Hancock to Sloatsburg.
This -147 is the distance from New York City back to Hancock.
Okay, that was about NumPy array broadcasting.
The course materials are available on YouTube and GitHub.
You can watch the course videos in sequence.
If you like this video, please subscribe and share.
I'm Yong Wang.
Thanks for watching.