Recommendation engine with Surprise SVD have become an important tool for businesses seeking to increase customer retention and satisfaction. These engines use algorithms to generate personalized recommendations for users, based on their past behavior and the behavior of other similar users. One popular algorithm for building recommendation engines is Singular Value Decomposition (SVD). In this tutorial, we will be using the Surprise library in Python to build a recommendation engine with Surprise SVD.
Recommendation Engine with Surprise SVD- Installing Surprise Library in Python
To begin with, we need to install the Surprise library which comes with all the algorithms you need to create a recommendation engine.
!pip install scikit-surprise
Recommendation Engine with Surprise SVD -Loading the Dataset and Performing Train-Test Split
Next, we need a dataset to train and test the recommendation engine. We will be using the popular MovieLens dataset for this purpose.
from surprise import Dataset from surprise.model_selection import train_test_split data = Dataset.load_builtin('ml-100k') trainset, testset = train_test_split(data, test_size=.25)
Recommendation Engine with Surprise SVD -Initializing and Training the SVD Algorithm
Now, we can initialize the SVD algorithm and train it on our dataset. We can set the number of factors (k) that we want the algorithm to use for approximation. Higher values of k will result in better performance but will also increase the training time.
from surprise import SVD algo = SVD(n_factors=50) algo.fit(trainset)
Recommendation Engine -Generating Recommendations for a User
Finally, we can use the trained algorithm to generate recommendations for a particular user.
user_id = str(196) item_ids = [] for item_id in range(1, 1683): if not trainset.ur.get(int(user_id)): item_ids.append(str(item_id)) predictions = algo.test([(user_id, item_id, 4.0) for item_id in item_ids]) recommendations = sorted(predictions, key=lambda x: x.est, reverse=True)[:10] for recommendation in recommendations: print(recommendation.iid, recommendation.est)
This code will generate top 10 recommended items for the user with id 196. You can change the user id to generate recommendations for other users.
Creating a recommendation engine with SVD is thus relatively simple with the Surprise library in Python. By using the trained algorithm to generate personalized recommendations, businesses can improve customer satisfaction and retention, leading to greater profits and success.
Want to learn more about Python, checkout the Python Official Documentation for detail.