Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lujihong opened this issue May 9, 2022 · 6 comments
Labels
question Further information is requested

Comments

@lujihong
Copy link

lujihong commented May 9, 2022

No description provided.

@the-dijkstra
Copy link
Member

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
];

@the-dijkstra the-dijkstra added the question Further information is requested label Jul 26, 2022
@lujihong
Copy link
Author

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
Copy link
Member

@lujihong

  • The score can range from [0,1]. you can think of it as a percentage [0%, 100%].
  • This package has 3 schemes to calculate the similarity between 2 users. (Weighted Slopeone, Weighted cosine, Cosine). each similarity function has a requirements for the dimension. I believe the Slope-one function is the scheme that can operate with the least amount of data-points (u1[0.5,0.5], u2[0.7,0.8])
  • There is real project made with laravel but it's a little bit old and unmaintained. I will look for the link and attach it here.

@the-dijkstra
Copy link
Member

the-dijkstra commented Jul 28, 2022

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)

  • Venue HasMany Rating
  • User HasMany Rating
  • Rating BelongsTo Venue
  • Rating BelongsTo User
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
Copy link
Author

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

@the-dijkstra
Copy link
Member

@lujihong great. good luck mate.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants