1. Broadcasting
Broadcasting takes vectorized operations to the next level!
2. Broadcasting introduced
Previously, we covered vectorized operations between arrays of the same size or between an array and a scalar. It's also possible to perform mathematical operations between arrays of different shapes. Since this involves "broadcasting", or stretching the smaller array across the larger one, the conventions that govern this type of array math are called "broadcasting."
3. Broadcasting a scalar
In fact, adding a scalar to an array uses broadcasting. Here, the scalar two that we add to each element in the array is broadcast across the array so that the result is the same as though the array had been added to an array full of twos. The difference is that broadcasting is more efficient in terms of memory and computing power.
4. Compatibility rules
Broadcasting only works with compatible arrays. To determine whether arrays are broadcastable, compare array dimensions from right to left.
5. Compatibility rules
A set of dimension lengths is compatible when one of them is one or they are equal. Every set of dimensions must be compatible for the arrays to be broadcastable. For example, dimension lengths one and five are compatible since one is one. Ten and ten are compatible since they are the same. Because both sets of dimensions are compatible, the arrays are broadcastable.
6. Broadcastable or not?
An array with ten rows and five columns is broadcastable with an array with ten rows and one column since one of the trailing dimension lengths is one, and the lengths of the first dimension are equal.
A ten by five array is also broadcastable with a five-element 1D array since the right-most dimensions are both five. Two arrays need not have the same number of dimensions to be broadcastable.
A ten by five array is not broadcastable with an array with five rows and ten columns since neither set of dimensions is compatible.
Finally, a ten by five array is not compatible with a ten-element 1D array since the right-most set of dimensions is not compatible.
7. Broadcasting rows
Let's add an array holding the numbers zero through four to a 2D array holding the numbers zero through nine with two rows and five columns. These arrays are broadcastable because both have a trailing dimension of five.
8. Broadcasting rows
NumPy broadcasts the 1D array by operating as though there is a copy of the 1D array for each row in the 2D array, then adding the two together.
9. Incompatible broadcasting
NumPy's default assumption is that the user is attempting to broadcast row-wise, as we saw in the previous example. Thus, a 1D array of two elements is not broadcastable across an array with two rows and five columns, because the right-most dimension of each array is not the same.
10. Broadcasting columns
We can broadcast an array of two elements across an array with two rows by changing its shape using dot-reshape so that the trailing dimensions are compatible.
Now, the array to be stretched has two rows and one column. Since a trailing dimension of one is compatible with any other trailing dimension, the arrays are broadcastable together.
11. Broadcasting columns
Again, the math is performed as though the single-column array has been stretched across an array of the same shape as the larger array. NumPy has done this without actually creating the stretched array; the benefits of broadcasting grow with the size of the data we work with!
12. Other operators
The same logic applies to multiplying and subtracting! Multiplying by a column multiplies the column by both columns in the larger array. Subtracting a row subtracts the row from both rows in the larger array.
13. Let's practice!
Let's get broadcasting!