|
1 | | -namespace Algorithms.RecommenderSystem |
| 1 | +namespace Algorithms.RecommenderSystem; |
| 2 | + |
| 3 | +public class CollaborativeFiltering(ISimilarityCalculator similarityCalculator) |
2 | 4 | { |
3 | | - public class CollaborativeFiltering(ISimilarityCalculator similarityCalculator) |
4 | | - { |
5 | | - private readonly ISimilarityCalculator similarityCalculator = similarityCalculator; |
| 5 | + private readonly ISimilarityCalculator similarityCalculator = similarityCalculator; |
6 | 6 |
|
7 | | - /// <summary> |
8 | | - /// Method to calculate similarity between two users using Pearson correlation. |
9 | | - /// </summary> |
10 | | - /// <param name="user1Ratings">Rating of User 1.</param> |
11 | | - /// <param name="user2Ratings">Rating of User 2.</param> |
12 | | - /// <returns>double value to reflect the index of similarity between two users.</returns> |
13 | | - public double CalculateSimilarity(Dictionary<string, double> user1Ratings, Dictionary<string, double> user2Ratings) |
| 7 | + /// <summary> |
| 8 | + /// Method to calculate similarity between two users using Pearson correlation. |
| 9 | + /// </summary> |
| 10 | + /// <param name="user1Ratings">Rating of User 1.</param> |
| 11 | + /// <param name="user2Ratings">Rating of User 2.</param> |
| 12 | + /// <returns>double value to reflect the index of similarity between two users.</returns> |
| 13 | + public double CalculateSimilarity(Dictionary<string, double> user1Ratings, Dictionary<string, double> user2Ratings) |
| 14 | + { |
| 15 | + var commonItems = user1Ratings.Keys.Intersect(user2Ratings.Keys).ToList(); |
| 16 | + if (commonItems.Count == 0) |
14 | 17 | { |
15 | | - var commonItems = user1Ratings.Keys.Intersect(user2Ratings.Keys).ToList(); |
16 | | - if (commonItems.Count == 0) |
17 | | - { |
18 | | - return 0; |
19 | | - } |
20 | | - |
21 | | - var user1Scores = commonItems.Select(item => user1Ratings[item]).ToArray(); |
22 | | - var user2Scores = commonItems.Select(item => user2Ratings[item]).ToArray(); |
| 18 | + return 0; |
| 19 | + } |
23 | 20 |
|
24 | | - var avgUser1 = user1Scores.Average(); |
25 | | - var avgUser2 = user2Scores.Average(); |
| 21 | + var user1Scores = commonItems.Select(item => user1Ratings[item]).ToArray(); |
| 22 | + var user2Scores = commonItems.Select(item => user2Ratings[item]).ToArray(); |
26 | 23 |
|
27 | | - double numerator = 0; |
28 | | - double sumSquare1 = 0; |
29 | | - double sumSquare2 = 0; |
30 | | - double epsilon = 1e-10; |
| 24 | + var avgUser1 = user1Scores.Average(); |
| 25 | + var avgUser2 = user2Scores.Average(); |
31 | 26 |
|
32 | | - for (var i = 0; i < commonItems.Count; i++) |
33 | | - { |
34 | | - var diff1 = user1Scores[i] - avgUser1; |
35 | | - var diff2 = user2Scores[i] - avgUser2; |
| 27 | + double numerator = 0; |
| 28 | + double sumSquare1 = 0; |
| 29 | + double sumSquare2 = 0; |
| 30 | + double epsilon = 1e-10; |
36 | 31 |
|
37 | | - numerator += diff1 * diff2; |
38 | | - sumSquare1 += diff1 * diff1; |
39 | | - sumSquare2 += diff2 * diff2; |
40 | | - } |
| 32 | + for (var i = 0; i < commonItems.Count; i++) |
| 33 | + { |
| 34 | + var diff1 = user1Scores[i] - avgUser1; |
| 35 | + var diff2 = user2Scores[i] - avgUser2; |
41 | 36 |
|
42 | | - var denominator = Math.Sqrt(sumSquare1 * sumSquare2); |
43 | | - return Math.Abs(denominator) < epsilon ? 0 : numerator / denominator; |
| 37 | + numerator += diff1 * diff2; |
| 38 | + sumSquare1 += diff1 * diff1; |
| 39 | + sumSquare2 += diff2 * diff2; |
44 | 40 | } |
45 | 41 |
|
46 | | - /// <summary> |
47 | | - /// Predict a rating for a specific item by a target user. |
48 | | - /// </summary> |
49 | | - /// <param name="targetItem">The item for which the rating needs to be predicted.</param> |
50 | | - /// <param name="targetUser">The user for whom the rating is being predicted.</param> |
51 | | - /// <param name="ratings"> |
52 | | - /// A dictionary containing user ratings where: |
53 | | - /// - The key is the user's identifier (string). |
54 | | - /// - The value is another dictionary where the key is the item identifier (string), and the value is the rating given by the user (double). |
55 | | - /// </param> |
56 | | - /// <returns>The predicted rating for the target item by the target user. |
57 | | - /// If there is insufficient data to predict a rating, the method returns 0. |
58 | | - /// </returns> |
59 | | - public double PredictRating(string targetItem, string targetUser, Dictionary<string, Dictionary<string, double>> ratings) |
60 | | - { |
61 | | - var targetUserRatings = ratings[targetUser]; |
62 | | - double totalSimilarity = 0; |
63 | | - double weightedSum = 0; |
64 | | - double epsilon = 1e-10; |
| 42 | + var denominator = Math.Sqrt(sumSquare1 * sumSquare2); |
| 43 | + return Math.Abs(denominator) < epsilon ? 0 : numerator / denominator; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Predict a rating for a specific item by a target user. |
| 48 | + /// </summary> |
| 49 | + /// <param name="targetItem">The item for which the rating needs to be predicted.</param> |
| 50 | + /// <param name="targetUser">The user for whom the rating is being predicted.</param> |
| 51 | + /// <param name="ratings"> |
| 52 | + /// A dictionary containing user ratings where: |
| 53 | + /// - The key is the user's identifier (string). |
| 54 | + /// - The value is another dictionary where the key is the item identifier (string), and the value is the rating given by the user (double). |
| 55 | + /// </param> |
| 56 | + /// <returns>The predicted rating for the target item by the target user. |
| 57 | + /// If there is insufficient data to predict a rating, the method returns 0. |
| 58 | + /// </returns> |
| 59 | + public double PredictRating(string targetItem, string targetUser, Dictionary<string, Dictionary<string, double>> ratings) |
| 60 | + { |
| 61 | + var targetUserRatings = ratings[targetUser]; |
| 62 | + double totalSimilarity = 0; |
| 63 | + double weightedSum = 0; |
| 64 | + double epsilon = 1e-10; |
65 | 65 |
|
66 | | - foreach (var otherUser in ratings.Keys.Where(u => u != targetUser)) |
| 66 | + foreach (var otherUser in ratings.Keys.Where(u => u != targetUser)) |
| 67 | + { |
| 68 | + var otherUserRatings = ratings[otherUser]; |
| 69 | + if (otherUserRatings.ContainsKey(targetItem)) |
67 | 70 | { |
68 | | - var otherUserRatings = ratings[otherUser]; |
69 | | - if (otherUserRatings.ContainsKey(targetItem)) |
70 | | - { |
71 | | - var similarity = similarityCalculator.CalculateSimilarity(targetUserRatings, otherUserRatings); |
72 | | - totalSimilarity += Math.Abs(similarity); |
73 | | - weightedSum += similarity * otherUserRatings[targetItem]; |
74 | | - } |
| 71 | + var similarity = similarityCalculator.CalculateSimilarity(targetUserRatings, otherUserRatings); |
| 72 | + totalSimilarity += Math.Abs(similarity); |
| 73 | + weightedSum += similarity * otherUserRatings[targetItem]; |
75 | 74 | } |
76 | | - |
77 | | - return Math.Abs(totalSimilarity) < epsilon ? 0 : weightedSum / totalSimilarity; |
78 | 75 | } |
| 76 | + |
| 77 | + return Math.Abs(totalSimilarity) < epsilon ? 0 : weightedSum / totalSimilarity; |
79 | 78 | } |
80 | 79 | } |
0 commit comments