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

How to implement similarity recommendation based on goods? Can you write an example? #9

Open lujihong opened 2 years ago

the-dijkstra commented 1 year ago

sorry for the late reply, here is a quick example:

$dataset = [
    "Intel core i5-12400" => [
        "user1" => 1,
        "user2" => 1,
        "user3" => 0.2,
    ],
    "Intel core i5-12900k" => [
        "user1" => 0.5,
        "user3" => 0.4,
        "user4" => 0.9,
    ],
    "Ryzen 5 5600x" => [
        "user1" => 0.2,
        "user2" => 0.5,
        "user3" => 1,
        "user4" => 0.4,
    ],
    "Asus Prime B660" => [
        "user2" => 0.2,
        "user3" => 0.4,
        "user4" => 0.5,
    ],
];

$recommenderService = new RecommenderService($dataset);

$recommender = $recommenderService->weightedSlopeone(); // WeightedSlopeone recommender

Now let's say a new user enters your shop and rates the Intel core i5-12400 with 0.4, you can now predict his ratings for other goods in your shop based on what he already rated, and based on other ratings from other users.

// Predict future ratings
$results = $recommender->predict([
    "Intel core i5-12400" => 0.4
]);

[
  "Intel core i5-12900k" => 0.25,
  "Ryzen 5 5600x" => 0.23,
  "Asus Prime B660" => 0.1
];
lujihong commented 1 year ago

Thank you very much for your reply. Can this score only be in the range of 0.1-1? And can it be 0.01,0.15,0.001... Or something?

In addition, from which dimensions can each user's score be generated, and how many points are appropriate for each generated dimension?

Is there a real project case that can explain how scores are generated and stored? Thank you!

the-dijkstra commented 1 year ago

@lujihong

the-dijkstra commented 1 year ago

Here is a database diagram for an app that stores user ratings for visited venues. and serves recommendations based on the those ratings.

drawSQL-export-2022-07-28_12_01

And here is the function that builds the recommendation service from the ratings, assuming here that I have Model called Rating

Context: the following code snippets are taken from a Laravel app with the following models (User, Rating, Venue)

public function getRecommender() {
  foreach (Venue::with(['ratings'])->all() as $venue) {
          foreach($venue->ratings as $rating) {
                [$venue->id][$rating->user_id] = $rating->rating;
          }  
  }

  $recommenderService = new RecommenderService($dataset);
  return $recommender = $recommenderService->weightedSlopeone(); 
}

To get the predictions for the current user here is the function that does that:

public function getPredictions($request) {
      $currentUser = $request->user();

      $evaluations = [];

      foreach ($currentUser->ratings as $rating ) {
          $evaluations[$rating->venue_id] = $rating->rating;
      }

      $recommender = $this->getRecommender();

      return $recommender->predict($evaluation);
}

If I manage to get a working laravel example I will attach it here.

lujihong commented 1 year ago

Thank you very much for your serious answer. I'll study it and ask you if I don't understand it.

the-dijkstra commented 1 year ago

@lujihong great. good luck mate.

Note: I have updated the code to reflect the last version of the package.