Bird
Raised Fist0
ML Pythonml~10 mins

Why recommendations drive engagement in ML Python - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the library used for building recommendation models.

ML Python
import [1]
Drag options to blanks, or click blank then click option'
Anumpy
Bsklearn
Ctensorflow
Dpandas
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing sklearn which is mainly for classical ML, not deep learning.
Choosing numpy or pandas which are for data manipulation, not modeling.
2fill in blank
medium

Complete the code to create a simple user-item interaction matrix using pandas.

ML Python
import pandas as pd
interactions = pd.DataFrame({'user_id': [1, 2, 1], 'item_id': [101, 102, 103], 'rating': [5, 3, 4]})
matrix = interactions.pivot(index='user_id', columns=[1], values='rating')
Drag options to blanks, or click blank then click option'
A'item_id'
B'rating'
C'user_id'
D'interaction'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'user_id' as columns which would not create the correct matrix.
Using 'rating' which is the values, not columns.
3fill in blank
hard

Fix the error in the code that trains a simple recommendation model using TensorFlow.

ML Python
import tensorflow as tf
model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])
model.compile(optimizer='adam', loss=[1])
model.fit(x_train, y_train, epochs=5)
Drag options to blanks, or click blank then click option'
A'binary_crossentropy'
B'mse'
C'categorical_crossentropy'
D'accuracy'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'accuracy' which is for classification, not regression.
Using 'categorical_crossentropy' which is for multi-class classification.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps items to their average ratings above 3.

ML Python
average_ratings = {item: [1] for item, ratings in ratings_dict.items() if [2] > 3}
Drag options to blanks, or click blank then click option'
Asum(ratings)/len(ratings)
Bmax(ratings)
Csum(ratings)
Dlen(ratings)
Attempts:
3 left
💡 Hint
Common Mistakes
Using max or sum alone which do not give average.
Using length alone which is count, not average.
5fill in blank
hard

Fill all three blanks to create a dictionary of recommended items with predicted scores above 4.

ML Python
recommendations = [1] for item, score in predicted_scores.items() if score [2] 4}
sorted_recs = sorted(recommendations.items(), key=[3], reverse=True)
Drag options to blanks, or click blank then click option'
A{item: score
B>
Clambda x: x[1]
D[item, score
Attempts:
3 left
💡 Hint
Common Mistakes
Using list brackets instead of dictionary braces.
Using '<' instead of '>' causing wrong filtering.
Sorting by key instead of value.

Practice

(1/5)
1. Why do recommendation systems increase user engagement on websites or apps?
easy
A. They show users content or products that match their interests
B. They randomly display items without any user data
C. They reduce the number of items users can see
D. They force users to see ads only

Solution

  1. Step 1: Understand user engagement

    User engagement means users spend more time and interact more with the platform.
  2. Step 2: Role of recommendations

    Recommendations show users items they like, making them more interested and active.
  3. Final Answer:

    They show users content or products that match their interests -> Option A
  4. Quick Check:

    Relevant content = higher engagement [OK]
Hint: Recommendations match user interests to boost engagement [OK]
Common Mistakes:
  • Thinking recommendations show random items
  • Believing recommendations limit user choices
  • Confusing ads with recommendations
2. Which of the following is the correct way to describe a recommendation system in simple terms?
easy
A. A system that shows the same items to all users
B. A system that guesses what users might like based on their past behavior
C. A system that deletes user data after each session
D. A system that only shows the newest items regardless of user interest

Solution

  1. Step 1: Define recommendation system

    It uses past user data to predict what they might like next.
  2. Step 2: Check options

    Only the system that guesses what users might like based on their past behavior describes this behavior correctly; others ignore personalization.
  3. Final Answer:

    A system that guesses what users might like based on their past behavior -> Option B
  4. Quick Check:

    Personalization = guessing user likes [OK]
Hint: Recommendations use past data to guess user preferences [OK]
Common Mistakes:
  • Thinking recommendations show same items to everyone
  • Confusing recommendation with data deletion
  • Ignoring user history in recommendations
3. Consider this Python code snippet for a simple recommendation score calculation:
user_preferences = {'sports': 5, 'music': 3, 'movies': 4}
item_features = {'sports': 1, 'music': 0, 'movies': 1}
score = sum(user_preferences[cat] * item_features[cat] for cat in item_features)
print(score)

What will be the output of this code?
medium
A. 8
B. 10
C. 7
D. 9

Solution

  1. Step 1: Calculate weighted scores

    Multiply user preference by item feature for each category: sports=5*1=5, music=3*0=0, movies=4*1=4.
  2. Step 2: Sum the results

    Sum = 5 + 0 + 4 = 9.
  3. Final Answer:

    9 -> Option D
  4. Quick Check:

    5+0+4 = 9 [OK]
Hint: Multiply and sum matching categories for score [OK]
Common Mistakes:
  • Adding all preference values without weights
  • Ignoring zero feature values
  • Multiplying wrong pairs of categories
4. The following code is intended to calculate a recommendation score but has a bug:
user_prefs = {'books': 4, 'games': 2}
item_feats = {'books': 1, 'games': 1}
score = 0
for cat in user_prefs:
    score += user_prefs[cat] * item_feats[cat]
print(score)

What is the error and how to fix it?
medium
A. KeyError if item_feats lacks a category; fix by iterating item_feats keys
B. SyntaxError due to missing colon; fix by adding colon after for loop
C. TypeError because score is int; fix by converting score to float
D. No error; code runs correctly

Solution

  1. Step 1: Identify potential KeyError

    The loop uses keys from user_prefs but accesses item_feats; if item_feats misses a key, error occurs.
  2. Step 2: Fix iteration to avoid missing keys

    Iterate over keys in item_feats or check key existence before access.
  3. Final Answer:

    KeyError if item_feats lacks a category; fix by iterating item_feats keys -> Option A
  4. Quick Check:

    Key mismatch causes error [OK]
Hint: Check keys exist before accessing dict values [OK]
Common Mistakes:
  • Assuming all keys match in both dicts
  • Ignoring possible KeyError exceptions
  • Confusing syntax errors with runtime errors
5. A streaming service wants to improve engagement by recommending movies. They have user watch history and movie genres. Which approach best uses machine learning to drive engagement?
hard
A. Display only movies with the highest global ratings ignoring user data
B. Recommend only the newest movies regardless of user preferences
C. Build a model that predicts user ratings for unseen movies based on past watched genres
D. Show random movies from the entire catalog to all users

Solution

  1. Step 1: Understand personalized recommendations

    Using user watch history and genres helps predict what each user likes.
  2. Step 2: Evaluate options

    Build a model that predicts user ratings for unseen movies based on past watched genres uses machine learning to personalize, increasing engagement; others ignore personalization.
  3. Final Answer:

    Build a model that predicts user ratings for unseen movies based on past watched genres -> Option C
  4. Quick Check:

    Personalized prediction = better engagement [OK]
Hint: Use user history to predict preferences for recommendations [OK]
Common Mistakes:
  • Ignoring user preferences in recommendations
  • Assuming newest or highest rated always engage users
  • Confusing random recommendations with personalized ones
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