问题介绍
Collaborative Filtering(协同过滤)是推荐系统中一种常见的技术,用于预测用户对物品的评分或偏好。它基于用户或物品之间的相似性,通过借鉴其他用户或物品的行为模式来预测目标用户的兴趣。
在本问题中,我们将使用协同过滤方法来预测用户对电影的评分。我们使用一个虚拟的电影评分数据集,包含电影ID、用户ID和评分三个字段。我们的目标是给定一个用户ID和电影ID,预测该用户对该电影的评分。
算法原理
协同过滤算法分为两种主要类型:基于用户的方法和基于物品的方法。在本问题中,我们将使用基于用户的协同过滤方法。
基于用户的协同过滤方法假设具有相似兴趣的用户在过去的行为中有类似的评分模式。算法的主要思想是通过计算目标用户和其他用户之间的相似度,找到与目标用户的评分行为最为相似的其他用户,然后根据这些相似用户的评分来预测目标用户对目标电影的评分。
具体而言,我们使用皮尔逊相关系数来计算用户之间的相似度。皮尔逊相关系数的计算公式如下所示:
$$
sim(u, v) = \frac{\sum_{i \in I}(r_{ui} – \overline{r_u})(r_{vi} – \overline{r_v})}{\sqrt{\sum_{i \in I}(r_{ui} – \overline{r_u})^2}\sqrt{\sum_{i \in I}(r_{vi} – \overline{r_v})^2}}
$$
其中,$sim(u, v)$表示用户$u$和用户$v$之间的相似度,$r_{ui}$表示用户$u$对物品$i$的评分,$\overline{r_u}$表示用户$u$的评分均值。
计算步骤
协同过滤的计算步骤如下:
- 读取电影评分数据集。
- 根据用户ID和电影ID找到目标用户和目标电影。
- 筛选出其他用户中评分过目标电影的用户,并计算这些用户与目标用户的皮尔逊相关系数。
- 根据皮尔逊相关系数和这些用户的评分,预测目标用户对目标电影的评分。
- 输出预测结果。
下面我们将使用Python代码实现这些步骤,并对代码进行详细解释。
Python代码实现
首先,我们需要导入所需的库:
import numpy as np
import pandas as pd
然后,我们定义一个函数collaborative_filtering
来执行协同过滤算法:
def collaborative_filtering(user_id, movie_id, ratings):
target_user = ratings[ratings['user_id'] == user_id]
target_movie = ratings[ratings['movie_id'] == movie_id]
target_user_mean = target_user['rating'].mean()
other_users = ratings[(ratings['movie_id'] == movie_id) & (ratings['user_id'] != user_id)]
similarities = []
for _, user in other_users.iterrows():
other_user = ratings[ratings['user_id'] == user['user_id']]
other_user_mean = other_user['rating'].mean()
numerator = ((target_user['rating'] - target_user_mean) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls (other_user['rating'] - other_user_mean)).sum()
denominator = np.sqrt(((target_user['rating'] - target_user_mean) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 2).sum()) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls np.sqrt(((other_user['rating'] - other_user_mean) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 2).sum())
similarity = numerator / denominator
similarities.append((user['user_id'], similarity))
similarities = sorted(similarities, key=lambda x: x[1], reverse=True)
top_users = similarities[:5] # 选择相似度最高的前5个用户
predicted_rating = target_user_mean
for user_id, similarity in top_users:
other_user = ratings[ratings['user_id'] == user_id]
other_user_rating = other_user[other_user['movie_id'] == movie_id]['rating']
if not other_user_rating.empty:
predicted_rating += similarity artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls (other_user_rating.values[0] - other_user_mean)
return predicted_rating
接下来,我们读取虚拟的电影评分数据集,并调用collaborative_filtering
函数进行预测:
ratings_data = {
'user_id': [1, 1, 2, 2, 3, 3, 4, 4, 5, 5],
'movie_id': [1, 2, 1, 2, 1, 2, 1, 2, 1, 2],
'rating': [5, 4, 3, 5, 4, 2, 3, 1, 5, 4]
}
ratings = pd.DataFrame(ratings_data)
user_id = 1
movie_id = 2
predicted_rating = collaborative_filtering(user_id, movie_id, ratings)
print("Predicted rating:", predicted_rating)
运行上述代码,我们将得到预测结果。
代码细节解释
在上述代码中,我们首先通过ratings[ratings['user_id'] == user_id]
和ratings[ratings['movie_id'] == movie_id]
筛选出目标用户和目标电影的评分。然后,我们计算目标用户的评分均值target_user_mean
。
接下来,我们筛选出其他用户中评分过目标电影的用户。对于每个其他用户,我们计算用户之间的皮尔逊相关系数,并将结果保存在similarities
列表中。
然后,我们根据相似度对其他用户进行排序,并选择相似度最高的前5个用户作为相似用户。
根据相似用户的评分和相似度,我们预测目标用户对目标电影的评分。最后,我们返回预测结果。
在上述代码中,我们使用了Pandas库来处理数据和进行相关计算。我们还使用了NumPy库中的函数来计算相关公式中的数学运算。
总结
通过基于用户的协同过滤算法,我们可以根据用户之间的相似度来预测用户对物品的评分或偏好。在本问题中,我们详细介绍了协同过滤算法的原理和公式推导,并给出了详细的计算步骤和Python代码实现。最终,我们可以使用该算法预测用户对电影的评分。
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/823963/
转载文章受原作者版权保护。转载请注明原作者出处!