基于物品的协同过滤使用余弦相似度电影推荐系统的Python代码,使用MovieLens数据集
时间: 2024-02-24 22:53:52 浏览: 120
好的,以下是基于物品的协同过滤使用余弦相似度电影推荐系统的Python代码,使用MovieLens数据集:
```python
import pandas as pd
import numpy as np
# 读取数据集
ratings = pd.read_csv('ratings.csv')
movies = pd.read_csv('movies.csv')
# 合并数据集
ratings = pd.merge(ratings, movies, on='movieId')
# 计算电影评分矩阵
movieRatings = ratings.pivot_table(index=['title'], columns=['userId'], values='rating')
# 填充缺失值
movieRatings = movieRatings.fillna(0)
# 计算余弦相似度
from sklearn.metrics.pairwise import cosine_similarity
movieSimilarity = cosine_similarity(movieRatings.T)
# 定义函数,返回与目标电影最相似的K个电影
def topKSimilarMovies(movie, K):
simMovies = movieSimilarity[movie]
simMovies = simMovies.argsort()[::-1]
return simMovies[1:K+1]
# 定义函数,根据最相似的K个电影推荐给用户
def recommendMovies(user, K):
userRatings = movieRatings[user]
simMovies = topKSimilarMovies(userRatings.name, K)
simMoviesRatings = movieRatings.iloc[simMovies]
weightedRatings = (simMoviesRatings * movieSimilarity[userRatings.name][simMovies]).sum(axis=0)
weightedRatings = weightedRatings.sort_values(ascending=False)
moviesToRecommend = weightedRatings.loc[~weightedRatings.index.isin(userRatings.index)]
return moviesToRecommend.index.tolist()
# 示例:为用户1推荐10部电影
user = 1
K = 10
recommendedMovies = recommendMovies(user, K)
print(recommendedMovies)
```
注意:上述代码仅作为示例,实际应用时需要根据具体情况进行调整和优化。
阅读全文