17 April 2023
Recommendation systems have become an integral part of many online platforms, from e-commerce to content streaming services. A recommendation system suggests items that a user may be interested in based on their preferences or behavior. Collaborative filtering is a popular technique for building recommendation systems that is based on the idea that users who have similar preferences in the past will have similar preferences in the future. In this tutorial, we will learn how to build a recommendation system using collaborative filtering in Python.
To follow along with this tutorial, you will need:
To install pandas:
pip install pandas
To install numpy:
pip install numpy
To install matplotlib:
pip install matplotlib
To install scikit-surprise:
pip install scikit-surprise
Make sure to run these commands in your terminal or command prompt after installing Python on your computer.
For this tutorial, we will use the MovieLens dataset, which contains movie ratings given by users. You can download the dataset from here.
The MovieLens dataset contains 100,000 ratings given by 943 users for 1,682 movies. We will preprocess the dataset to make it suitable for building a recommendation system using collaborative filtering.
First, let's load the dataset into a pandas DataFrame:
import pandas as pd # Load the dataset into a pandas DataFrame ratings = pd.read_csv('ratings.csv')
The ratings.csv
file contains four columns: userId
, movieId
, rating
, and timestamp
. We only need the first three columns, so we can drop the timestamp
column:
# Drop the timestamp column ratings.drop('timestamp', axis=1, inplace=True)
Next, we can split the dataset into a training set and a test set. We will use 80% of the data for training and 20% for testing. We can split the data randomly using scikit-learn's train_test_split
function:
from klearn.model_selection import train_test_split # Split the data into a training set and a test set trainset, testset = train_test_split(ratings, test_size=0.2, random_state=42)
The trainset
and testset
variables now contain the training and test data, respectively.
We will now use the Surprise library to build a user-based collaborative filtering model. Surprise is a Python scikit for building and analyzing recommender systems that deal with explicit rating data. To install Surprise, you can run the following command:
pip install scikit-surprise
First, let's load the Dataset
and Reader
classes from Surprise:
from surprise import Dataset, Reader
Next, we need to specify the rating scale for the dataset. The MovieLens dataset contains ratings from 1 to 5, so we will use the Reader
class to specify this:
# Define the rating scale reader = Reader(rating_scale=(1, 5))
We can now load the training data into a Surprise Dataset
object:
# Load the training data into a Surprise Dataset object data = Dataset.load_from_df(trainset, reader)
We can then use the SVD
algorithm to train the user-based collaborative filtering model:
from surprise import SVD from surprise import accuracy # Train the user-based collaborative filtering model using the SVD algorithm model = SVD() model.fit
4.1. User-Based Collaborative Filtering User-based collaborative filtering involves finding users who are similar to a given user and recommending items that those similar users have liked. The similarity between users can be computed using different similarity measures such as cosine similarity, Euclidean distance, and Pearson correlation.
To implement user-based collaborative filtering, we can use the Surprise library in Python. Surprise is a Python scikit for building and analyzing recommender systems that deal with explicit rating data.
Here is an example code snippet for implementing user-based collaborative filtering using Surprise:
from surprise import KNNBasic from surprise import Dataset from surprise.model_selection import cross_validate # Load the dataset data = Dataset.load_builtin('ml-100k') # Use the KNN algorithm with user-based similarity sim_options = {'name': 'cosine', 'user_based': True} algo = KNNBasic(sim_options=sim_options) # Run 5-fold cross-validation and print the results cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)
This code loads the MovieLens 100K dataset, which is included in the Surprise library, and uses the KNNBasic algorithm with user-based cosine similarity to compute recommendations. The cross_validate()
function performs 5-fold cross-validation and prints the results.
4.2. Item-Based Collaborative Filtering Item-based collaborative filtering involves finding items that are similar to a given item and recommending those similar items to the user who has liked the given item. The similarity between items can be computed using different similarity measures such as cosine similarity, Euclidean distance, and Pearson correlation.
To implement item-based collaborative filtering, we can use the Surprise library in Python. Here is an example code snippet for implementing item-based collaborative filtering using Surprise:
from surprise import KNNBasic from surprise import Dataset from surprise.model_selection import cross_validate # Load the dataset data = Dataset.load_builtin('ml-100k') # Use the KNN algorithm with item-based similarity sim_options = {'name': 'cosine', 'user_based': False} algo = KNNBasic(sim_options=sim_options) # Run 5-fold cross-validation and print the results cross_validate(algo, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)
This code loads the MovieLens 100K dataset, which is included in the Surprise library, and uses the KNNBasic algorithm with item-based cosine similarity to compute recommendations. The cross_validate()
function performs 5-fold cross-validation and prints the results.
It is important to note that these methods are not the only ones that can be used to build a recommendation system, and there are many other techniques and algorithms available. However, the methods discussed in this article should provide a good starting point for anyone looking to build a recommendation system. However, it's worth noting that there are many other techniques and algorithms that can be used to build more sophisticated and accurate recommendation systems. Additionally, it's important to keep in mind that the success of a recommendation system depends on the quality and quantity of data available, as well as the specific use case and the target audience.
In conclusion, building a recommendation system can be a challenging but rewarding task for Python developers. By understanding the basic concepts and techniques involved in recommendation systems, developers can create powerful tools that help users discover new content and products, and ultimately drive business growth. I hope this article has provided a useful introduction to the topic and inspired you to explore further.
After building the recommendation system, the next step is to evaluate its performance. Evaluation helps in determining how well the system is performing and whether it's providing recommendations that users find useful.
One commonly used metric for evaluating the performance of recommendation systems is the accuracy metric. Accuracy is the ratio of the number of correct recommendations to the total number of recommendations made by the system. However, accuracy alone cannot provide a complete picture of the system's performance.
Other metrics that can be used to evaluate the performance of a recommendation system include precision, recall, F1 score, and mean average precision. These metrics take into account the number of relevant items recommended, the number of irrelevant items recommended, and the order in which the items are recommended.
Once the recommendation system is built and evaluated, the next step is to deploy it. Deployment involves making the system available to users so that they can start using it. There are different ways to deploy a recommendation system, depending on the requirements and resources available.
One option is to deploy the recommendation system as a web application. This involves building a web interface that users can interact with to get recommendations. The web application can be hosted on a web server and made available to users over the internet.
Another option is to integrate the recommendation system into an existing application. This involves adding the recommendation system as a feature of the application. For example, a recommendation system for an e-commerce site can be integrated into the site's search functionality to provide personalized search results.
Building a recommendation system is a complex task that requires a deep understanding of machine learning algorithms, data processing techniques, and software development skills. However, with the right tools and techniques, it's possible to build a recommendation system that provides useful recommendations to users.
In this article, we have discussed the steps involved in building a recommendation system, including data collection and preprocessing, algorithm selection, model training, and evaluation. We have also discussed the importance of evaluating the performance of the recommendation system and the different ways to deploy it.
By following these steps and continuously improving the recommendation system, it's possible to create a system that provides valuable recommendations to users, improves user engagement, and drives business growth.
JBI Training that can help you improve your skills in Python:
Python Programming Fundamentals: "World Class" Rated course - A comprehensive introduction to Python - a simple and popular language widely used for rapid application development, testing and data analytics.
Data Analysis with Pandas: This course teaches participants how to use the pandas library for data manipulation, analysis, and visualization. Topics covered include data cleaning, merging, grouping, and pivoting. Become fluent and adopt best practices in the use of the pandas library for Python
Python Advanced : Gain a deeper practical understanding of the Python programming language and ecosystem. This course provides a solid overview of the Python language including some low level details essential to working confidently and fluidly with Python.
Python Machine Learning: Gain Python Machine Learning Skills for Predictive Analytics.
These courses can help the reader develop the necessary skills to build recommendation systems and chatbots using Python.
here are some official documentation and links that can be useful:
Pandas documentation: https://pandas.pydata.org/docs/ This is the official documentation for the pandas library, which is a powerful tool for data manipulation and analysis in Python.
NumPy documentation: https://numpy.org/doc/ This is the official documentation for the NumPy library, which provides support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
Matplotlib documentation: https://matplotlib.org/stable/contents.html This is the official documentation for the Matplotlib library, which is a plotting library for Python. It provides a wide variety of customizable charts, graphs, and other visualization tools.
scikit-learn documentation: https://scikit-learn.org/stable/documentation.html This is the official documentation for the scikit-learn library, which is a popular machine learning library for Python. It includes a wide range of tools for classification, regression, clustering, and more.
CONTACT
+44 (0)20 8446 7555
Copyright © 2024 JBI Training. All Rights Reserved.
JB International Training Ltd - Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS
Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us