3. Maths4ML: Dot Product

#Drawbacks of Rulers (Euclidean Distance)

#Example :

There is data of 2 people listening to music :

Person APerson B
Time per day5 hours (300 mins)10 mins
jazz270 mins (90%)9 mins (90%)
pop30 mins (10%)1 min (10%)

When plotted on a graph where axes are min. of jazz & min. of pop, then person A & B are miles apart.

The Euclidean distance between them will be huge.

  • It tells how much physical space separates these two points?

Thus according to Euclidean distance, A & B have completely different taste. But logically, their tastes are same only the magnitude is different.

To solve this problem of looking at magnitude or looking at directions, Shadows are used.

#Shadow (Projection)

Let there be 2 vectors A and B. Let a light source is shining down from above on A, perpendicular to B and casts shadow of A on B.

The length of shadow tells how much of vector A goes in the direction of vector B.

Or it can also be conveyed as drawing a straight line from the tip of the top vector down so that it hits the bottom vector perpendicularly.

The angle between the vectors/arrow tells the cosine similarity.

  • Vectors pointing is same directions will have angle of : perfectly aligned.
  • Vectors perpendicular to each other will have angle of : unrelated.

#Dot Product

To understand projections dot product and its geometric definition is helpful.

Multiply matching coordinates and sum them up.

#Geometric definition

So, dot product is basically Length of A x Length of B x Percentage of alignment ().

cosine similarity

cosine similarity

From trigonometry,

It is saying, how much of a exists in the direction of b.

So, Dot Product is designed to measure the total impact of one vector moving along another.

#Cosine Similarity

This comes directly from the geometric definition above.

The vectors get normalized by dividing by lengths. This turns vectors a & b into unit vectors. And now only their alignment is left to compare.

  • Result +1.0: Perfect Match ().
  • Result 0.0: Orthogonal / No Relation ().
    • Because .
  • Result -1.0: Exact Opposites ().

#Shadow Caster

Drag the white arrow (vector A) or adjust the sliders to change its length and direction.

  • The yellow line is the projection / shadow cast upon the green / ground vector.
  • When the white arrow is straight up, shodow is gone and cosine similarity is 0, signifying orthogonality or independence.
  • An obtuse angle or () represents negative correlation

With this, dot product and its geometric meaning is covered.

Table of Contents