Tutorialspoint
Problem
Solution
Submissions

Recommendation System

Certification: Advanced Level Accuracy: 50% Submissions: 2 Points: 20

Implement a content-based recommendation system in Python that suggests items to users based on item features and user preferences. The system should use cosine similarity as the primary metric for comparing features.

Example 1
  • Input:
    items = {"item1": {...}, "item2": {...}, ...}
    user_ratings = {"user1": {"item1": 5, "item3": 2}, ...}
  • Output:
    [("item2", 0.78), ("item4", 0.65)]
  • Explanation:
    • Analyze user1’s high rating for item1.
    • Compare item features with cosine similarity.
    • Recommend items most similar to preferred ones.
Example 2
  • Input:
    items = {"book1": {...}, "book2": {...}, ...}
    user_ratings = {"alice": {"book1": 5, "book4": 4}, ...}
  • Output:
    [("book2", 0.62), ("book5", 0.45), ("book3", 0.21)]
  • Explanation:
    • Analyze Alice's liking for fiction/fantasy.
    • Recommend based on cosine similarity.
    • Items are ranked by similarity score.
Constraints
  • 1 ≤ number of items ≤ 10^5
  • 1 ≤ number of users ≤ 10^4
  • 1 ≤ features per item ≤ 20
  • Time Complexity: O(n * m)
  • Space Complexity: O(n * m + u * r)
Object-Oriented ProgrammingAmazonGoogle
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Implement a feature vectorization mechanism to convert item features into numerical vectors
  • Use cosine similarity to compute similarity between items
  • Build user profiles based on their rating history
  • Implement a prediction function to estimate ratings for unseen items
  • Sort recommendations by predicted ratings or similarity scores

Steps to solve by this approach:

 Step 1: Create a class that maintains items, user ratings, and a set of all unique features

 Step 2: Implement feature vectorization by converting categorical and numerical features into binary vectors
 Step 3: Implement similarity computation using cosine similarity between vectors
 Step 4: Build user profiles by averaging the vectors of rated items, weighted by their ratings
 Step 5: Generate recommendations by computing similarity between user profiles and unrated items

Submitted Code :