As an example, let's say I have a very simple data set. I am given a csv with three columns, user_id, book_id, rating. The rating can be any number 0-5, where 0 means the user has NOT rated the book.

Let's say I randomly pick three users, and I get these feature/rating vectors.

Martin: $<3,3,5,1,2,3,2,2,5>$

Jacob: $<3,3,5,0,0,0,0,0,0>$

Grant: $<1,1,1,2,2,2,2,2,2>$

The similarity calculations:

+--------------+---------+---------+----------+ | | M & J | M & G | J & G | +--------------+---------+---------+----------+ | Euclidean | 6.85 | 5.91 | 6.92 | +--------------+---------+---------+----------+ | Cosine | .69 | .83 | .32 | +--------------+---------+---------+----------+

Now, my expectation of similarity is that Martin and Jacob would be the most similar. I would expect this because they have EXACTLY the same ratings for the books that both of them have rated. But we end up finding that Martin and Grant are the most similar.

I understand mathematically how we get to this conclusion, but I don't understand how I can rely on Cosine Angular distance or Euclidean distance as a means of calculating similarity, if this type of thing occurs. For what interpretation are Martin and Grant more similar than Martin and Jacob?

One thought I had was to just calculate Euclidean distance, but ignore all books for which one user hasn't rated the book.

I then end up with this

+--------------+---------+---------+----------+ | | M & J | M & G | J & G | +--------------+---------+---------+----------+ | Euclidean | 0 | 5.91 | 6.92 | +--------------+---------+---------+----------+ | Cosine | .69 | .83 | .32 | +--------------+---------+---------+----------+

Of course now I have a Euclidean distance of 0, which fits what I would expect of the recommender system. I see many tutorials and lectures use Cosine Angular distance to ignore the unrated books, rather than use Euclidean and ignore them, so I believe this must not work in general.


Just to experiment a little, I adjusted Jacob's feature vector to be much more similar:

Jacob: $<3,3,5,1,2,3,2,0,0>$

When I calculate Cosine Angular distance with Martin, I still only get .82! Still less similar than Martin and Grant, yet by inspection I would expect these two to be very similar.

Could somebody help explain where my thinking is wrong, and possibly suggest another similarity measure?


1 Answer 1


If you look at the definitions of the two distances, cosine distance is the normalized dot product of the two vectors and euclidian is the square root of the sum of the squared elements of the difference vector.

The cosine distance between M and J is smaller than between M and G because the normalization factor of M's vector still includes the numbers for which J didn't have any ratings. Even if you make J's vector more similar, like you did, the remaining numbers of M (2 and 5) get you the number you get. The number for M and G is this high because they both have non-zeroes for all the books. Even though they seem quite different, the normalization factors in the cosine are more "neutralized" by the non-zeroes for corresponding entries in the dot product. Maths don't lie.

The books J didn't rate will be ignored if you make their numbers zero in the computation of the normalization factor for M. Maybe the fault in your thinking is that the books J didn't rate should be 0 while they shouldn't be any number.

Finally, for recommendation systems, I would like to refer to matrix factorization.

  • $\begingroup$ This is exactly what I was looking for. Thank you. $\endgroup$ Oct 5, 2015 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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