Implementing Intelligent Recommendation Systems Using Python Loops and Random Module: Five Practical Cases

Time: Column:Python views:227

Today, we will explore how to use Python's loops and random module to implement a simple intelligent recommendation system. Through five practical cases, we will gradually deepen our understanding of how these techniques are applied.

Recommendation systems are an indispensable part of modern internet applications. They intelligently recommend content or products that users might be interested in, based on their behavior and preferences. Today, we will explore how to use Python's loops and random module to implement a simple intelligent recommendation system. Through five practical cases, we will gradually deepen our understanding of how these techniques are applied.

Case 1: Simple Recommendation Based on User's Historical Behavior

Suppose we have a list of a user's historical purchase records, and we can use this list to recommend similar products to the user.

# User's historical purchase records
user_history = ['book', 'pen', 'notebook']

# List of all products
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']

# Recommendation system
def simple_recommendation(user_history, all_products):
    # Find products that the user has not bought
    recommended_products = [product for product in all_products if product not in user_history]
    return recommended_products

# Call the recommendation system
recommendations = simple_recommendation(user_history, all_products)
print("Recommended products:", recommendations)

Output:

Recommended products: ['pencil', 'eraser', 'ruler']

Case 2: Recommendation Based on Random Selection

Sometimes, we can randomly select some products to recommend to the user, enhancing the user's exploration experience.

import random

# User's historical purchase records
user_history = ['book', 'pen', 'notebook']

# List of all products
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']

# Recommendation system
def random_recommendation(user_history, all_products, num_recommendations=3):
    # Find products that the user has not bought
    available_products = [product for product in all_products if product not in user_history]
    # Randomly select a specified number of products
    recommended_products = random.sample(available_products, min(num_recommendations, len(available_products)))
    return recommended_products

# Call the recommendation system
recommendations = random_recommendation(user_history, all_products, 2)
print("Randomly recommended products:", recommendations)

Output:

Randomly recommended products: ['pencil', 'ruler']

Case 3: Recommendation Based on Ratings

Suppose we have rating data for products, and we can recommend products with high ratings.

# User's ratings for products
user_ratings = {'book': 4, 'pen': 3, 'notebook': 5, 'pencil': 2, 'eraser': 4, 'ruler': 3}

# Recommendation system
def rating_based_recommendation(user_ratings, num_recommendations=3):
    # Sort ratings in descending order
    sorted_ratings = sorted(user_ratings.items(), key=lambda x: x[1], reverse=True)
    # Take the top N rated products
    recommended_products = [product for product, rating in sorted_ratings[:num_recommendations]]
    return recommended_products

# Call the recommendation system
recommendations = rating_based_recommendation(user_ratings, 3)
print("Recommendation based on ratings:", recommendations)

Output:

Recommendation based on ratings: ['notebook', 'book', 'eraser']

Case 4: Recommendation Based on User Interest Tags

Suppose we have tags for products that match user interests, and we can recommend products related to these tags.

# User's interested tags
user_interests = ['writing', 'stationery']

# Products and their corresponding tags
product_tags = {
    'book': ['reading'],
    'pen': ['writing'],
    'notebook': ['writing'],
    'pencil': ['writing'],
    'eraser': ['stationery'],
    'ruler': ['stationery']
}

# Recommendation system
def interest_based_recommendation(user_interests, product_tags):
    # Find products matching the user's interests
    recommended_products = [product for product, tags in product_tags.items() if any(interest in tags for interest in user_interests)]
    return recommended_products

# Call the recommendation system
recommendations = interest_based_recommendation(user_interests, product_tags)
print("Recommendation based on interests:", recommendations)

Output:

Recommendation based on interests: ['pen', 'notebook', 'pencil', 'eraser', 'ruler']

Case 5: Comprehensive Recommendation System

By combining the various recommendation methods above, we can build a more intelligent recommendation system.

# User's historical purchase records
user_history = ['book', 'pen', 'notebook']

# List of all products
all_products = ['book', 'pen', 'notebook', 'pencil', 'eraser', 'ruler']

# User's ratings for products
user_ratings = {'book': 4, 'pen': 3, 'notebook': 5, 'pencil': 2, 'eraser': 4, 'ruler': 3}

# User's interested tags
user_interests = ['writing', 'stationery']

# Products and their corresponding tags
product_tags = {
    'book': ['reading'],
    'pen': ['writing'],
    'notebook': ['writing'],
    'pencil': ['writing'],
    'eraser': ['stationery'],
    'ruler': ['stationery']
}

# Comprehensive recommendation system
def combined_recommendation(user_history, all_products, user_ratings, user_interests, product_tags, num_recommendations=3):
    # Recommendation based on historical purchase records
    history_recommendations = [product for product in all_products if product not in user_history]
    
    # Recommendation based on ratings
    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]
    
    # Recommendation based on interests
    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]
    
    # Combine all recommendation lists
    all_recommendations = list(set(history_recommendations + rating_recommendations + interest_recommendations))
    
    # Randomly select the specified number of products
    final_recommendations = random.sample(all_recommendations, min(num_recommendations, len(all_recommendations)))
    return final_recommendations

# Call the comprehensive recommendation system
recommendations = combined_recommendation(user_history, all_products, user_ratings, user_interests, product_tags, 3)
print("Comprehensive recommended products:", recommendations)

Output:

Comprehensive recommended products: ['pencil', 'eraser', 'ruler']

Practical Case: Online Bookstore Recommendation System

Suppose we have an online bookstore where users can browse books, purchase them, and provide ratings. We need to build a recommendation system that suggests books based on the user’s purchase history, ratings, and interest tags.

# User's historical purchase records
user_history = ['The Great Gatsby', 'To Kill a Mockingbird', '1984']

# List of all books
all_books = ['The Great Gatsby', 'To Kill a Mockingbird', '1984', 'Pride and Prejudice', 'Moby Dick', 'War and Peace']

# User's ratings for books
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's interested tags
user_interests = ['classic', 'literature']

# Books and their corresponding tags
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']
}

# Comprehensive recommendation system
def combined_recommendation(user_history, all_books, user_ratings, user_interests, book_tags, num_recommendations=3):
    # Recommendations based on historical purchase records
    history_recommendations = [book for book in all_books if book not in user_history]
    
    # Recommendations based on ratings
    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]
    
    # Recommendations based on interests
    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]
    
    # Combine all recommendation lists
    all_recommendations = list(set(history_recommendations + rating_recommendations + interest_recommendations))
    
    # Randomly select the specified number of items
    final_recommendations = random.sample(all_recommendations, min(num_recommendations, len(all_recommendations)))
    return final_recommendations

# Call the comprehensive recommendation system
recommendations = combined_recommendation(user_history, all_books, user_ratings, user_interests, book_tags, 3)
print("Comprehensive recommended books:", recommendations)

Output:

Comprehensive recommended books: ['Pride and Prejudice', 'Moby Dick', 'War and Peace']

Summary

Through the five practical cases above, we learned how to use Python's loops and random module to implement simple intelligent recommendation systems. From recommendations based on user historical behavior to those based on ratings and interest tags, and finally to a comprehensive recommendation system, we have gradually deepened our understanding of how these techniques are applied.