phpjuice / opencf

PHP implementation of the (Weighted Slopeone,Cosine, Weighted Cosine) rating-based collaborative filtering schemes.
https://phpjuice.gitbook.io/opencf
MIT License
94 stars 7 forks source link

Division by zero error #10

Closed imhaggarwal closed 9 months ago

imhaggarwal commented 9 months ago

First of all thank you for developing such an excellend library. While predicting, I sometimes get Division by zero error depending on the input data for prediction. Moreover, it also gives this error verying the algorithm used for training. Is there any condition under which it behaves like this which I'm missing? I'm attaching the error screenshot which might help in debugging. Thank you image

the-dijkstra commented 9 months ago

from what I see in the picture I belive the error is on your end, on line 38 you are trying to divide $predValue = $sum / $freq

in the use case where $vectors[0] is empty, the loop will be skipped and the value of $freq in that case will be 0. so you need to check first the value of $freq before you divide.

imhaggarwal commented 9 months ago

Thanks for your reply. I'm sorry I think I am missing one important detail. The code which is there in the screenshot is from the file phpjuice\opencf\src\Algorithms\Slopeone\Predictor.php . I have not written this code, I just called the getPrediction function.

the-dijkstra commented 9 months ago

hmm I will check that it's been a while since I worked on this package I will make sure to fix the issue.