How to Work with Broadcast and Transpose
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

This guide brings us to the end of preprocessing. And the last two topics that we'll be covering are broadcasting and transposing. Neither of these topics are overly difficult to understand, but it's way easier to explain both of these concepts by using a picture and examples.

medium

medium

Take for instance two arrays, both array A and array B are two dimensional, each having three rows and three columns. When we want to perform a mathematical operation, it's really easy to visualize how each element can be mapped to a corresponding element. But when the arrays aren't the same size, it doesn't seem to be as straightforward.

I'm going to summarize a little bit here, but NumPy's documentation says, "The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations." Further into their documentation, it also says that there's two general rules for broadcasting. And those are: two dimensions are compatible when, one, they're equal, or two, one of them is one.

medium

medium

It's really easy to see in our first example that they're both equal. And in the second example, one of the arrays is one dimensional. And in the one dimensional case, what NumPy is doing is that it's stretching the smaller array to fit the size of the larger array. And it's able to stretch an array in either direction.

medium

Let's see how this looks using an actual matrix. Array A will range from one to nine, and then we'll transform that into a two-dimensional three by three matrix, and array B will just include one, two, and three. Let's go ahead and run this and check out the result. If you run through it, you can see that all the elements in the first column were multiplied by one, the second column by two, and the third by three. So broadcasting worked exactly how we expected it to.

medium

By now I hope that you trust me, but in case you don't, let's go ahead and add the new access function to array B and then run it once again. And this time you'll see that all the elements in the first row were multiplied by one, the elements in the second row were multiplied by two, and the third row by three. So we can see how stretching has been done in both of the directions.

medium

medium

Broadcasting isn't limited to one and two dimensions either. We can broadcast a smaller two-dimensional matrix across the larger three-dimensional matrix. In this diagram, you can see each element is multiplied by the corresponding element across all three dimensions. Now, applying this to our actual code, let's just make a array B the larger array. Now when we run it, you're able to see how array A was actually stretched across all three dimensions. So in all three dimensions, each element in row one column three were multiplied by three.

medium

To finish up broadcasting, let's go over a couple examples of when it won't work. The first is when the arrays are different sizes. Let's just get rid of both of these reshapes and then just run the code. And you should be seeing an error message, broadcasting can't be done. The other instance that it shouldn't work is if they're the same size but the dimensions don't line up. So take this one for example. We can't broadcast a smaller two-dimensional array across another two-dimensional array.

medium

The last topic that we're going to be going over is transposing. The actual definition of transposing is to cause two or more things to change place with one another. But in NumPy, the concept is probably a little bit more relatable to the linear algebra description. And that says that transposing switches the row and column indices of the matrix.

medium

So if we start with an array that has two rows and 10 columns, the transposed matrix will have 10 rows and two columns. You'll also notice that all of the elements that made up the first row in the original array now make up all the elements in the first column of the transposed array. And the elements of the second row are now the elements of the second column.

And this is something that can be done really easily with NumPy. So, I'll create the same array that we used in our example, then all we need to do is call the transpose function and pass in the name of the array. You can see how all of the first row of the original array is now the first column and the second row became the second column of the transposed array.

medium

By now, some of you might be wondering why either of these concepts are important. And to be completely honest, neither of these will serve you much of a purpose for the vast majority of this course. But when we eventually get to neural networks, it's going to start to make a lot more sense why we need these. I know we haven't covered these terms yet, but both of these concepts are going to be used when we're weighting an input by applying a scalar value, or when it comes to optimizing an algorithm by forward or backwards propagation.

And that brings us to the end of this section which means we finally get to start working with actual machine learning algorithms. So for now, I'll wrap it up, and I will see you in the next guide.