我有一个关于在推荐系统中使用皮尔逊相关系数的问题。
我的数据库中目前有 3 个集合。1个用户,1个餐厅,1个评论。
我编写了一个函数,它接受 2 个用户 ID 和他们提交的评论列表并返回一个双精度数,这是基于他们提交的评论的 2 个用户之间的皮尔逊相关系数。
所以这个函数的作用是列出用户提交的所有评论的 2 个列表。然后一个 for 循环检查他们是否有评论留在同一家餐厅,并将这些评论放在一个列表中。该列表用于计算系数。
我只是想知道我是否以正确的方式使用这个系数。我想给第一个用户推荐。我可以用这个系数作为一个很好的指标来判断一个人是否很适合另一个用户吗?
如果这不是匹配用户的好方法,那么更好的方法是什么?
如果有人想知道,这是我计算系数的函数。
public static double CalculatePearsonCorrelation(Guid userId1, List<Review> user1Reviews,
Guid userId2, List<Review> user2Reviews)
{
//Resetting the dictionary
restaurantRecommendations = new Dictionary<Guid, List<Review>>();
//Matching the reviews with the corresponding user
restaurantRecommendations.Add(userId1, user1Reviews);
restaurantRecommendations.Add(userId2, user2Reviews);
//Check if users have enough reviews to get a correct correlation
if (restaurantRecommendations[userId1].Count < 4)
throw new NotEnoughReviewsException("UserId " + userId1 + " doesn't contain enough reviews for this correlation");
if (restaurantRecommendations[userId2].Count < 4)
throw new NotEnoughReviewsException("UserId " + userId2 + " doesn't contain enough reviews for this correlation");
//This will be the list of reviews that are the same per subject for the two users.
List<Review> shared_items = new List<Review>();
//Loops through the list of reviews of the selected user (userId1)
foreach (var item in restaurantRecommendations[userId1])
{
//Checks if they have any reviews on subjects in common
if (restaurantRecommendations[userId2].Where(x => x.subj.Id == item.subj.Id).Count() != 0)
{
//Adds these reviews to a list on which the correlation will be based
shared_items.Add(item);
}
}
//If they don't have anything in common, the correlation will be 0
if (shared_items.Count() == 0)
return 0;
//I decided users need at least 4 subjects in common, else there won't be an accurate correlation
if (shared_items.Count() < 4)
throw new NotEnoughReviewsException("UserId " + userId1 + " and UserId " + userId2 + " don't have enough reviews in common for a correlation");
////////////////////////// Calculating the pearson correlation //////////////////////////
double product1_review_sum = 0.00f;
double product2_review_sum = 0.00f;
double product1_rating = 0f;
double product2_rating = 0f;
double critics_sum = 0f;
foreach (Review item in shared_items)
{
product1_review_sum += restaurantRecommendations[userId1].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating;
product2_review_sum += restaurantRecommendations[userId2].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating;
product1_rating += Math.Pow(restaurantRecommendations[userId1].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating, 2);
product2_rating += Math.Pow(restaurantRecommendations[userId2].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating, 2);
critics_sum += restaurantRecommendations[userId1].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating *
restaurantRecommendations[userId2].Where(x => x.subj.Id == item.subj.Id).FirstOrDefault().rating;
}
//Calculate pearson correlation
double num = critics_sum - (product1_review_sum * product2_review_sum / shared_items.Count);
double density = Math.Sqrt((product1_rating - Math.Pow(product1_review_sum, 2) / shared_items.Count) *
((product2_rating - Math.Pow(product2_review_sum, 2) / shared_items.Count)));
if (density == 0)
return 0;
return num / density;
}
}