0
$\begingroup$

I'm trying to compute the cosine similarity of the following vector:

1 1 0 1
2 0 1 1

$$ \frac {1*2+1*0+0*1+1*1}{ \sqrt{1^2+1^2+0^2+1^2}\sqrt{2^2+0^2+1^2+1^2}} \,\,.$$

According to Wolframalpha this is equal to:

$$\frac{3}{1.732 * 2.449} = 0.707 \,\,.$$

But according to this document Example for computing cosine similarity (where I have the upper equation from) the result is $0.72$.

Who is wrong?

$\endgroup$
  • 1
    $\begingroup$ I don't see anything off topic in asking how to calculate cosine similarity. $\endgroup$ – gung - Reinstate Monica Dec 16 '15 at 13:02
2
$\begingroup$

First, in R this is

v1 = c(1, 1, 0, 1)
v2 = c(2, 0, 1, 1)
sum(v1 * v2) / sqrt(sum(v1^2) * sum(v2^2))
## 0.7071

Second, by hand you can find that this is exactly equal to $\frac{1}{\sqrt 2}$ which is approximately 0.707. So Wolfram Alpha is correct. The pdf might have rounded strangely or made an arithmetic error.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.