What is cosine similarity?
The similarity of two vectors can be measured using cosine similarity. So, let’s start with vector properties. Given two vectors, one vector can be projected onto another to show “how much” a vector is pointing in the same direction as the other. Figure 5.1 shows a 2D graph of the projection of vector a onto vector b.
Figure 5.1 – The projection of vector a on vector b
It is the shadow of vector a being cast on vector b. If the angle is small, the shadow will be long. It means the two vectors are very close. If the angle is as large as 90 degrees, the shadow is almost 0. It means the two vectors are not related at all. Therefore, the angle between the two vectors can measure the similarity. The length of the shadow is the length of a times the cosine of the angle between the two vectors. We will use the dot product of two vectors to mathematically define the similarity.
The dot product of vectors...