How to Build a Recommendation System with Python: A Practical Guide
Recommendation systems power the personalized experiences we see on Netflix, Amazon, and Spotify. Building one in Python is more accessible than you think, thanks to libraries like pandas, scikit-learn, and Surprise. In this tutorial, you’ll learn the core steps to build a memory-based collaborative filtering system.
Before writing code, decide which approach fits your data. Content-based filtering recommends items similar to what the user liked before, while collaborative filtering leverages the behavior of many users. For most beginners, collaborative filtering with a user-item rating matrix is the easiest and most effective starting point.
1. Prepare Your Data
Load your ratings data into a pandas DataFrame with three essential columns: user_id, item_id, and rating. Clean the data by removing missing values and duplicates.
- Use
pd.read_csv()to load your dataset - Drop rows with empty or invalid ratings
- Create a pivot table: users as rows, items as columns
2. Compute Similarity
Use cosine similarity to measure how alike users or items are. The scikit-learn library provides an efficient implementation:
from sklearn.metrics.pairwise import cosine_similarity
sim_matrix = cosine_similarity(user_item_matrix)
3. Generate Recommendations
For a target user, find the top N most similar users. Then score each unseen item by taking a weighted average of their ratings, weighted by similarity. Finally, filter out anything the user has already rated.
4. Evaluate and Improve
Split the data into training and test sets, then compute Root Mean Squared Error (RMSE) to measure prediction accuracy. For a more advanced approach, try matrix factorization with the Surprise library, which handles latent factors automatically.
Conclusion
With just a few lines of Python, you can build a functional recommendation engine. Start with collaborative filtering, measure your errors, and gradually experiment with hybrid models. Your complete code is ready to deploy.