Bird
Raised Fist0
ML Pythonml~20 mins

Content-based filtering in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Content-based Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
๐Ÿง  Conceptual
intermediate
2:00remaining
How does content-based filtering recommend items?
Which of the following best describes how content-based filtering recommends items to a user?
AIt recommends items similar to those the user liked before based on item features.
BIt recommends items liked by other users with similar tastes.
CIt randomly recommends popular items regardless of user preferences.
DIt recommends items based on the time of day and user location.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how the system uses the user's past preferences and item details.
โ“ Predict Output
intermediate
2:00remaining
Output of cosine similarity calculation
What is the output of this Python code that calculates cosine similarity between two item feature vectors?
ML Python
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

item1 = np.array([[1, 0, 1, 0]])
item2 = np.array([[0, 1, 1, 0]])
similarity = cosine_similarity(item1, item2)
print(round(similarity[0][0], 2))
A0.33
B0.50
C0.67
D0.75
Attempts:
2 left
๐Ÿ’ก Hint
Recall cosine similarity formula and count overlapping features.
โ“ Model Choice
advanced
2:00remaining
Best model for content-based filtering with sparse user-item data
Which model is most suitable for content-based filtering when user-item interaction data is sparse but item features are rich?
ACollaborative filtering using user similarity
BMatrix factorization using only user-item ratings
CK-Nearest Neighbors using item feature vectors
DRandom forest classifier on user demographic data
Attempts:
2 left
๐Ÿ’ก Hint
Focus on models that use item features directly.
โ“ Hyperparameter
advanced
2:00remaining
Choosing the number of neighbors in content-based KNN
In a content-based filtering system using K-Nearest Neighbors on item features, what is the effect of increasing the number of neighbors (k)?
AIt increases recommendation diversity but may reduce relevance.
BIt always improves recommendation accuracy.
CIt decreases the number of recommended items.
DIt causes the model to ignore item features.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how more neighbors affect similarity and variety.
๐Ÿ”ง Debug
expert
3:00remaining
Why does this content-based filtering code produce identical recommendations for all users?
Given this simplified content-based filtering code, why do all users get the same recommended items? ```python import numpy as np user_profiles = { 'user1': np.array([1, 0, 1]), 'user2': np.array([1, 0, 1]), 'user3': np.array([1, 0, 1]) } item_features = { 'itemA': np.array([1, 0, 0]), 'itemB': np.array([0, 1, 1]), 'itemC': np.array([1, 0, 1]) } recommendations = {} for user, profile in user_profiles.items(): scores = {} for item, features in item_features.items(): scores[item] = np.dot(profile, features) recommended = sorted(scores, key=scores.get, reverse=True)[:2] recommendations[user] = recommended print(recommendations) ```
AThe code uses Euclidean distance instead of dot product.
BThe item features are normalized incorrectly causing identical scores.
CThe sorting function is not stable, causing random recommendations.
DAll user profiles are identical, so dot products and recommendations are the same.
Attempts:
2 left
๐Ÿ’ก Hint
Check the user profiles carefully.

Practice

(1/5)
1. What is the main idea behind content-based filtering in recommendation systems?
easy
A. It recommends items based on what other users liked.
B. It recommends items similar to what the user liked before based on item features.
C. It randomly suggests items without any user data.
D. It recommends items based on the most popular items overall.

Solution

  1. Step 1: Understand content-based filtering concept

    Content-based filtering uses the features of items a user liked to find similar items.
  2. Step 2: Compare with other recommendation methods

    Unlike collaborative filtering, it does not rely on other users' preferences but on item features.
  3. Final Answer:

    It recommends items similar to what the user liked before based on item features. -> Option B
  4. Quick Check:

    Content-based filtering = Recommend similar items [OK]
Hint: Focus on item features and user history for content-based filtering [OK]
Common Mistakes:
  • Confusing content-based with collaborative filtering
  • Thinking recommendations are random
  • Assuming popularity drives recommendations
2. Which of the following is the correct way to represent item features for content-based filtering in Python?
easy
A. item_features = [1, 2, 3, 4]
B. item_features = ['item1', 'item2', 'item3']
C. item_features = {'item1': 'popular', 'item2': 'new'}
D. item_features = {'item1': [0.2, 0.8], 'item2': [0.5, 0.5]}

Solution

  1. Step 1: Identify correct data structure for features

    Features should be numeric vectors representing item properties, stored in a dictionary keyed by item IDs.
  2. Step 2: Check each option

    item_features = {'item1': [0.2, 0.8], 'item2': [0.5, 0.5]} uses a dictionary with lists of numbers, suitable for feature vectors. Others are lists or strings, not suitable for numeric features.
  3. Final Answer:

    item_features = {'item1': [0.2, 0.8], 'item2': [0.5, 0.5]} -> Option D
  4. Quick Check:

    Features as numeric vectors = item_features = {'item1': [0.2, 0.8], 'item2': [0.5, 0.5]} [OK]
Hint: Use dictionaries with numeric lists for item features [OK]
Common Mistakes:
  • Using strings instead of numeric vectors
  • Using simple lists without keys
  • Confusing item labels with features
3. Given the following Python code for content-based filtering, what will be the output?
import numpy as np

item_features = {'A': np.array([1, 0]), 'B': np.array([0, 1]), 'C': np.array([1, 1])}
user_likes = np.array([1, 0])
similarities = {item: np.dot(user_likes, features) for item, features in item_features.items()}
recommended = max(similarities, key=similarities.get)
print(recommended)
medium
A. B
B. C
C. A
D. Error

Solution

  1. Step 1: Calculate dot products for similarity

    Dot product with user_likes [1,0]: - A: [1,0]ยท[1,0] = 1 - B: [1,0]ยท[0,1] = 0 - C: [1,0]ยท[1,1] = 1
  2. Step 2: Find item with max similarity

    Items A and C both have similarity 1, but max() returns the first max key, which is 'A'.
  3. Final Answer:

    A -> Option C
  4. Quick Check:

    Max dot product = 'A' [OK]
Hint: Dot product similarity picks highest matching feature vector [OK]
Common Mistakes:
  • Assuming 'C' is chosen because it has more features
  • Confusing dot product with sum
  • Expecting an error due to dictionary comprehension
4. Identify the error in this content-based filtering code snippet and select the fix:
item_features = {'X': [1, 0], 'Y': [0, 1]}
user_profile = [1, 0]
similarities = {}
for item in item_features:
    similarities[item] = user_profile * item_features[item]
print(similarities)
medium
A. Replace '*' with a dot product function like numpy.dot
B. Change user_profile and item_features values to strings
C. Use '+' instead of '*' to combine lists
D. No error, code runs fine

Solution

  1. Step 1: Understand the operation on lists

    Multiplying two Python lists with '*' is invalid and causes a TypeError.
  2. Step 2: Correct the operation to compute similarity

    Use numpy.dot or equivalent to compute dot product between vectors for similarity.
  3. Final Answer:

    Replace '*' with a dot product function like numpy.dot -> Option A
  4. Quick Check:

    List multiplication invalid, use dot product [OK]
Hint: Use dot product for vector similarity, not '*' on lists [OK]
Common Mistakes:
  • Trying to multiply lists directly
  • Using '+' which concatenates lists, not similarity
  • Ignoring error messages
5. You want to recommend movies to a user using content-based filtering. The user liked movies with genres ['Action', 'Adventure']. You have a movie database with genre tags. Which approach best personalizes recommendations?
hard
A. Calculate similarity by counting overlapping genres between user liked movies and candidate movies.
B. Recommend the most popular movies regardless of genre.
C. Recommend movies liked by other users who watched 'Action' movies.
D. Randomly select movies from the database.

Solution

  1. Step 1: Understand content-based filtering with genre tags

    Content-based filtering uses item features (genres) to find similar items to what the user liked.
  2. Step 2: Choose method that compares genre overlap

    Counting overlapping genres directly measures similarity between user preferences and candidate movies.
  3. Final Answer:

    Calculate similarity by counting overlapping genres between user liked movies and candidate movies. -> Option A
  4. Quick Check:

    Genre overlap similarity = Calculate similarity by counting overlapping genres between user liked movies and candidate movies. [OK]
Hint: Match user liked genres with movie genres for recommendations [OK]
Common Mistakes:
  • Using popularity instead of user preferences
  • Confusing collaborative filtering with content-based
  • Random recommendations ignoring user data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard