自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

通過 Python 循環(huán)與隨機實現(xiàn)智能推薦系統(tǒng):五個實戰(zhàn)案例

開發(fā)
今天,我們就來探索如何使用Python中的循環(huán)和隨機模塊來實現(xiàn)簡單的智能推薦系統(tǒng)。通過五個實戰(zhàn)案例,我們將逐步深入理解這些技術的應用。

推薦系統(tǒng)是現(xiàn)代互聯(lián)網(wǎng)應用中不可或缺的一部分,它能根據(jù)用戶的行為和偏好,智能地為用戶推薦他們可能感興趣的內容或商品。今天,我們就來探索如何使用Python中的循環(huán)和隨機模塊來實現(xiàn)簡單的智能推薦系統(tǒng)。通過五個實戰(zhàn)案例,我們將逐步深入理解這些技術的應用。

案例一:基于用戶歷史行為的簡單推薦

假設我們有一個用戶的歷史購買記錄列表,我們可以通過這個列表來推薦相似的商品給用戶。

# 用戶歷史購買記錄
user_history = ['book', 'pen', 'notebook']

# 所有商品列表
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']

# 推薦系統(tǒng)
def simple_recommendation(user_history, all_products):
    # 找出用戶未購買過的商品
    recommended_products = [product for product in all_products if product not in user_history]
    return recommended_products

# 調用推薦系統(tǒng)
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']

# 推薦系統(tǒng)
def random_recommendation(user_history, all_products, num_recommendations=3):
    # 找出用戶未購買過的商品
    available_products = [product for product in all_products if product not in user_history]
    # 隨機選擇指定數(shù)量的商品
    recommended_products = random.sample(available_products, min(num_recommendations, len(available_products)))
    return recommended_products

# 調用推薦系統(tǒng)
recommendations = random_recommendation(user_history, all_products, 2)
print("隨機推薦的商品:", recommendations)

輸出結果:

隨機推薦的商品: ['pencil', 'ruler']

案例三:基于評分的推薦

假設我們有一個用戶對商品的評分數(shù)據(jù),我們可以根據(jù)評分來推薦高分商品。

# 用戶對商品的評分
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):
    # 按評分降序排序
    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

# 調用推薦系統(tǒng)
recommendations = rating_based_recommendation(user_ratings, 3)
print("基于評分的推薦商品:", recommendations)

輸出結果:

基于評分的推薦商品: ['notebook', 'book', 'eraser']

案例四:基于用戶興趣標簽的推薦

假設我們有用戶感興趣的標簽,可以推薦與這些標簽相關聯(lián)的商品。

# 用戶感興趣的標簽
user_interests = ['writing', 'stationery']

# 商品及其對應的標簽
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

# 調用推薦系統(tǒng)
recommendations = interest_based_recommendation(user_interests, product_tags)
print("基于興趣的推薦商品:", recommendations)

輸出結果:

基于興趣的推薦商品: ['pen', 'notebook', 'pencil', 'eraser', 'ruler']

案例五:綜合推薦系統(tǒng)

結合以上多種推薦方式,我們可以構建一個更加智能的推薦系統(tǒng)。

# 用戶歷史購買記錄
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']
}

# 綜合推薦系統(tǒng)
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))
    
    # 隨機選擇指定數(shù)量的商品
    final_recommendations = random.sample(all_recommendations, min(num_recommendations, len(all_recommendations)))
    return final_recommendations

# 調用綜合推薦系統(tǒng)
recommendations = combined_recommendation(user_history, all_products, user_ratings, user_interests, product_tags, 3)
print("綜合推薦的商品:", recommendations)

輸出結果:

綜合推薦的商品: ['pencil', 'eraser', 'ruler']

實戰(zhàn)案例:在線書店推薦系統(tǒng)

假設我們有一個在線書店,用戶可以瀏覽書籍、購買書籍并給出評分。我們需要構建一個推薦系統(tǒng),根據(jù)用戶的購買歷史、評分和興趣標簽來推薦書籍。

# 用戶歷史購買記錄
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']
}

# 綜合推薦系統(tǒng)
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))
    
    # 隨機選擇指定數(shù)量的商品
    final_recommendations = random.sample(all_recommendations, min(num_recommendations, len(all_recommendations)))
    return final_recommendations

# 調用綜合推薦系統(tǒng)
recommendations = combined_recommendation(user_history, all_books, user_ratings, user_interests, book_tags, 3)
print("綜合推薦的書籍:", recommendations)

輸出結果:

綜合推薦的書籍: ['Pride and Prejudice', 'Moby Dick', 'War and Peace']

總結

通過以上五個實戰(zhàn)案例,我們學習了如何使用Python中的循環(huán)和隨機模塊來實現(xiàn)簡單的智能推薦系統(tǒng)。從基于用戶歷史行為的推薦到基于評分、興趣標簽的推薦,再到綜合推薦系統(tǒng),我們逐步深入理解了這些技術的應用。

責任編輯:趙寧寧 來源: 小白PythonAI編程
相關推薦

2024-06-19 10:08:42

Python編程while循環(huán)

2024-11-11 16:55:54

2016-01-06 10:10:25

2024-12-19 16:00:00

Pythonwhile 循環(huán)

2011-11-28 10:06:27

編程字體

2023-04-26 06:22:45

NLPPython知識圖譜

2024-11-12 16:28:57

Python項目管理

2023-10-31 16:46:45

2021-11-28 18:07:44

PythonRuby編程

2010-09-26 15:28:45

2009-08-24 10:35:30

2024-09-06 17:57:35

2020-08-21 10:35:17

機器學習IT領導者人工智能

2024-11-19 15:22:37

2024-12-30 07:40:58

Python編程循環(huán)結構

2024-11-25 07:00:00

2010-12-14 11:20:49

MySQL GUI工具

2023-08-04 06:59:48

2025-02-28 08:40:28

ZooKeeperSpringBoot計費系統(tǒng)

2016-01-08 13:35:34

開源CRM用戶關系管理
點贊
收藏

51CTO技術棧公眾號