Elena' s AI Blog

How recommendation engines actually work (with Python code)

08 May 2024 (updated: 14 Sep 2026) / 21 minutes to read

Elena Daehnhardt

Midjourney AI art, May 2024


TL;DR:
  • Recommender systems generally use collaborative filtering (finding similar users) or content-based filtering (finding similar items). We implement both in Python using scikit-learn for cosine similarity and TF-IDF, and scipy for Singular Value Decomposition (SVD).

Previous: Part 25 β€” Bias-Variance Challenge

Next: Part 27 β€” OpenAI's Model Show-off

How Recommendation Engines Work: Collaborative and Content-Based Filtering

Ever wondered how Netflix or Spotify manages to guess exactly what you want to watch or listen to next? The secret lies in recommendation algorithms. A recommender system is a machine learning system that predicts which items a user will prefer, based on past ratings or item features, and surfaces the highest-scoring ones.

Whether they are suggesting movies, songs, or the next book to read, these systems generally rely on two core approaches: collaborative filtering (finding people with similar tastes) and content-based filtering (finding items similar to what you already like).

In this post, we will look under the hood of these recommendation systems, exploring the mathematical theory behind them and implementing them from scratch using Python. Let’s go!

The Rating Prediction Task in Recommender Systems

When we create Recommender Systems (RS), we consider that we have a set of users and items which are recommended to these users. In practice, we have a prior history of user ratings. This history is used to create suggestions or recommendations.

Consider a movie recommender as a widely given example of a recommender system. For instance, users watch Netflix content and rate movies they watch. Netflix has knowledge of preferred movies and recommends movies not yet seen that will be possibly liked by users (ideally :)

Basic RS uses matrices to store user ratings, such as :

[Users\Movies]  | User 1 | User 2 | User 3 | ... | User N |
----------------|-----------------------------------------|
Movie 1         |   10   |    4   |   6    | ... |   9    |
----------------------------------------------------------|
Movie 2         |   ?    |    7   |   9    | ... |   7    |
----------------------------------------------------------|   
Movie 3         |   7    |    9   |   6    | ... |   ?    |
----------------------------------------------------------|
Movie 4         |   ?    |    ?   |   9    | ... |   7    |
----------------------------------------------------------|  

Notice that User 1 did not watch Movie 2 (we have a β€œ?” question mark in the cell of the rating table), and User N did not watch Movie 3.

We have to predict the missing user ratings. This task is called rating prediction. We can recommend movies with the highest predicted ratings when we predict all the missing values or movie ratings.

Indeed, not all recommenders use this matrix format in practice. Data structures and algorithms must be optimised for effective resource management and reduced computation time.

Recommendations should be created quickly with scalability in mind. This is particularly important when dealing with big data in production settings. However, let’s keep it simple and consider the rating matrix structure.

Collaborative Filtering: Recommending by User and Item Similarity

πŸ”’ Subscribe to keep reading.

Content-Based Filtering: Recommending by Item Features

πŸ”’ Subscribe to keep reading.

Summary: Collaborative vs Content-Based Filtering

πŸ”’ Subscribe to keep reading.

Conclusion

πŸ”’ Subscribe to keep reading.

References

πŸ”’ Subscribe to keep reading.

You've hit a Deep Dive tutorial.

I spend dozens of hours researching, coding, and breaking things to write these guides. This content is free, but reserved for my subscriber community. Drop your email below to unlock this guide (and all past/future deep dives):

Already a subscriber? Use the magic link from your last newsletter, or reset your password.

New subscribers get an inbox mail: Set a password to unlock articles. The form does not log you in β€” use the same email afterwards.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.





Citation
Elena Daehnhardt. (2024) 'How recommendation engines actually work (with Python code)', daehnhardt.com, 08 May 2024. Available at: https://daehnhardt.com/blog/2024/05/08/recommender_system_approaches_with_python_code_collaborative_filtering_content_based/
All Posts