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 axis is 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 a dot product.
Well let's do what we do.
Dot product name pi.
Let's have a research because what we're practicing.
Here we go.
Name pi dot dot product of two arrays.
Oh that's not very helpful.
Sometimes you get that then docs it'll give you a very brief explanation of what happens specifically
if both A and B a one day arise.
It is the inner product of vectors.
Okay.
There's a few text instructions here of what dot product actually is but let's focus on the code.
I'll go back to our notebook we'll write some code first and then we'll have a little look into what
the DOT PRODUCT actually does.
The thing you need to remember the dot product it's just another way of finding patterns between two
different sets of numbers.
So let's start off with the random seed will create two more matrices that we can use for an example.
I want this one to be Rand and let's make it of size 5 and 3 and we want map to beautiful.
We'll just make it the same thing.
Random dot Rand int 10.
Size equals 5 3.
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 a shift tab return random in is from low to high.
Beautiful.
Let's see these in action.
MATT one wonderful Matt to even more wonderful.
And then we also want to say we already know the shape but let's just do it anyway.
So we've got the shapes here.
Matt 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.
Matt one Times Matt.
Two.
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.
Matt one Matt two wonderful this array down here is just going to be the combination of these two multiplied
element wise five times six is 30 0 times 7 0 but this is I said a little tricky word before element
y's multiplication.
There's a few different names to this but it's mostly called element y's hard Ahmad product.
That's that's how you spell out I believe.
Let's have a look.
Hard hard.
Product Name pi.
That's what we're after How to Get element wise.
Yeah.
MP dot 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.
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 MP dot.
You can use num piled up we've imported num pies and P so we're gonna do MP dot form mat 1.
Map 2 which is just our two matrices that we made.
So let's run this oh that didn't work.
Damn it.
Our value era shapes 5 3 5 3.
Not aligned.
3 dim 1 does not equal 5.
Okay we go back up to see the shapes of our matrices.
They're both of shape.
5 3 5 3.
This is saying shapes 5 3 and 5 3 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 a goes into this top left
square a Times F no sorry b times.
I just said simple and here I am messing it up the 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.
Now what is going on here the resulting matrix is gonna be three by two and we have a times J.
Okay.
Following along with that then we have b times L plus.
Okay 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.
Now let's look at a little bit more of a colorful demonstration of the dot product so here we've got
the letters matrix if we go back here that the same one is here now we've got some color added.
This is a 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 that numbers on the inside must match.
So for a dot product to happen between two matrices they're 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 3 and 2.
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 5 0 3 4 6 8 and we're calling dot between this matrix and this one so we've got 5 it's going
to come over here be multiplied by 4 that equals 20.
Yep we've got that 5 4.
This is a Times J.
Now we've got 0 times 6 0 times 6.
This is B times l yep we've got that.
We're adding that up now we've got three times eight.
This is C times n Orion and that's 24.
And if we add it these three numbers together.
So we're going to go eight times J plus B times l policy 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 followed 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 dot x y 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 where the waterfall process comes in.
Now it's going to flow down so this row two 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 and we get the middle
right 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 much matched 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 1 and Matrix 2 if their internal shapes are 5 and 3 and
5 and 3
or maybe we could use a transpose let's try that out transpose map 1 because what does it transpose
do.
It flips the accesses around so let's go map one dot t.
What does that look like.
Mm hmm.
Okay.
Might look at that and see maybe it'll work but let's really confirm it map one dot t dot shape.
3 5.
Is that what we need.
Yeah.
We need to flip it around.
So if we have met two dot shape
beautiful so now the inside shapes.
If we were to transpose mat 1 matching so let's do this actually before we did MP dot map one not two.
Okay maybe we transpose Matt to that one dot shape Matt to t to stay consistent while we go above.
Let's make Matt three Eagles MP dot map one and then we want Matt to dot t.
So this should work if our shapes are aligned Matt.
Three beautiful.
And now if we have Matt 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 accesses 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 3 is the same as the outside numbers.
Wonderful.
Now when I first came across dot 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 at like Matrix Multiplication done 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 see
in the next video.