Building a recommender system — part 2

Aleksandar Gakovic
May 10, 2020 · 4 min read

In part 1 of this two-part series, we learnt how to import and prepare data for a recommendation engine. In this article, we’ll look at:

  • How the data is used and handled
  • How a recommendation is produced
  • How you can use that recommendation in your project.

As before you can refer to my project GitHub page Zero to Hero and code along.

“Let’s get started”

Let's get started…

  1. Import the necessary library modules.
Reader and Dataset and a prediction algorithm are the core components.

Surprise — Simple Python RecommendatIon System Engine— documentation here, offers a wide variety of prediction algorithms. I experimented with SVD, and the variety of Neighborhood algorithms offered to find that a Basic-KNN performs best for my project. Use a grid-search/random search to fine-tune your hyperparameters and use cross-validation with RMSE, MAE, or Accuracy to evaluate.

2. Reading in the data.

The reader is how surprise makes sense of the data.

First, we instantiate a Reader(). Passing in rating_scale as an argument with 1, 5 representing, you guessed it… a rating scale of 1 to 5. This will allow surprise to make sense of our data.

Assign a Surprise-Dataset to a variable ‘train_data’ passing in the columns we want to use and calling upon our reader created above.

Lastly here we use a train_test_split to get training and test sets with the usual 25/75 split.

3. Its time to choose a model and fit it to our trainset.

Algobaseitems is our model.

You have a number of options for which model to choose in this Baseline K-Nearest Neighbour model I have chosen the parameters Cosine similarity (sim_options) and for the distances to calculated for the items (heroes in this case).

4. Getting the recommendation to the target audience

One way to provide a recommendation as a service is to code a rating system in which users are provided a sample of the items, giving them ratings. This allows you to overcome the cold start problem if a user has not rated anything ever before. Once they have rated a selection of sample items they can be given a recommendation.

Below is a snippet from my user rating function.

This is a snippet from the user rating function.

Using the input() method I ask users to rate a sample of five heroes.

The samples come from this snippet of code which rests in the same function snipped above. Note the lines of code under the comment Obtain a random data frame entry.

We’ve reached the final stretch.

5. Now that you have new ratings, you can use them to make predictions for this new user.

Making a prediction:

algo.predict(user, item, clip=False)[3]))

The process from here is straightforward:

  • Add the new ratings to the original rating DataFrame.
  • Read into a surprise dataset.
  • Train a new model using the updated DataFrame.
  • Make predictions for the user and order those predictions from highest rated to lowest rated, you can return the top n recommendations with the text name of the item.

…and that's how to build a simple recommendation system with Sci-kit Surprise!

The Startup

Get smarter at building your thing. Join The Startup’s +724K followers.

Aleksandar Gakovic

Written by

Practicing Data Scientist. Interested in Games, Gamification, Ocean Sciences, Music, Biology.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Aleksandar Gakovic

Written by

Practicing Data Scientist. Interested in Games, Gamification, Ocean Sciences, Music, Biology.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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