DataCTW
Published in

DataCTW

Build a recommendation engine in 10 minutes using Recombee in Python

We’ll build a recommendation engine based on Coursera dataset to recommend courses to user by using a third party service called recombee.

Photo by Morning Brew on Unsplash

What is a recommendation engine?

Source: Human for AI

How does recombee works?

  • Detail Views (User has viewed details of an item)
  • Cart Addition (User has added an item in the cart)
  • Rating (User has rated an item)
  • Bookmarks (User has saved/bookmarked an item)
  • Purchases (User has purchased an item)

Getting Started with Recombee

$ pip install recombee-api-client
from recombee_api_client.api_client import RecombeeClient
from recombee_api_client.api_requests import *
import random
import pandas as pd
client = RecombeeClient('ADD_YOUR_API_IDENTIFIER', 'ADD_YOUR_PRIVATE_TOKEN')

Dataset

course_df = pd.read_csv("coursea_data.csv", index_col=0)
user_df = pd.read_excel (r'user.xlsx')
client.send(AddItemProperty('course_title', 'string'))
client.send(AddItemProperty('course_organization', 'string'))
client.send(AddItemProperty('course_certificate_type', 'string'))
client.send(AddItemProperty('course_rating', 'double'))
client.send(AddItemProperty('course_difficulty', 'string'))
client.send(AddItemProperty('course_students_enrolled', 'string'))
client.send(AddUserProperty('citizenship', 'string'))
client.send(AddUserProperty('email', 'string'))
client.send(AddUserProperty('full_name', 'string'))
client.send(AddUserProperty('gender', 'string'))
requests = [SetItemValues(
course_df.index[i], #itemId
#values:
{
"course_title": course_df['course_title'][i],
"course_organization": course_df['course_organization'][i],
"course_certificate_type": course_df['course_Certificate_type'][i],
"course_rating": course_df['course_rating'][i],
"course_difficulty": course_df['course_difficulty'][i],
"course_students_enrolled": course_df['course_students_enrolled'][i]
},
cascade_create=True # Use cascadeCreate for creating item
# with given itemId if it doesn't exist
) for i in range(len(course_df))]
# Send catalog to the recommender system
client.send(Batch(requests))
user_requests = [SetUserValues(
row['id'], #itemId
#values:
{
"citizenship": row['citizenship'],
"email": row['email'],
"full_name": row['first_name'],
"gender": row['gender']
},
cascade_create=True # Use cascadeCreate for creating item
# with given itemId if it doesn't exist
) for idx, row in user_df.iterrows()]
# Send catalog to the recommender system
client.send(Batch(user_requests))
#user11 has viewed item0, item104, item107 and rated item110.client.send(AddDetailView('11','0', cascade_create=True))
client.send(AddDetailView('11','104', cascade_create=True))
client.send(AddDetailView('11','107', cascade_create=True))
client.send(AddRating('11','110', 0.5, cascade_create=True))# Rating rescaled to interval [-1.0,1.0],
# where -1.0 means the worst rating possible, 0.0 means neutral, and 1.0 means absolutely positive rating.
# For example, in the case of 5-star evaluations, rating = (numStars-3)/2 formula may be used for the conversion.
# So here user rated 4/5 to a course (4-3)/2 = 0.5
#Also the rating here will have no effect on the rating of item110 that is available in the dataset.
#user13 has viewed item109, item110, item0, item103, item101, item104.
client.send(AddDetailView('13','109', cascade_create=True))
client.send(AddDetailView('13','110', cascade_create=True))
client.send(AddDetailView('13','0', cascade_create=True))
client.send(AddDetailView('13','103', cascade_create=True))
client.send(AddDetailView('13','101', cascade_create=True))
client.send(AddDetailView('13','104', cascade_create=True))
#user14 has viewed item10, item11 and purchased item115.
client.send(AddDetailView('14','10', cascade_create=True))
client.send(AddDetailView('14','111', cascade_create=True))
client.send(AddPurchase('14','115', cascade_create=True))
#user15 has viewed item113, item111, item 105, item102.
client.send(AddDetailView('15','113', cascade_create=True))
client.send(AddDetailView('15','111', cascade_create=True))
client.send(AddDetailView('15','105', cascade_create=True))
client.send(AddDetailView('15','102', cascade_create=True))
#user17 has viewed item140, item142, item15, item151, item144 and purchased item15.
client.send(AddDetailView('17','140', cascade_create=True))
client.send(AddDetailView('17','142', cascade_create=True))
client.send(AddDetailView('17','15', cascade_create=True))
client.send(AddDetailView('17','151', cascade_create=True))
client.send(AddDetailView('17','144', cascade_create=True))
client.send(AddPurchase('17','15', cascade_create=True))
recommended = client.send(RecommendItemsToUser('11',5))
print(recommended)
{'recommId': 'e30ae1d99a1dae19155a6643f1214d0c', 
'recomms': [
{'id': '200'},
{'id': '807'},
{'id': '565'},
{'id': '871'},
{'id': '664'}
]}
  • The user only viewed beginner or intermediate level courses and recommendations are all within that.
  • Rating of recommendation is between 4.6–4.8 based on the four interactions.
  • Certificate type was also only course and specialization and we got recommendations based on that.
  • The courses user viewed is in general 2 categories i.e. Python and Design the recommendations we got are in those categories.

Conclusion:

--

--

DataCTW is a website of online tutorials hosted at different places and are completely free. Our aim is to provide tutorials from scratch so that anyone who is looking to learn something new can benefit from it.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store