Collaborative filtering

  • Collaborative filtering leverages similarities between users and items to provide recommendations, unlike content-based filtering.

  • This method allows for serendipitous recommendations by suggesting items based on the preferences of similar users.

  • Collaborative filtering models can automatically learn embeddings, eliminating the need for manual feature engineering.

  • These embeddings represent users and items in a shared space where their proximity reflects similarity and preference.

  • Training these models involves optimizing embeddings to align with user feedback, bringing similar users and preferred items closer together in the embedding space.

To address some of the limitations of content-based filtering,collaborative filtering usessimilarities between users anditems simultaneously to provide recommendations. This allowsfor serendipitous recommendations; that is, collaborative filteringmodels can recommend an item to user A based on the interests ofa similar user B. Furthermore, the embeddings can be learnedautomatically, without relying on hand-engineering of features.

Movie recommendation example

Consider a movie recommendation system in which the training data consistsof a feedback matrix in which:

  • Each row represents a user.
  • Each column represents an item (a movie).

The feedback about movies falls into one of two categories:

  • Explicit— users specify how much they liked a particular movieby providing a numerical rating.
  • Implicit— if a user watches a movie, the system infers that theuser is interested.

To simplify, we will assume that the feedback matrix is binary; that is, a valueof 1 indicates interest in the movie.

When a user visits the homepage, the system should recommend moviesbased on both:

  • similarity to movies the user has liked in the past
  • movies that similar users liked

For the sake of illustration, let's hand-engineer some features for the moviesdescribed in the following table:

MovieRatingDescription
The Dark Knight RisesPG-13Batman endeavors to save Gotham City from nuclear annihilation in this sequel toThe Dark Knight, set in the DC Comics universe.
Harry Potter and the Sorcerer's StonePGA orphaned boy discovers he is a wizard and enrolls in Hogwarts School of Witchcraft and Wizardry, where he wages his first battle against the evil Lord Voldemort.
ShrekPGA lovable ogre and his donkey sidekick set off on a mission to rescue Princess Fiona, who is emprisoned in her castle by a dragon.
The Triplets of BellevillePG-13When professional cycler Champion is kidnapped during the Tour de France, his grandmother and overweight dog journey overseas to rescue him, with the help of a trio of elderly jazz singers.
MementoRAn amnesiac desperately seeks to solve his wife's murder by tattooing clues onto his body.

1D embedding

Suppose we assign to each movie a scalar in \([-1, 1]\) that describeswhether the movie is for children (negative values) or adults (positive values).Suppose we also assign a scalar to each user in \([-1, 1]\) that describesthe user's interest in children's movies (closer to -1) or adultmovies (closer to +1). The product of the movie embedding and the userembedding should be higher (closer to 1) for movies that we expect the userto like.

Image showing several movies and users arranged along a one-dimensional embedding space. The position of each movie along this axis describes whether this is a children's movie (left) or an adult movie (right). The position of a user describes interest in children or adult movies.

In the diagram below, each checkmark identifies a movie that a particularuser watched. The third and fourth users have preferences that arewell explained by this feature—the third user prefers movies for childrenand the fourth user prefers movies for adults. However, the first and secondusers' preferences are not well explained by this single feature.

Image of a feedback matrix, where a row corresponds to a user, and a column corresponds to a movie. Each user and each movie is mapped to a one-dimensional embedding (as described in the previous figure), such that the product of the two embeddings approximates the ground truth value in the feedback matrix.

2D embedding

One feature was not enough to explain the preferences of all users. To overcomethis problem, let's add a second feature: the degree to which each movie isa blockbuster or an arthouse movie. With a second feature, we can now representeach movie with the following two-dimensional embedding:

Image showing several movies and users arranged on a two-dimensional embedding space. The position of each movie along the horizontal axis describes whether this is a children's movie (left) or an adult movie (right); its position along the vertical axis describes whether this is a blockbuster movie (top) or an arthouse movie (bottom). The position of the users reflect their interests in each category.

We again place our users in the same embedding space to best explainthe feedback matrix: for each (user, item) pair, we would like thedot product of the user embedding and the item embedding to be closeto 1 when the user watched the movie, and to 0 otherwise.

Image of the same feedback matrix. This time, each user and each movie is mapped to a two-dimensional embedding (as described in the previous figure), such that the dot product of the two embeddings approximates the ground truth value in the feedback matrix.

Note: We represented both items and users in the same embedding space.This may seem surprising. After all, users and items are two differententities. However, you can think of the embedding space as an abstractrepresentation common to both items and users, in which we can measuresimilarity or relevance using a similarity metric.

In this example, we hand-engineered the embeddings. In practice, the embeddingscan be learnedautomatically, which is the power of collaborative filteringmodels. In the next two sections, we will discuss different models to learnthese embeddings, and how to train them.

The collaborative nature of this approach is apparent when the model learns theembeddings. Suppose the embedding vectors for the movies are fixed. Then,the model can learn an embedding vector for the users to best explaintheir preferences. Consequently, embeddings of users with similar preferenceswill be close together. Similarly, if the embeddings for the users are fixed,then we can learn movie embeddings to best explain the feedback matrix.As a result, embeddings of movies liked by similar users will be close inthe embedding space.

Check your understanding

The model recommends a shopping app to a user because they recently installed a similar app. What kind of filtering is this an example of?
Content-based filtering
Good job! Content-based filtering doesn't look at other users.
Collaborative filtering
Collaborative filtering takes other users into consideration. In the given scenario we only care about one user.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-25 UTC.