Netflix Recommendation System using Collaborative Filtering

A step-by-step build of a Netflix-style recommendation system, using a user-item matrix and cosine similarity to predict which movies a user hasn't seen yet.

5:38 video4 min readWatch on YouTube

Every time Netflix or Spotify suggests something you end up watching or listening to, there is a specific mechanism behind that guess, and it is not magic. It is a pattern-matching technique called collaborative filtering, and it works by finding people whose tastes look like yours and borrowing from what they liked that you haven't tried yet.

What collaborative filtering actually assumes

The core idea is easiest to see with a small example. Say there are two users and two movies, and both users have watched and rated both movies. If a third movie exists that only one of those users has watched, and the two users have historically shown similar taste, it makes sense to recommend that third movie to the user who hasn't seen it yet. That is the entire logic of collaborative filtering in one sentence: it relies on the preferences and similarities between users, and it recommends based on the patterns those similarities reveal, not on any description of the movie itself.

The two datasets behind the system

Building a real version of this system starts with two datasets. A movie dataset holds the movie ID, the movie name, and its release year. A ratings dataset holds the user ID, the rating that user gave, and which movie ID that rating belongs to. Before doing anything else, it's worth checking for missing values, though in this case the data was already clean, which meant the build could move straight to structuring it.

Building the user-item matrix

The first real construction step is generating a user-item matrix. Picture a table where each row is a user and each column is a movie. If a user watched and rated a given movie, that rating fills the corresponding cell. If they never watched it, the cell is empty, and those empty cells get filled with zero as a placeholder. With three users and four movies as an example, one user might have rated movies one and two but never watched movie three or four. A second user might have rated movies one and two while skipping three and four. A third might have skipped movie one but rated two and three. The resulting matrix, rows of users against columns of movies, is the foundation everything else in the system is built on.

Measuring similarity between users

Once the matrix exists, the next step is calculating how similar users are to one another. This build uses cosine similarity, though other options exist, including Pearson correlation or Euclidean distance, and each comes with its own tradeoffs. After computing similarity across all users, the system narrows down to the top ten nearest neighbors for every user, meaning the ten other users whose rating patterns most closely match theirs. Those neighbor sets become the basis for everything predicted next.

Predicting the ratings that don't exist yet

This is the step that turns similarity data into actual recommendations. The system iterates over every user and every movie, looking specifically for cells where the value is zero, meaning the user never rated that movie. For each of those empty cells, it uses the similarity matrix to calculate a predicted rating and assigns that predicted value back into the cell. In effect, the system is asking: given how this user's ten most similar neighbors rated this movie, what would this user probably have rated it?

Turning predictions into a recommendation list

With predicted ratings filled in across the matrix, the final step is straightforward. For a given user, the system pulls their predicted values for every movie they haven't already watched or rated, and selects the highest-scoring ones as recommendations. Applied to a specific user in the dataset, this produces a concrete, ranked list of movies that user is statistically likely to enjoy based on people who rate movies the way they do.

Key takeaways

  • Collaborative filtering recommends items based on similarities between users' rating patterns, not on descriptions of the items themselves.
  • A user-item matrix, users as rows and movies as columns with ratings as values, is the structural foundation of the system.
  • Missing ratings are filled with zero as a placeholder before similarity is calculated.
  • Cosine similarity is used here to measure how alike two users' tastes are, though Pearson correlation and Euclidean distance are viable alternatives.
  • Predicted ratings are generated only for the zero cells, using the ratings of a user's top ten most similar neighbors.
  • Final recommendations are the highest predicted-rating movies a user hasn't already watched.

Who this is for

This is for anyone learning how recommendation systems work under the hood, students building a first machine learning project with real movie rating data, and developers who want a concrete, step-by-step example of collaborative filtering rather than a black-box explanation.

Full transcript(auto-generated, with timestamps)

[0:01]Hello everyone today let us learn how to implement a recommendation system what is a recommendation system usually we get recommendations on Netflix or Spotify and uh let us see how those recommendations are made and how those align with our preferences and tastes there are many algorithms used in building recommendation systems but today let us Focus on collaborative filtering so what is collaborative filtering let's take an example to understand that now there are two users user one and user two and there are two movies M1 and M2 so both these users have watched both the movies and there is a third movie empty which only us it

[0:56]Two watched and uh since previously user one and user two or similar content now we want to recommend this movie 3 to user one also uh so that he might also like this movie so basically collaborative filtering means it relies on the preferences and similarities between the users and then recommend uh to the users based on the uh items or the similarities between the users today uh let us implement the Netflix movie recommendation system for this uh we have uh two two data sets taken from uh Netflix movies which is the movie data set and the movie rating data set in the movie data set we have the movie

[1:51]ID the movie name and year in which it is released and in the rating data set we have the user ID the rating uh what we what they gave for a particular movie and the movie ID which they have rated for cleaning uh I've checked the N values and this data is already cleaned uh so uh we can uh we are good to proceed now first we have to uh generate a user item Matrix what is a user item Matrix suppose uh we have user one user two user 3 and here the items means movies Movie 1 movie two Movie 3 and Movie 4 now user one has watched movie

[2:42]One and rated it uh they they might have watched Movie 2 and also rated it and Movie 3 and they didn't watch movie 4 so let's give the rating here zero and a user two might have watched movie one movie two and they they have haven't watched Movie 3 and Movie 4 and user uh three haven't watched Movie 1 but they watch uh Movie 2 and three and didn't watch movie four so this is a user item Matrix where uh the row values consist of the users the column values consist of the movies and the values as the ratings for the corresponding user and the

[3:27]Movie now after generating the user item Matrix now uh I filled in with uh the N values with a zero and uh now we are calculating the similarity between the users uh in order to do this here I'm using cosine similarity but there are other similarity methods that we can use for example PIR by similarity or ukian distance also works after that uh I want the top uh 10 name Neighbors which means that the top 10 similar users to every particular user after getting that now we have to uh predict the ratings of the uh movies which the users haveen been rated previously we have filled those values

[4:17]With zeros here now we want to predict these values and to do now I have to predict the ratings I already have the user item Matrix and the uh similarity user like the user similarity Matrix also now here uh I'm iterating over all the users and over all the items and finding where the value of the cell is zero that means the user hasn't uh rated and uh based on the similarity Matrix I am calculating the predicted rating and assigning it to that particular cell after this uh we want to implement the recommendations and for that for every user that I want to recommend a movie

[5:09]I'm taking the user values and the top 10 movies that they haven't already watched or they haven't already rated and out of that I'm recommending top and items which they might like and uh for the user 424 these are the recommendations that we are making thank you

More videos

Humanitarians AI Lyrical Literacy Project