推荐系统是现代互联网应用中不可或缺的一部分,它能根据用户的行为和偏好,智能地为用户推荐他们可能感兴趣的内容或商品。今天,我们就来探索如何使用Python中的循环和随机模块来实现简单的智能推荐系统。通过五个实战案例,我们将逐步深入理解这些技术的应用。
案例一:基于用户历史行为的简单推荐
假设我们有一个用户的历史购买记录列表,我们可以通过这个列表来推荐相似的商品给用户。
# 用户历史购买记录
user_history = ['book', 'pen', 'notebook']
# 所有商品列表
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']
# 推荐系统
def simple_recommendation(user_history, all_products):
# 找出用户未购买过的商品
recommended_products = [product for product in all_products if product not in user_history]
return recommended_products
# 调用推荐系统
recommendations = simple_recommendation(user_history, all_products)
print("推荐的商品:", recommendations)
输出结果:
推荐的商品: ['pencil', 'eraser', 'ruler']
案例二:基于随机选择的推荐
有时候,我们可以随机选择一些商品来推荐给用户,增加用户的探索体验。
import random
# 用户历史购买记录
user_history = ['book', 'pen', 'notebook']
# 所有商品列表
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']
# 推荐系统
def random_recommendation(user_history, all_products, num_recommendations=3):
# 找出用户未购买过的商品
available_products = [product for product in all_products if product not in user_history]
# 随机选择指定数量的商品
recommended_products = random.sample(available_products, min(num_recommendations, len(available_products)))
return recommended_products
# 调用推荐系统
recommendations = random_recommendation(user_history, all_products, 2)
print("随机推荐的商品:", recommendations)
输出结果:
随机推荐的商品: ['pencil', 'ruler']
案例三:基于评分的推荐
假设我们有一个用户对商品的评分数据,我们可以根据评分来推荐高分商品。
# 用户对商品的评分
user_ratings = {'book': 4, 'pen': 3, 'notebook': 5, 'pencil': 2, 'eraser': 4, 'ruler': 3}
# 推荐系统
def rating_based_recommendation(user_ratings, num_recommendations=3):
# 按评分降序排序
sorted_ratings = sorted(user_ratings.items(), key=lambda x: x[1], reverse=True)
# 取前N个高分商品
recommended_products = [product for product, rating in sorted_ratings[:num_recommendations]]
return recommended_products
# 调用推荐系统
recommendations = rating_based_recommendation(user_ratings, 3)
print("基于评分的推荐商品:", recommendations)
输出结果:
基于评分的推荐商品: ['notebook', 'book', 'eraser']
案例四:基于用户兴趣标签的推荐
假设我们有用户感兴趣的标签,可以推荐与这些标签相关联的商品。
# 用户感兴趣的标签
user_interests = ['writing', 'stationery']
# 商品及其对应的标签
product_tags = {
'book': ['reading'],
'pen': ['writing'],
'notebook': ['writing'],
'pencil': ['writing'],
'eraser': ['stationery'],
'ruler': ['stationery']
}
# 推荐系统
def interest_based_recommendation(user_interests, product_tags):
# 找出与用户兴趣匹配的商品
recommended_products = [product for product, tags in product_tags.items() if any(interest in tags for interest in user_interests)]
return recommended_products
# 调用推荐系统
recommendations = interest_based_recommendation(user_interests, product_tags)
print("基于兴趣的推荐商品:", recommendations)
输出结果:
基于兴趣的推荐商品: ['pen', 'notebook', 'pencil', 'eraser', 'ruler']
案例五:综合推荐系统
结合以上多种推荐方式,我们可以构建一个更加智能的推荐系统。
# 用户历史购买记录
user_history = ['book', 'pen', 'notebook']
# 所有商品列表
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']
# 用户对商品的评分
user_ratings = {'book': 4, 'pen': 3, 'notebook': 5, 'pencil': 2, 'eraser': 4, 'ruler': 3}
# 用户感兴趣的标签
user_interests = ['writing', 'stationery']
# 商品及其对应的标签
product_tags = {
'book': ['reading'],
'pen': ['writing'],
'notebook': ['writing'],
'pencil': ['writing'],
'eraser': ['stationery'],
'ruler': ['stationery']
}
# 综合推荐系统
def combined_recommendation(user_history, all_products, user_ratings, user_interests, product_tags, num_recommendations=3):
# 基于历史购买记录的推荐
history_recommendations = [product for product in all_products if product not in user_history]
# 基于评分的推荐
sorted_ratings = sorted(user_ratings.items(), key=lambda x: x[1], reverse=True)
rating_recommendations = [product for product, rating in sorted_ratings if product not in user_history]
# 基于兴趣的推荐
interest_recommendations = [product for product, tags in product_tags.items() if any(interest in tags for interest in user_interests) and product not in user_history]
# 合并所有推荐列表
all_recommendations = list(set(history_recommendations + rating_recommendations + interest_recommendations))
# 随机选择指定数量的商品
final_recommendations = random.sample(all_recommendations, min(num_recommendations, len(all_recommendations)))
return final_recommendations
# 调用综合推荐系统
recommendations = combined_recommendation(user_history, all_products, user_ratings, user_interests, product_tags, 3)
print("综合推荐的商品:", recommendations)
输出结果:
综合推荐的商品: ['pencil', 'eraser', 'ruler']
实战案例:在线书店推荐系统
假设我们有一个在线书店,用户可以浏览书籍、购买书籍并给出评分。我们需要构建一个推荐系统,根据用户的购买历史、评分和兴趣标签来推荐书籍。
# 用户历史购买记录
user_history = ['The Great Gatsby', 'To Kill a Mockingbird', '1984']
# 所有书籍列表
all_books = ['The Great Gatsby', 'To Kill a Mockingbird', '1984', 'Pride and Prejudice', 'Moby Dick', 'War and Peace']
# 用户对书籍的评分
user_ratings = {
'The Great Gatsby': 4,
'To Kill a Mockingbird': 3,
'1984': 5,
'Pride and Prejudice': 2,
'Moby Dick': 4,
'War and Peace': 3
}
# 用户感兴趣的标签
user_interests = ['classic', 'literature']
# 书籍及其对应的标签
book_tags = {
'The Great Gatsby': ['classic', 'novel'],
'To Kill a Mockingbird': ['classic', 'novel'],
'1984': ['classic', 'dystopian'],
'Pride and Prejudice': ['classic', 'romance'],
'Moby Dick': ['classic', 'adventure'],
'War and Peace': ['classic', 'epic']
}
# 综合推荐系统
def combined_recommendation(user_history, all_books, user_ratings, user_interests, book_tags, num_recommendations=3):
# 基于历史购买记录的推荐
history_recommendations = [book for book in all_books if book not in user_history]
# 基于评分的推荐
sorted_ratings = sorted(user_ratings.items(), key=lambda x: x[1], reverse=True)
rating_recommendations = [book for book, rating in sorted_ratings if book not in user_history]
# 基于兴趣的推荐
interest_recommendations = [book for book, tags in book_tags.items() if any(interest in tags for interest in user_interests) and book not in user_history]
# 合并所有推荐列表
all_recommendations = list(set(history_recommendations + rating_recommendations + interest_recommendations))
# 随机选择指定数量的商品
final_recommendations = random.sample(all_recommendations, min(num_recommendations, len(all_recommendations)))
return final_recommendations
# 调用综合推荐系统
recommendations = combined_recommendation(user_history, all_books, user_ratings, user_interests, book_tags, 3)
print("综合推荐的书籍:", recommendations)
输出结果:
综合推荐的书籍: ['Pride and Prejudice', 'Moby Dick', 'War and Peace']
总结
通过以上五个实战案例,我们学习了如何使用Python中的循环和随机模块来实现简单的智能推荐系统。从基于用户历史行为的推荐到基于评分、兴趣标签的推荐,再到综合推荐系统,我们逐步深入理解了这些技术的应用。