I create a recommendation engine which finds item similarities according to user ratings. I'm trying to use adjusted cosine similarity to find similarities. I follow these steps.

  1. Find mean rating of an every item.
  2. Subtract mean rating from each item rating.
  3. Apply cosine similarity.

My problem is at the second step. If all users give same rating to an item, subtracting mean rating from each rating creates zero vector. Because this vectors are dividers in cosine similarity, this causes zero division error. So is there a solution for this?


1 Answer 1


welcome to the Data science SE community:) A couple of questions in regards to the OP.

  1. In what percentage of cases is this problem of dividing by zero would occur. Could you compute the percentage of such data points in the entire dataset after step 2. Based on the percentage, you could either consider dropping those points ( of the percentage is too less), similarly think of other approaches to go about tackling the problem, which brings me to point nr.2

  2. Might think of using some other similarly measure which could solve this problem https://pytorch.org/docs/stable/generated/torch.nn.CosineSimilarity.html This similarity score adds a small epsilon to avoid division by zero error.

  • $\begingroup$ I can not ignore zero values but adding a small epsilon value is great solution. Thank you $\endgroup$
    – Ertugrul
    Sep 6, 2022 at 6:35

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.