How to Build a Recommendation Engine Using Surprise in Python

How to Build a Recommendation Engine Using Surprise in Python

Introduction

Building reliable recommendation systems from scratch can be a daunting task, requiring complex algorithms and data handling. This can result in spending significant time implementing common algorithms like SVD or handling cross-validation procedures. However, with the Surprise library, you can build and evaluate recommendation systems with just a few lines of code.

Quick Start with Surprise

Surprise provides a simple and efficient way to build recommendation systems. Here’s an example of how to use it:

import pandas as pd

from surprise import Dataset, SVD, Reader
from surprise.model_selection import cross_validate

# Create a sample ratings dataset
ratings_dict = {
    "itemID": [1, 1, 1, 2, 2],
    "userID": [9, 32, 2, 45, 4],
    "rating": [3, 2, 4, 3, 1],
}
df = pd.DataFrame(ratings_dict)

# Create a reader with a rating scale of 1-5
reader = Reader(rating_scale=(1, 5))

# Load the dataset from the dataframe
data = Dataset.load_from_df(df[["userID", "itemID", "rating"]], reader)

# Create an SVD algorithm object
algo = SVD()

# Run 2-fold cross-validation and print results
result = cross_validate(algo, data, cv=2, measures=['RMSE', 'MAE'], verbose=True)

Output:

Evaluating RMSE, MAE of algorithm SVD on 2 split(s).
    
                      Fold 1  Fold 2  Mean    Std     
RMSE (testset)    1.2706  1.4050  1.3378  0.0672  
MAE (testset)     1.0295  0.9963  1.0129  0.0166  
Fit time          0.00    0.00    0.00    0.00    
Test time         0.00    0.00    0.00    0.00    

Computing Rating Predictions

You can also use Surprise to compute rating predictions for a given user and item. Here’s an example:

item_id = 302
user_id = 196

pred = algo.predict(user_id, item_id, verbose=True)

Output:

user: 196        item: 302        r_ui = None   est = 3.00   {'was_impossible': False}

Available Algorithms

Surprise provides a variety of algorithms for building recommendation systems, including:

  • Basic algorithms
  • k-NN inspired algorithms
  • Matrix factorization-based algorithms

You can view all available algorithms here.

Conclusion

Surprise is a powerful library for building recommendation systems. With its simple and efficient API, you can quickly build and evaluate recommendation systems using a variety of algorithms.

Link to Surprise

Search

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran