Stop 11 · Unsupervised LearningTopics · 10/10

Recommender Systems Basics

Part of the Machine Learning Roadmap

In short

The 'You may also like' row drives a big share of what people watch and buy online. Behind it are a few core ideas: match items to your past likes, learn from people similar to you, or uncover hidden tastes from a ratings table.

Recommenders suggest items a user is likely to want. Content-based methods match item features to a user's past likes, collaborative filtering uses patterns from similar users or items, and matrix factorisation learns hidden tastes from a ratings table.

The jaadu behind a small lending library counter hands a book to one reader while glancing at another reader holding a similar stack of books.

Think of it like this

The neighbourhood book lending library

The uncle who runs the lending library recommends books two ways. 'You liked a detective novel, here's another detective novel' matches the book's features: that's content-based. 'Riya and you liked the same five books, and Riya loved this one, so you might too' uses other readers: that's collaborative filtering.

Watch it happen

Two families of recommenders

Content-based

  • Uses item features: genre, cast, language, price.
  • Recommends items similar to what this user liked.
  • Works for brand-new items, but rarely surprises the user.

Collaborative filtering

  • Uses only the user-item ratings or clicks table.
  • Finds similar users or similar items from behaviour.
  • Can surprise, but struggles with new users and new items (cold start).

Most real systems are hybrids: content features handle cold start, and collaborative signals add discovery.

The big ideas

What you need to know

The ratings matrix

Rows are users, columns are items, and most cells are empty. Recommending means filling in the blanks.

Item-item similarity

Compare item columns with cosine similarity. 'People who bought this also bought' is item-based collaborative filtering.

Matrix factorisation

Learn a short vector of hidden tastes for each user and each item. Their dot product predicts the rating.

Cold start

New users have no history and new items have no ratings. Fall back on popularity or content features.

See it in code

Walk through the code

python
1import numpy as np2import pandas as pd3from sklearn.metrics.pairwise import cosine_similarity4 5ratings = pd.DataFrame(6    [[5, 4, 0, 1, 0],7     [4, 5, 1, 0, 0],8     [0, 1, 5, 4, 5],9     [1, 0, 4, 5, 4],10     [5, 0, 0, 1, 0]],11    index=["Aman", "Bhavna", "Chetan", "Divya", "Esha"],12    columns=["3 Idiots", "Dangal", "Drishyam", "Kahaani", "Andhadhun"],13)                                    # 0 means not rated14 15item_sim = pd.DataFrame(cosine_similarity(ratings.T),16                        index=ratings.columns, columns=ratings.columns)17 18user = ratings.loc["Esha"]19unseen = user[user == 0].index20scores = {m: (item_sim.loc[m, user.index] * user).sum() / item_sim.loc[m, user.index][user > 0].sum()21          for m in unseen}22print(sorted(scores.items(), key=lambda kv: kv[1], reverse=True))

1/5Five users, five films. Zero means the user hasn't rated it yet.

Item-based collaborative filtering on a tiny movie ratings table.

Save yourself the pain

Common mistakes

Oops

Treating a missing rating as a rating of zero when computing averages.

Do this instead

Missing means unknown, not disliked. Only average over items the user actually rated.

Oops

Evaluating a recommender only by rating error.

Do this instead

Users see a ranked list. Measure precision@k or recall@k on held-out interactions too.

Your mission

Recommend a movie

~40 min

Quick check

A new food delivery app has no user history yet. Which approach can still recommend restaurants?

Go deeper

  1. 1Build an item-item collaborative filter with cosine similarity on the MovieLens dataset.
  2. 2Recommend five movies for one user and explain why each was picked.
  3. 3Read about the cold-start problem and one way to handle new users.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge