Can someone shoe me how to work out cosine similarity please? I understand that someone has answered a similar question beforesimilar question link but i do not understand how the end result was reached.
Asked
Active
Viewed 177 times
-2
-
Use the equation. *Where* do you fail? – Has QUIT--Anony-Mousse May 17 '16 at 19:27
-
i don't understand the equation is where i fail – user3412172 May 18 '16 at 11:38
-
Then you need to get a **linear algebra book** and study that! We don't know where to start. – Has QUIT--Anony-Mousse May 18 '16 at 12:22
-
on the similar question link that is posted on the question, the best answer shows the cosine similarity of 0.822. all i want to know is how he got that answer from the previous step – user3412172 May 18 '16 at 12:26
-
He computed the equation, which is straightforward linear algebra. – Has QUIT--Anony-Mousse May 18 '16 at 12:45
1 Answers
0
As cosine similarity equation is
And in the similar question link the chosen answer has computed two vectors standing for word counts for two sentences.
A = (2,1,0,2,0,1,1,1)
B = (2,1,1,1,1,0,1,1)
So we can compute the dot product of A and B is
dotProduct(A,B) = 2x2 + 1x1 + 0x1 + 2x1 + 0x1 + 1x0 + 1x1 + 1x1 = 9
and magnitude of A and B are
magnitude(A) = sqrt(2x2 + 1x1 + 0x0 + 2x2 + 0x0 + 1x1 + 1x1 + 1x1) = 3.464
magnitude(B) = sqrt(2x2 + 1x1 + 1x1 + 1x1 + 1x1 + 0x0 + 1x1 + 1x1) = 3.162
Then we could apply the equation:
similarity = cos(theta) = dotProduct(A,B) / (magnitude(A) x magnitude(B))
= 9 / (3.464 x 3.162)
= 0.822
where theta is the angle between vector A and vector B