How to implement collaborative filtering and recommendation system with PHP
Collaborative filtering and recommendation system is a very commonly used algorithm and technology, widely used in e-commerce, social media and online services. The collaborative filtering algorithm analyzes a user's behavior and preferences, compares it with the behavior of other users to find similar users, and makes personalized recommendations for users based on these similarities. This article will introduce how to implement collaborative filtering and recommendation systems in PHP.
$ratings = [ 'user1' => ['item1' => 4, 'item2' => 3, 'item3' => 5], 'user2' => ['item1' => 5, 'item2' => 1, 'item3' => 2], 'user3' => ['item1' => 2, 'item2' => 4, 'item3' => 1], ];
function pearson_similarity($ratings, $user1, $user2) { $common_items = array_intersect(array_keys($ratings[$user1]), array_keys($ratings[$user2])); $n = count($common_items); $sum1 = $sum2 = $sum1_sq = $sum2_sq = $p_sum = 0; foreach ($common_items as $item) { $rating1 = $ratings[$user1][$item]; $rating2 = $ratings[$user2][$item]; $sum1 += $rating1; $sum2 += $rating2; $sum1_sq += pow($rating1, 2); $sum2_sq += pow($rating2, 2); $p_sum += $rating1 * $rating2; } $num = $p_sum - ($sum1 * $sum2 / $n); $den = sqrt(($sum1_sq - pow($sum1, 2) / $n) * ($sum2_sq - pow($sum2, 2) / $n)); if ($den == 0) return 0; return $num / $den; }
function user_based_recommendation($ratings, $user, $n = 5) { $similarity = array(); $weighted_sum = array(); $similarity_sum = array(); foreach ($ratings as $other_user => $items) { if ($other_user == $user) continue; $sim = pearson_similarity($ratings, $user, $other_user); if ($sim <= 0) continue; foreach ($items as $item => $rating) { if (!isset($ratings[$user][$item]) || $ratings[$user][$item] == 0) { $weighted_sum[$item] += $rating * $sim; $similarity_sum[$item] += $sim; } } } $rankings = array(); foreach ($weighted_sum as $item => $weighted_rating) { if ($similarity_sum[$item] > 0) { $rankings[$item] = $weighted_rating / $similarity_sum[$item]; } } arsort($rankings); return array_slice($rankings, 0, $n, true); }
In the above example code, $n
represents the number of recommendations to be generated, and the default is 5. user_based_recommendation
The function will return an array of items sorted from high to low by recommendation score.
$recommendations = user_based_recommendation($ratings, 'user1', 3); foreach ($recommendations as $item => $rating) { echo "推荐物品:$item, 评分:$rating "; }
The above example will generate 3 recommended items for user1
, and output the result.
Summary:
Through the above steps, we have shown how to use PHP to implement collaborative filtering and recommendation systems. First, we prepared the data of user-item matrix and calculated the similarity between users. Then, personalized recommendations are generated based on the similarity. This is just one implementation method, and actual applications may need to be modified appropriately according to specific needs. I hope this article can help you understand how to use PHP to implement collaborative filtering and recommendation systems.
The above is the detailed content of How to implement collaborative filtering and recommendation systems with PHP. For more information, please follow other related articles on the PHP Chinese website!