‫Now that we've covered reshaping and transposing, you might be thinking when would we ever want to
‫use a transpose?
‫It just flips the axes around.
‫When is that useful?
‫Well, that's what we're going to cover in this video.
‫And one of the main use cases for transposing a matrix is the DOT product.
‫It might be thinking, now, dot product.
‫What the hell is the DOT product?
‫Well, let's do what we do.
‫Dot product, num py let's have a research because it's what we're practicing.
‫Here we go.
‫Num py dot.
‫Dot product of two arrays.
‫Oh, that's not very helpful.
‫Sometimes you'll get that in the docs.
‫It'll give you a very brief explanation of what happens.
‫Specifically if both A and B are wondrous, it is the inner product of vectors.
‫Okay, there's a few text instructions here of what a dot product actually is, but let's focus on the
‫code.
‫We'll go back to our notebook.
‫We'll write some code first and then we'll have a little look into what the product actually does.
‫The thing you need to remember with the doc product, it's just another way of finding patterns between
‫two different sets of numbers.
‫So let's start off with a random seed.
‫We'll create two more matrices that we can use for an example.
‫We want this one to be rand int.
‫Let's make it of size five and three and we want Matt to beautiful.
‫We'll just make it the same thing.
‫Random rand int ten size equals five three.
‫And now we've seen rand in before.
‫Do you know what these two lines of code will do?
‫So let's have a look.
‫As a reminder, we can do shift tab.
‫Return random integers from low to high.
‫Beautiful.
‫Let's see these in action.
‫Matt.
‫One, wonderful.
‫Matt.
‫Two.
‫Even more.
‫Wonderful.
‫And then we also want to see.
‫We all already know the shape, but let's just do it anyway.
‫So we've got the shapes here that to shape.
‫Wonderful.
‫So in a previous video, we've seen a few different ways of performing arithmetic on matrices like this.
‫One of the ways that we did was multiplication.
‫So we go mat one times, mat to.
‫Let's bring these down so we can see them because I want to stay nice and zoomed in to keep the code
‫big.
‫There we go.
‫Map one.
‫Map two.
‫Wonderful.
‫This area down here is just going to be the combination of these two multiplied element wise.
‫Five times six is 30 zero.
‫Times seven is zero.
‫But this is, I said a little tricky word before element wise multiplication.
‫There's a few different names for this, but it's mostly called element wise hard IMADE product.
‫That's that's how you spell it I believe.
‫Let's have a look hard mod product name pie.
‫That's what we're after.
‫How to get element wise.
‫Yeah.
‫MP multiply, which is the same as using this little star symbol.
‫So that's element y's multiplication.
‫Another form of multiplication or product between two matrices is the DOT product.
‫So let's have a look at that DOT product.
‫If we go back actually to our documentation, the formula for doing a dot product is NP dot.
‫You can use an umpire, we've imported an umpire's NP, so we're going to do NP dot, we want to perform
‫mat one, mat two, which is just our two matrices that we made.
‫So let's run this.
‫Oh, that didn't work.
‫-- it.
‫Our value error shapes five, three, five, three.
‫Not aligned.
‫Three, Tim one does not equal five.
‫Okay.
‫We go back up to see the shapes of our matrices, their both of shape.
‫Five, three, five, three.
‫And this is saying shapes five, three and five, three not aligned.
‫Well, the difference between dot product and element wise.
‫So we've seen element wise before.
‫It's relatively simple.
‫We've got a times E goes into this top left square.
‫A Times F Oh no, sorry.
‫B times f I just set it simple and here I am messing it up.
‫B Times F But if we kept going through that, we'd eventually end up with this matrix.
‫But the DOT product is a little bit different.
‫Now we've got, say, a three by three matrix here, a three by two matrix here, and we're calling
‫dot on this one with this one.
‫And now what is going on here?
‫The resulting matrix is going to be three by two and we have a times J Okay, following along with that,
‫but then we have B times L plus OC, then we have C times N, All right.
‫So it's got the top row here multiplied by the first column and adding up the total.
‫And now let's look at a little bit more of a colourful demonstration of the DOT product.
‫So here we've got the letters matrix.
‫If we go back here.
‫Got the same one as here.
‫Now we've got some colour added.
‫This is a the top row of the matrix and the left multiplied by the column of the matrix on the right
‫equals this top left square.
‫And then if we kept going through, we would eventually get this resulting matrix here.
‫But there's a couple of rules with a DOT product.
‫But numbers on the inside must match.
‫So for a dot product to happen between two matrices, their dimensions, if you put them side by side.
‫So this three matches this three.
‫So this matrix here has three rows and so does this one.
‫And now the resulting matrix is the size of the outside numbers.
‫So the dimensions here are three by two of this resulting matrix, because the outside numbers of these
‫two matrices are three and two.
‫Okay, so we've got that.
‫For a dot product to happen, the inside numbers must match.
‫Now let's have a look with some numbers that have actually been filled in.
‫We've got 503468 and we're calling DOT between this matrix and this one.
‫So we've got five.
‫It's going to come over here.
‫B multiplied by four.
‫That equals 20.
‫Yeah, we've got that five four.
‫This is eight times J now we've got zero times six.
‫Zero times six.
‫This is B times L Yeah, we've got that.
‫We're adding that up now.
‫We've got three times eight.
‫This is C times n, all right.
‫And that's 24.
‫And if we added these three numbers together.
‫So we're going to go eight times J plus B, times L plus C, times N Yeah, beautiful.
‫We get 44 and that's going to fill the top left square of our resulting array.
‫Now, if we went through these two matrices with numbers full and follow the same steps that we've got
‫in this letters matrix, we would end up with something like this.
‫But this is a static demonstration.
‫So let's check out this little beautiful resource here, which is an interactive demo.
‫So this is matrix multiplication, z, z.
‫This is called the waterfall technique.
‫What this demonstration is going to do, and you can change these numbers here to be something similar
‫to maybe what we've got here or just to view the process of what's happening.
‫So if we click multiply, it's going to take the matrix that was on the right and put it on top.
‫This is where the waterfall process comes in.
‫Now it's going to flow down.
‫So this row to six one used to be here as a column.
‫And if we add these together, multiply them.
‫We get 15 in the top left.
‫What it's done is it's just replicated this step here.
‫We've got the top left.
‫Now if we follow it through.
‫Okay.
‫We get the top right and we get the middle left.
‫Then we get the middle righ and the bottom left.
‫And then we finally finish up with the bottom right number.
‫So have a play around with this.
‫If you check it out, you can keep going through.
‫It takes a little bit of practice to understand the DOT product.
‫Let's go back to our notebook.
‫And now that we know a little bit more about the DOT product, we know the rules.
‫The numbers on the inside must match, and the new size of the resulting matrix is three by two because
‫it takes the outside numbers.
‫How would we make this work?
‫With our Matrix one and Matrix two.
‫If their internal shapes are five and three and five and three.
‫Hmm.
‫Well maybe we could use a transpose.
‫Let's try that.
‫Our transpose map one.
‫Because what does it transpose do.
‫It flips the axes around.
‫So let's go map one dot t.
‫What does that look like?
‫Hmm.
‫Okay.
‫Might look at that and see.
‫Maybe it'll work, but let's really confirm it, Matt.
‫One t shape.
‫Three.
‫Five.
‫Is that what we need?
‫Yeah, we need to flip it around.
‫So if we have that two dot shape.
‫Beautiful.
‫So now the inside shapes.
‫If we were to transpose mat one a matching so let's do this.
‫We're actually before we did import Matt one.
‫Matt two.
‫Okay, maybe we transpose Matt to Matt one shape Matt to t to stay consistent with what we got above.
‫Let's make Matt three equals npe dot matt one and then we want Matt to t so this should work if our
‫shapes are aligned.
‫Matt three.
‫Beautiful.
‫And now if we have map three, what is the shape of Matt three?
‫Yes.
‫Excellent.
‫So now we can see where the transpose might come into play.
‫If we wanted to do a dot product on two matrices where in our first try the shapes were incorrect,
‫we might want to flip the axes.
‫Of course, you could do this with the reshape as well.
‫But this is just a handy example where transpose comes into play for performing something like a DOT
‫product.
‫Now we can see the resulting shape of Matrix three is the same as the outside numbers.
‫Wonderful.
‫Now, when I first came across product, it took me a little while to understand how to actually go
‫through it.
‫A great exercise was to write out a matrix by hand and multiply it and figure something out like this,
‫or use the demo like matrix multiplication, x, y, z.
‫The main takeaway from the DOT product is that it's just another tool in our arsenal to find patterns
‫between two different arrays of numbers, as we've seen before.
‫Now, in the next section, we'll have a look at where the DOT product might be used in practice.
‫So have a practice around, create some matrices, transpose them, see if you can do a DOT product
‫and I'll see you in the next video.