Building a Recommendation Engine with Python and Surprise

In today’s world of abundant data, recommendations have become an integral part of online platforms. Whether it’s a music streaming service, an e-commerce website, or a social media platform, personalized recommendations help users find what they want faster, leading to a better user experience.

Introduction to Recommendation Engines and Surprise

Building recommendation engines is becoming increasingly important as more businesses look to personalize user experiences. A recommendation engine is a software system that recommends items, products, or content to users based on their past actions, preferences, or behavior. A popular tool for building recommendation engines is Python, an open-source programming language with powerful libraries and frameworks.

One of the most popular libraries for building recommendation engines in Python is Surprise. Surprise is a Python library for building and analyzing recommender systems. It provides a range of collaborative filtering algorithms, including Singular Value Decomposition (SVD), K-Nearest Neighbors (KNN), and Non-negative Matrix Factorization (NMF).

In this article, we’ll explore how to build a recommendation engine using Python and the Surprise library. We’ll walk through the process of loading and preparing data, building a recommendation model, and evaluating the model’s performance.

Setting up with Environment

Before we begin, you’ll need to install Python 3 and the following libraries:

  • NumPy
  • SciPy
  • Surprise

NumPy and SciPy are Python libraries that provide powerful tools for scientific computing and data analysis. These libraries are widely used in the data science community and are required by many other popular Python libraries, so installing them is a good idea if you plan to work with data in Python.

Surprise is a Python library specifically designed for building recommendation engines, and it provides a number of pre-built algorithms and evaluation metrics to make the process easier.

You can install these libraries using pip, the package installer for Python. Open a command prompt and run the following commands:

pip install numpy
pip install scipy
pip install scikit-surprise

Loading and Preparing Data

The first step in building a recommendation engine is loading and preparing the data. Surprise provides several built-in datasets, including the MovieLens 100k dataset, which contains 100,000 ratings from 943 users on 1682 movies. We’ll use this dataset as an example.

To load the dataset, we’ll use the built-in Dataset.load_builtin() method:

from surprise import Dataset

data = Dataset.load_builtin('ml-100k')

This method downloads the dataset and loads it into a Dataset object, which we can use to build a recommendation model.

The next step is to split the data into training and testing sets. We’ll use the built-in train_test_split() method from Surprise to do this:

from surprise.model_selection import train_test_split

trainset, testset = train_test_split(data, test_size=0.25)

This method randomly splits the data into training and testing sets, with 75% of the data used for training and 25% used for testing.

Building a Recommendation Model

Now that we’ve loaded and prepared the data, we can build a recommendation model using Surprise. We’ll use the SVD algorithm, which is a matrix factorization technique that reduces the dimensionality of the user-item matrix by decomposing it into two lower-dimensional matrices.

To build the model, we’ll create an instance of the SVD class and fit it to the training data:

from surprise import SVD
from surprise import accuracy

model = SVD()

We can then use the model to generate recommendations for a specific user by calling the predict() method:

user_id = 1
item_id = 10

prediction = model.predict(user_id, item_id)

This code generates a prediction for user 1 on item 10 and prints the estimated rating.

Evaluating the Model

The final step is to evaluate the performance of the model. We’ll use the root mean squared error (RMSE) metric to measure the difference between the predicted ratings and the actual ratings in the testing set:

predictions = model.test(testset)

This code generates predictions for all the test set data and calculates the RMSE value.

Tuning the Recommendation Engine

Surprise provides several parameters that can be tuned to improve the performance of the recommendation engine. We can pass these parameters to the SVD algorithm using the GridSearchCV class, which performs a grid search over a range of parameter values and returns the best set of parameters.

For example, we can tune the number of latent factors used in the SVD algorithm as follows:

from surprise.model_selection import GridSearchCV

param_grid = {'n_factors': [50, 100, 150],
              'n_epochs': [20, 30],
              'lr_all': [0.002, 0.005],
              'reg_all': [0.4, 0.6]}

grid_search = GridSearchCV(SVD, param_grid, measures=['rmse'], cv=3)


This code performs a grid search over the n_factors, n_epochs, lr_all, and reg_all parameters and returns the best set of parameters based on the RMSE metric.

Using Cross-Validation

Surprise also provides built-in cross-validation methods for evaluating the performance of the recommendation engine. Cross-validation is a technique for assessing how well a model generalizes to new data by partitioning the data into multiple folds and evaluating the model on each fold.

To perform cross-validation in Surprise, we can use the cross_validate() method:

from surprise.model_selection import cross_validate

model = SVD(n_factors=100, n_epochs=20, lr_all=0.005, reg_all=0.4)

cv_results = cross_validate(model, data, measures=['RMSE'], cv=5)


This code performs a 5-fold cross-validation on the SVD algorithm with the specified parameters and prints the average RMSE value across all folds.


Building a recommendation engine is a complex task that requires careful consideration of data, algorithms, and evaluation metrics. In this article, we’ve explored how to build a recommendation engine using Python and the Surprise library.

We’ve shown how to load and prepare data, build a recommendation model using the SVD algorithm, and evaluate the model’s performance using the RMSE metric. We’ve also explored some advanced features of Surprise, including parameter tuning and cross-validation.

With the knowledge and tools provided in this article, you should be able to build and fine-tune recommendation engines for a variety of applications. By providing personalized recommendations to users, you can enhance the user experience and drive business success.