Linear Algebra for Machine Learning - Dot product and angle between 2 vectors Lecture 3

**The Concept of Angle between Two Vectors**

One of the fundamental concepts in linear algebra and geometry is the angle between two vectors. We saw this idea in 2D space where we could easily find the length of a vector and then use it to calculate the angle between two vectors. The formula for finding the angle between two vectors in 2D space is given by cos theta = (a1 * B1 + a2 * B2) / (sqrt(a1^2 + a2^2) * sqrt(B1^2 + B2^2)).

If we have two vectors, let's say vector A and vector B, and they are perpendicular to each other, which means the angle between them is 90 degrees. In this case, if we know that the dot product of A and B is zero, we can conclude that these two vectors are perpendicular to each other. This is because the dot product formula gives us (a1 * B1 + a2 * B2) = 0, which implies that the angle between A and B is 90 degrees.

**Extension to n-Dimensional Space**

Now, let's extend this concept to any dimensional space. We know that in 3D space, we can visualize vectors and find their lengths and angles with each other. But what if we have vectors in an n-dimensional space? Can we still calculate the angle between two vectors?

The answer is yes. If we have two vectors A and B in n-dimensional space, and we compute their dot product, it will be given by the formula: a · b = (a1 * b1) + (a2 * b2) + ... + (an * bn), where an and bn are the corresponding components of vectors A and B.

If this dot product is zero, then we can conclude that the two vectors are perpendicular to each other. And if we want to find the angle between these two vectors, we can use the formula cos theta = a · b / (|A| * |B|), where |A| and |B| are the magnitudes of vectors A and B.

**Geometric Interpretation**

But what does this dot product actually mean? When we compute a · b, we are essentially computing the sum of the products of corresponding components of vectors A and B. In 2D space, this gives us the length of vector A multiplied by the length of vector B times cos theta.

If we take this to n-dimensional space, the dot product becomes (a1 * b1) + (a2 * b2) + ... + (an * bn), which is nothing but a1^2 + a2^2 + ... + an^2. This is the square of the length of vector A.

In any dimensional space, this means that when we compute a · a, we get the square of the length of vector A from the origin. This is because the dot product formula gives us (a1 * a1) + (a2 * a2) + ... + (an * an), which is equivalent to a1^2 + a2^2 + ... + an^2.

**Extension to n-Dimensional Space**

We can extend this idea of computing angles between vectors in any dimensional space. If we have two vectors A and B in n-dimensional space, and we compute their dot product, it will give us the sum of the products of corresponding components of vectors A and B.

If this dot product is zero, then we can conclude that the two vectors are perpendicular to each other. And if we want to find the angle between these two vectors, we can use the formula cos theta = a · b / (|A| * |B|), where |A| and |B| are the magnitudes of vectors A and B.

This is what we've learned in today's video - that we can extend our understanding of angles between vectors from 2D space to any dimensional space. We have seen how the dot product formula works, how it relates to lengths and angles, and how it applies to n-dimensional space.

"WEBVTTKind: captionsLanguage: enso imagine if you're given two vectors a and B let's assume both of them are row vectors right let's assume I have a 1 a 2 so on so forth am similarly vector B which has B 1 components B 1 B 2 so on so forth the N now mathematically let's learn some very basic operations what about addition of a and B if I add these two vectors what do I get I simply get it's basically called component wise the addition a 2 plus B 2 so what I'm doing here is I'm basically taking the first component from both a and B and I am summing it up that becomes the first component of a plus B let's assume a plus B equals to C similarly I am taking the second component here and I'm summing it up here right very very straightforward idea so so on so forth a n plus BN this is how you add two vectors very very simple idea this is the addition of vectors then the immediate question that comes to your mind is is there something like multiplication because we learnt all these concepts in in simple algebra right so is there is there a concept of multiplication of two vectors there are actually four vectors that two types of multiplications there is something called a dot product and a cross product there are two types of multiplications so some of you who have studied physics in your lemons and 12th grades would recall you what what dot product is and what a cross product is so just for simplicity we don't use cross product much in machine learning so I will not go into the details of what a cross product is but let's go and understand what a dot product is so if I'm given two vectors a and B a dot product is written as a dot B right first I'll explain the notation and then we'll go and understand the geometry behind it so a dot B is nothing but you take again it's a component wise multiplication a dot B is nothing but a 1 multiplied by B 1 plus a 2 multiplied by B 2 plus a 3 multiplied by B 3 so on so forth a n multiplied you might ask okay this multiplication part seems logical to us but why are you adding just take it from me that this is just notation okay just let's assume we're understanding notation I promise you're connected with geometry very soon very quickly okay so for now let's let's define it this way now if I write it in vector notation what is this a 1 B 1 plus a 2 B 2 a 3 B 3 so on and so forth I can actually write it in vector notation right I can write it as a 1 a 2 so on so forth if I have a row vector for a and if I have a column vector for B B 1 B 2 so on so forth BN some of you might have studied about about multiplying matrices and multiplying and multiplying vectors in your 11th or 12th grade so what does this mean this basically means that so this is first of all let's remember this is a 1 cross n matrix or a 1 cross it's basically a row vector this is a column vector which means it is n cross 1 as long as this and this match you can multiply them right so since this is N and this is also n you can multiply these two vectors or these 2 matrices if you want to think about it this is a matrix with only one row this is a matrix with only one column right when I multiply them the simple matrix multiplication that we all learned in high school it's nothing but a 1 multiplied by B 1 plus a 2 multiplied by B 2 and so on and so forth right now from a notation standpoint typically whenever somebody says a vector they're typically just for notation sake they typically refer to it as a column vector so whenever somebody says a is a vector of size n what do they mean they typically mean that a is a column vector this is just for simplicity whenever somebody says a vector by default it's a it's a column vector because that's always is confusion is a given vector column vector or row vector if nothing is explicitly told about that you have to assume that itself it's it's a row it's a column vector sorry right so what we have here is a row vector of a and a column vector of B this is also written as numerically this was also I mean for simplicity when you a row vector when you have a row vector let's assume I have a row vector a 1 so let's let's do I have a column vector just for simplicity I have a 1 a 2 so on so forth a n right let's assume this is my vector a because the default representation is always a column vector which is of size n cross 1 my n rows and 1 column all right now what does a transpose mean a transpose means basically swap your rows with columns and columns with rows so a transpose a so some of you have learned basic linear algebra in your high school or 11th and 12th grade would mean it will recognize this a transpose nothing but a 1 a 2 so on so forth am so I can convert a column matrix column vector into a row vector by just taking a operator called transpose and this becomes 1 cross M so going back to our previous example what we have here literally is nothing but a transpose multiplied by B right so you can write a dot B as nothing but a transpose B and what does it all boil down to it boiled down to summation of 1 to n if if a is an N dimensional vector a I bi this is what a dot product is from from a form of pure linear algebra perspective but now let's go and understand geometrically what does a dot B mean because that's important right where to connect geometry with linear algebra because if you don't connect it we don't understand what a dot product means in high dimensional spaces because we're doing all of this new algebraic exercise to understand geometry so let's take let's assume we live in a two dimensional space or we have a two dimensional space let's assume I have a vector a and vector B of course a has two components a 1 and a 2 B also has two components B 1 and B 2 right now when we have these two components this is your vector a right this is your vector B now what does a dot B represent geometrically a dot B represents a dot B can be written as I'll explain what each of them mean okay let's assume that angle between a and B is Theta and the length of a so this is this is nothing but the length of a length of vector a and this is your origin right so length of a is nothing but distance from distance of a from origin similarly this is length of B so length of a vector is represented with two two dashes surrounding it okay this is nothing but length of a vector okay so what is what is a with two bars behind it it is nothing but this length this length that will just highlight it for you it is nothing but this length and this length is nothing but B so a dot B is nothing but the length of a multiplied the length of B multiplied by cos of the angle between them right now this is very useful now if somebody gives you two vectors as a1 a2 how do you determine what is the angle between them right so let's let us quickly see that so if somebody gave you a envy what is I can write a dot B as a 1 B 1 plus a 2 B 2 which is also equal to length of a length of B cos theta which means theta is nothing but cos inverse of some people also call it R cos cos inverse of a 1 B 1 plus a 2 B 2 right divided by length of a and length of B and we know that length of a length of a is nothing but a1 square plus a2 square under root we saw this distance of a distance of a point from an origin in one of the previous videos so this is the length length of a vector so if somebody gives you two vectors a and B you can easily find the angle between both of them if you just know the components a 1 a 2 B 1 B right I can I can still write so basically I can write this a the length of a as a 1 and a 2 so if you just give me the components of a1 a2 b1 b2 I can find the angle between those two vectors now this takes us to an interesting idea what if I have two vectors let's let's say I have two vectors okay I have a vector a here and I have a vector B here let's assume they're perpendicular to each other which basically means the angle between them is 90 degrees in the X assume this is x1 and this is x2 two dimensions right your a is a 1 a 2 your B again is B 1 B 2 now the quick question is what is now if I know that a dot B what what will a dot B be like okay length of a length of B and cause of 90 and I know that cos of 90 is 0 so a dot B will be 0 because this is 0 and so if a dot product between two vectors is 0 we know that those two vectors are perpendicular to each other right so remember this whole idea of angle between two vectors can be extended to any dimensions your a could be any dimensional vector a 1 a 2 it could lie in any dimensions it could lie in an n dimensional space we do measure B also can exist in an n dimensional space it doesn't matter even though we can't visualize anything more than 3d your a and B could be in any dimensional space I could just compute a dot B which will always be length of a and length B and cos theta right which means I can compute the theta as cos inverse of a way I be I summation write by length of a and length B so if we if you give me two vectors in any dimensional space I can compute the angle between those two vectors I can compute the theta between a and B very very easily because what I have learnt in today I am just extending it to n-dimensional space right this is what I learnt in 2d right whatever I learned in 2d I am just extending it to n-dimensional space similarly if I have two vectors in n-dimensional space and if I compute a dot B what is a dot B it would be is nothing but summation over I equals to 1 to n AI bi if that turns out to be 0 I know whatever dimension the space that a and B could like all that this implies that a is perpendicular to B right I don't care whether you're operating in 2d 3d or nd this is the beauty of linear algebra in linear algebra and continent geometry are very nicely connected the relationships like this and whatever we will learn in 2d we can easily extend it to nd because vectors could be of 2d 3d or any dimensions and all the geometric intuition that we're learning from 2d we can extend to any dimensional space now the next question is what about a dot a what if I multiply I I do a dot product of a with a what does a tour air actually mean it basically means a 1 into a 1 plus a 2 into a 2 so on so forth a n into a n and this is nothing but a 1 square plus a 2 square so on so forth a n square and this is nothing but the distance of a from origin squared right we know that given any point a given any point a its distance from origin is nothing but a1 square plus a2 square plus so on so forth a n square under root right we know that the distance from origin D is can can be written as this and when you do a dot a what we get is basically square of the distance of that vector a from origin and this could this is in any dimensional space the theory still it just holds good that's the beauty of it so what we've done is we have written dot product in 2d right we have written about dot product in 2d right and we have understood geometrically as angles between points and we're extending the same idea to n dimensions because dot product can be generalized when and from that generalization we are understanding the N dimensional geometry without being able to visualize it that's the super duper power of lean logicso imagine if you're given two vectors a and B let's assume both of them are row vectors right let's assume I have a 1 a 2 so on so forth am similarly vector B which has B 1 components B 1 B 2 so on so forth the N now mathematically let's learn some very basic operations what about addition of a and B if I add these two vectors what do I get I simply get it's basically called component wise the addition a 2 plus B 2 so what I'm doing here is I'm basically taking the first component from both a and B and I am summing it up that becomes the first component of a plus B let's assume a plus B equals to C similarly I am taking the second component here and I'm summing it up here right very very straightforward idea so so on so forth a n plus BN this is how you add two vectors very very simple idea this is the addition of vectors then the immediate question that comes to your mind is is there something like multiplication because we learnt all these concepts in in simple algebra right so is there is there a concept of multiplication of two vectors there are actually four vectors that two types of multiplications there is something called a dot product and a cross product there are two types of multiplications so some of you who have studied physics in your lemons and 12th grades would recall you what what dot product is and what a cross product is so just for simplicity we don't use cross product much in machine learning so I will not go into the details of what a cross product is but let's go and understand what a dot product is so if I'm given two vectors a and B a dot product is written as a dot B right first I'll explain the notation and then we'll go and understand the geometry behind it so a dot B is nothing but you take again it's a component wise multiplication a dot B is nothing but a 1 multiplied by B 1 plus a 2 multiplied by B 2 plus a 3 multiplied by B 3 so on so forth a n multiplied you might ask okay this multiplication part seems logical to us but why are you adding just take it from me that this is just notation okay just let's assume we're understanding notation I promise you're connected with geometry very soon very quickly okay so for now let's let's define it this way now if I write it in vector notation what is this a 1 B 1 plus a 2 B 2 a 3 B 3 so on and so forth I can actually write it in vector notation right I can write it as a 1 a 2 so on so forth if I have a row vector for a and if I have a column vector for B B 1 B 2 so on so forth BN some of you might have studied about about multiplying matrices and multiplying and multiplying vectors in your 11th or 12th grade so what does this mean this basically means that so this is first of all let's remember this is a 1 cross n matrix or a 1 cross it's basically a row vector this is a column vector which means it is n cross 1 as long as this and this match you can multiply them right so since this is N and this is also n you can multiply these two vectors or these 2 matrices if you want to think about it this is a matrix with only one row this is a matrix with only one column right when I multiply them the simple matrix multiplication that we all learned in high school it's nothing but a 1 multiplied by B 1 plus a 2 multiplied by B 2 and so on and so forth right now from a notation standpoint typically whenever somebody says a vector they're typically just for notation sake they typically refer to it as a column vector so whenever somebody says a is a vector of size n what do they mean they typically mean that a is a column vector this is just for simplicity whenever somebody says a vector by default it's a it's a column vector because that's always is confusion is a given vector column vector or row vector if nothing is explicitly told about that you have to assume that itself it's it's a row it's a column vector sorry right so what we have here is a row vector of a and a column vector of B this is also written as numerically this was also I mean for simplicity when you a row vector when you have a row vector let's assume I have a row vector a 1 so let's let's do I have a column vector just for simplicity I have a 1 a 2 so on so forth a n right let's assume this is my vector a because the default representation is always a column vector which is of size n cross 1 my n rows and 1 column all right now what does a transpose mean a transpose means basically swap your rows with columns and columns with rows so a transpose a so some of you have learned basic linear algebra in your high school or 11th and 12th grade would mean it will recognize this a transpose nothing but a 1 a 2 so on so forth am so I can convert a column matrix column vector into a row vector by just taking a operator called transpose and this becomes 1 cross M so going back to our previous example what we have here literally is nothing but a transpose multiplied by B right so you can write a dot B as nothing but a transpose B and what does it all boil down to it boiled down to summation of 1 to n if if a is an N dimensional vector a I bi this is what a dot product is from from a form of pure linear algebra perspective but now let's go and understand geometrically what does a dot B mean because that's important right where to connect geometry with linear algebra because if you don't connect it we don't understand what a dot product means in high dimensional spaces because we're doing all of this new algebraic exercise to understand geometry so let's take let's assume we live in a two dimensional space or we have a two dimensional space let's assume I have a vector a and vector B of course a has two components a 1 and a 2 B also has two components B 1 and B 2 right now when we have these two components this is your vector a right this is your vector B now what does a dot B represent geometrically a dot B represents a dot B can be written as I'll explain what each of them mean okay let's assume that angle between a and B is Theta and the length of a so this is this is nothing but the length of a length of vector a and this is your origin right so length of a is nothing but distance from distance of a from origin similarly this is length of B so length of a vector is represented with two two dashes surrounding it okay this is nothing but length of a vector okay so what is what is a with two bars behind it it is nothing but this length this length that will just highlight it for you it is nothing but this length and this length is nothing but B so a dot B is nothing but the length of a multiplied the length of B multiplied by cos of the angle between them right now this is very useful now if somebody gives you two vectors as a1 a2 how do you determine what is the angle between them right so let's let us quickly see that so if somebody gave you a envy what is I can write a dot B as a 1 B 1 plus a 2 B 2 which is also equal to length of a length of B cos theta which means theta is nothing but cos inverse of some people also call it R cos cos inverse of a 1 B 1 plus a 2 B 2 right divided by length of a and length of B and we know that length of a length of a is nothing but a1 square plus a2 square under root we saw this distance of a distance of a point from an origin in one of the previous videos so this is the length length of a vector so if somebody gives you two vectors a and B you can easily find the angle between both of them if you just know the components a 1 a 2 B 1 B right I can I can still write so basically I can write this a the length of a as a 1 and a 2 so if you just give me the components of a1 a2 b1 b2 I can find the angle between those two vectors now this takes us to an interesting idea what if I have two vectors let's let's say I have two vectors okay I have a vector a here and I have a vector B here let's assume they're perpendicular to each other which basically means the angle between them is 90 degrees in the X assume this is x1 and this is x2 two dimensions right your a is a 1 a 2 your B again is B 1 B 2 now the quick question is what is now if I know that a dot B what what will a dot B be like okay length of a length of B and cause of 90 and I know that cos of 90 is 0 so a dot B will be 0 because this is 0 and so if a dot product between two vectors is 0 we know that those two vectors are perpendicular to each other right so remember this whole idea of angle between two vectors can be extended to any dimensions your a could be any dimensional vector a 1 a 2 it could lie in any dimensions it could lie in an n dimensional space we do measure B also can exist in an n dimensional space it doesn't matter even though we can't visualize anything more than 3d your a and B could be in any dimensional space I could just compute a dot B which will always be length of a and length B and cos theta right which means I can compute the theta as cos inverse of a way I be I summation write by length of a and length B so if we if you give me two vectors in any dimensional space I can compute the angle between those two vectors I can compute the theta between a and B very very easily because what I have learnt in today I am just extending it to n-dimensional space right this is what I learnt in 2d right whatever I learned in 2d I am just extending it to n-dimensional space similarly if I have two vectors in n-dimensional space and if I compute a dot B what is a dot B it would be is nothing but summation over I equals to 1 to n AI bi if that turns out to be 0 I know whatever dimension the space that a and B could like all that this implies that a is perpendicular to B right I don't care whether you're operating in 2d 3d or nd this is the beauty of linear algebra in linear algebra and continent geometry are very nicely connected the relationships like this and whatever we will learn in 2d we can easily extend it to nd because vectors could be of 2d 3d or any dimensions and all the geometric intuition that we're learning from 2d we can extend to any dimensional space now the next question is what about a dot a what if I multiply I I do a dot product of a with a what does a tour air actually mean it basically means a 1 into a 1 plus a 2 into a 2 so on so forth a n into a n and this is nothing but a 1 square plus a 2 square so on so forth a n square and this is nothing but the distance of a from origin squared right we know that given any point a given any point a its distance from origin is nothing but a1 square plus a2 square plus so on so forth a n square under root right we know that the distance from origin D is can can be written as this and when you do a dot a what we get is basically square of the distance of that vector a from origin and this could this is in any dimensional space the theory still it just holds good that's the beauty of it so what we've done is we have written dot product in 2d right we have written about dot product in 2d right and we have understood geometrically as angles between points and we're extending the same idea to n dimensions because dot product can be generalized when and from that generalization we are understanding the N dimensional geometry without being able to visualize it that's the super duper power of lean logic\n"