python基于协同过滤算法的图书推荐系统代码
时间: 2023-11-07 18:05:33 浏览: 122
以下是一个基于协同过滤算法的图书推荐系统的 Python 代码示例:
```python
import pandas as pd
import numpy as np
# 加载数据集
books = pd.read_csv('books.csv')
ratings = pd.read_csv('ratings.csv')
# 统计用户对书籍的评分数量
ratings_count = pd.DataFrame(ratings.groupby('book_id')['rating'].count())
ratings_count.rename(columns={'rating': 'rating_count'}, inplace=True)
books = pd.merge(books, ratings_count, on='book_id')
# 选取评分数量排名前10的书籍
popular_books = list(ratings_count.sort_values('rating_count', ascending=False).index[:10])
# 选取评分高于平均分的书籍
mean_rating = np.mean(ratings['rating'])
high_rating_books = list(ratings[ratings['rating'] > mean_rating]['book_id'].unique())
# 构建用户-书籍评分矩阵
user_book_ratings = pd.pivot_table(ratings, values='rating', index=['user_id'], columns=['book_id']).fillna(0)
# 计算用户之间的相似度
from sklearn.metrics.pairwise import cosine_similarity
user_similarity = cosine_similarity(user_book_ratings)
# 根据用户之间的相似度,为每个用户推荐书籍
def recommend_books(user_id):
user_ratings = user_book_ratings.loc[user_id].values.reshape(1, -1)
similarity_scores = list(enumerate(user_similarity[user_id]))
similarity_scores = sorted(similarity_scores, key=lambda x: x[1], reverse=True)
similar_users = [i[0] for i in similarity_scores]
recommended_books = []
for user in similar_users:
books_rated = list(user_book_ratings.loc[user][user_book_ratings.loc[user] > 0].index)
recommended_books.extend(list(set(books_rated) - set(recommended_books)))
if len(recommended_books) >= 10:
break
recommended_books = list(set(recommended_books) & set(high_rating_books + popular_books))
return recommended_books
# 测试推荐系统
print(recommend_books(1))
```
请注意,此代码示例仅用于演示目的,实际上线的推荐系统需要更多的数据预处理、模型调参和性能优化等工作。
阅读全文