Querying the recommendations
In the previous exercises, you've learned how to calculate a table with course recommendations on a daily basis. Now that this recommendations
table is in the data warehouse, you could also quickly join it with other tables in order to produce important features for DataCamp students such as customized marketing emails, intelligent recommendations for students and other features.
In this exercise, you will get a taste of how the newly created recommendations
table could be utilized by creating a function recommendations_for_user()
which automatically gets the top recommended courses based per user ID for a particular rating threshold.
This exercise is part of the course
Introduction to Data Engineering
Exercise instructions
- Complete the query in the
recommendations_for_user()
function definition. It should join thecourses
table. - Complete the
read_sql()
function inrecommendations_for_user()
. Theparams
argument is incomplete: it's missing a threshold. - Run the
recommendations_for_user()
function you defined in the last statements and observe the results.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
def recommendations_for_user(user_id, threshold=4.5):
# Join with the courses table
query = """
SELECT title, rating FROM recommendations
INNER JOIN ____ ON courses.course_id = recommendations.course_id
WHERE user_id=%(user_id)s AND rating>%(threshold)s
ORDER BY rating DESC
"""
# Add the threshold parameter
predictions_df = pd.read_sql(query, db_engine, params = {"user_id": user_id,
"____": ____})
return predictions_df.title.values
# Try the function you created
print(recommendations_for_user(12, 4.65))