通過(guò) Python 循環(huán)與隨機(jī)實(shí)現(xiàn)智能推薦系統(tǒng):五個(gè)實(shí)戰(zhàn)案例
推薦系統(tǒng)是現(xiàn)代互聯(lián)網(wǎng)應(yīng)用中不可或缺的一部分,它能根據(jù)用戶的行為和偏好,智能地為用戶推薦他們可能感興趣的內(nèi)容或商品。今天,我們就來(lái)探索如何使用Python中的循環(huán)和隨機(jī)模塊來(lái)實(shí)現(xiàn)簡(jiǎn)單的智能推薦系統(tǒng)。通過(guò)五個(gè)實(shí)戰(zhàn)案例,我們將逐步深入理解這些技術(shù)的應(yīng)用。
案例一:基于用戶歷史行為的簡(jiǎn)單推薦
假設(shè)我們有一個(gè)用戶的歷史購(gòu)買記錄列表,我們可以通過(guò)這個(gè)列表來(lái)推薦相似的商品給用戶。
# 用戶歷史購(gòu)買記錄
user_history = ['book', 'pen', 'notebook']
# 所有商品列表
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']
# 推薦系統(tǒng)
def simple_recommendation(user_history, all_products):
# 找出用戶未購(gòu)買過(guò)的商品
recommended_products = [product for product in all_products if product not in user_history]
return recommended_products
# 調(diào)用推薦系統(tǒng)
recommendations = simple_recommendation(user_history, all_products)
print("推薦的商品:", recommendations)
輸出結(jié)果:
推薦的商品: ['pencil', 'eraser', 'ruler']
案例二:基于隨機(jī)選擇的推薦
有時(shí)候,我們可以隨機(jī)選擇一些商品來(lái)推薦給用戶,增加用戶的探索體驗(yàn)。
import random
# 用戶歷史購(gòu)買記錄
user_history = ['book', 'pen', 'notebook']
# 所有商品列表
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']
# 推薦系統(tǒng)
def random_recommendation(user_history, all_products, num_recommendations=3):
# 找出用戶未購(gòu)買過(guò)的商品
available_products = [product for product in all_products if product not in user_history]
# 隨機(jī)選擇指定數(shù)量的商品
recommended_products = random.sample(available_products, min(num_recommendations, len(available_products)))
return recommended_products
# 調(diào)用推薦系統(tǒng)
recommendations = random_recommendation(user_history, all_products, 2)
print("隨機(jī)推薦的商品:", recommendations)
輸出結(jié)果:
隨機(jī)推薦的商品: ['pencil', 'ruler']
案例三:基于評(píng)分的推薦
假設(shè)我們有一個(gè)用戶對(duì)商品的評(píng)分?jǐn)?shù)據(jù),我們可以根據(jù)評(píng)分來(lái)推薦高分商品。
# 用戶對(duì)商品的評(píng)分
user_ratings = {'book': 4, 'pen': 3, 'notebook': 5, 'pencil': 2, 'eraser': 4, 'ruler': 3}
# 推薦系統(tǒng)
def rating_based_recommendation(user_ratings, num_recommendations=3):
# 按評(píng)分降序排序
sorted_ratings = sorted(user_ratings.items(), key=lambda x: x[1], reverse=True)
# 取前N個(gè)高分商品
recommended_products = [product for product, rating in sorted_ratings[:num_recommendations]]
return recommended_products
# 調(diào)用推薦系統(tǒng)
recommendations = rating_based_recommendation(user_ratings, 3)
print("基于評(píng)分的推薦商品:", recommendations)
輸出結(jié)果:
基于評(píng)分的推薦商品: ['notebook', 'book', 'eraser']
案例四:基于用戶興趣標(biāo)簽的推薦
假設(shè)我們有用戶感興趣的標(biāo)簽,可以推薦與這些標(biāo)簽相關(guān)聯(lián)的商品。
# 用戶感興趣的標(biāo)簽
user_interests = ['writing', 'stationery']
# 商品及其對(duì)應(yīng)的標(biāo)簽
product_tags = {
'book': ['reading'],
'pen': ['writing'],
'notebook': ['writing'],
'pencil': ['writing'],
'eraser': ['stationery'],
'ruler': ['stationery']
}
# 推薦系統(tǒng)
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
# 調(diào)用推薦系統(tǒng)
recommendations = interest_based_recommendation(user_interests, product_tags)
print("基于興趣的推薦商品:", recommendations)
輸出結(jié)果:
基于興趣的推薦商品: ['pen', 'notebook', 'pencil', 'eraser', 'ruler']
案例五:綜合推薦系統(tǒng)
結(jié)合以上多種推薦方式,我們可以構(gòu)建一個(gè)更加智能的推薦系統(tǒng)。
# 用戶歷史購(gòu)買記錄
user_history = ['book', 'pen', 'notebook']
# 所有商品列表
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']
# 用戶對(duì)商品的評(píng)分
user_ratings = {'book': 4, 'pen': 3, 'notebook': 5, 'pencil': 2, 'eraser': 4, 'ruler': 3}
# 用戶感興趣的標(biāo)簽
user_interests = ['writing', 'stationery']
# 商品及其對(duì)應(yīng)的標(biāo)簽
product_tags = {
'book': ['reading'],
'pen': ['writing'],
'notebook': ['writing'],
'pencil': ['writing'],
'eraser': ['stationery'],
'ruler': ['stationery']
}
# 綜合推薦系統(tǒng)
def combined_recommendation(user_history, all_products, user_ratings, user_interests, product_tags, num_recommendations=3):
# 基于歷史購(gòu)買記錄的推薦
history_recommendations = [product for product in all_products if product not in user_history]
# 基于評(píng)分的推薦
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))
# 隨機(jī)選擇指定數(shù)量的商品
final_recommendations = random.sample(all_recommendations, min(num_recommendations, len(all_recommendations)))
return final_recommendations
# 調(diào)用綜合推薦系統(tǒng)
recommendations = combined_recommendation(user_history, all_products, user_ratings, user_interests, product_tags, 3)
print("綜合推薦的商品:", recommendations)
輸出結(jié)果:
綜合推薦的商品: ['pencil', 'eraser', 'ruler']
實(shí)戰(zhàn)案例:在線書店推薦系統(tǒng)
假設(shè)我們有一個(gè)在線書店,用戶可以瀏覽書籍、購(gòu)買書籍并給出評(píng)分。我們需要構(gòu)建一個(gè)推薦系統(tǒng),根據(jù)用戶的購(gòu)買歷史、評(píng)分和興趣標(biāo)簽來(lái)推薦書籍。
# 用戶歷史購(gòu)買記錄
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']
# 用戶對(duì)書籍的評(píng)分
user_ratings = {
'The Great Gatsby': 4,
'To Kill a Mockingbird': 3,
'1984': 5,
'Pride and Prejudice': 2,
'Moby Dick': 4,
'War and Peace': 3
}
# 用戶感興趣的標(biāo)簽
user_interests = ['classic', 'literature']
# 書籍及其對(duì)應(yīng)的標(biāo)簽
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']
}
# 綜合推薦系統(tǒng)
def combined_recommendation(user_history, all_books, user_ratings, user_interests, book_tags, num_recommendations=3):
# 基于歷史購(gòu)買記錄的推薦
history_recommendations = [book for book in all_books if book not in user_history]
# 基于評(píng)分的推薦
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))
# 隨機(jī)選擇指定數(shù)量的商品
final_recommendations = random.sample(all_recommendations, min(num_recommendations, len(all_recommendations)))
return final_recommendations
# 調(diào)用綜合推薦系統(tǒng)
recommendations = combined_recommendation(user_history, all_books, user_ratings, user_interests, book_tags, 3)
print("綜合推薦的書籍:", recommendations)
輸出結(jié)果:
綜合推薦的書籍: ['Pride and Prejudice', 'Moby Dick', 'War and Peace']
總結(jié)
通過(guò)以上五個(gè)實(shí)戰(zhàn)案例,我們學(xué)習(xí)了如何使用Python中的循環(huán)和隨機(jī)模塊來(lái)實(shí)現(xiàn)簡(jiǎn)單的智能推薦系統(tǒng)。從基于用戶歷史行為的推薦到基于評(píng)分、興趣標(biāo)簽的推薦,再到綜合推薦系統(tǒng),我們逐步深入理解了這些技術(shù)的應(yīng)用。