Collaborative Filtering on Anime Dataset using fastai2
By: Vinayak Nayak
Tags: Recommendations, Fastai, Collaborative Filtering, Python, Data Science

The Basic Intuition behind Collaborative Filtering

In order to perform collaborative filtering, both the user/ subscriber/ consumer and the item/ article/ product are represented as matrices. (terminology here is specific to the application but since we’re dealing with anime data, we’ll call users as viewers and item as anime respectively.)

More precisely, every user is represented as a vector of numeric values and so is every anime. These values are learned by the algorithm to minimise a loss function which is most commonly the Mean Squared Error Loss (MSE).

The values learned for the user and anime vectors correspond to an abstract n-dimensional space and really have no human-interpretative meaning. But as an attempt to understand what’s really happening, we can think of it this way. Consider the dataset that we’re working with i.e. Viewer-Anime Dataset.

Photo by Roman Denisenko on Unsplash

Let’s say every anime consists of 5 components — romance, comedy, slice of life, harem and action. Then an anime can be represented in the vector space of 5-Dimensions each dimension being one of the above.

In that 5-Dimensional world, Oregairu and Hyouka would be closer to each other and Tokyo Ghoul would be relatively far from them. This approach therefore creates a n-dimensional space where all anime that have similar components will be grouped closer than the ones which have different anime.

Now, think of the viewer. The viewer vector will on the other hand represent what proportion of every dimension is dear to the user. Let’s say the user is a fan of romance and slice of life, then the vector representation of this user will have high numeric values along those dimensions and a smaller numeric value along the remaining dimensions.

In order to consolidate the information from both the viewer and anime we resort to using the dot product which is a simple multiplication along the respective vector dimensions followed by an aggregation. Let me explain the same using the example of these three anime that we nitpicked above.

Image by Vinayak

The columns represent names of the Anime and each of them is represented as a 5-Dimensional Vector with each component/dimension representing a particular genre as shown above.

As we can see Hyouka and Oregairu have mostly similar values along all the dimensions and Tokyo Ghoul has pretty different values across all dimensions.

Image by Vinayak

Viewer 1 prefers action and slice of life a lot more than other components, so, let’s compute the dot product of viewer 1 with all three anime. I’ll show the computation once manually below and you can similarly compute other dot products.

Image by Vinayak

We take the two vectors Oregairu and Viewer 1 and multiply respective components together as seen in the snap above i.e. Multiply Romance Dimension of Oregairu with Romance Dimension of Viewer 1, Comedy dimension of Oregairu with Comedy dimension of Viewer 1 and so on. Finally aggregate these products together into a single value by summing.

Once we do this for all three anime we observe Viewer 1’s dot products are 29.2, 26 and 31.8 respectively. This means Viewer 1 likes Tokyo Ghoul the best which is indeed high on action.

Viewer 2 on the other hand is into romance and harem. His dot product with all three anime are 47.5, 35.5, 22.5 which means he would like Oregairu the most which is indeed a teenage love comedy! Yukinoshita, Yuigahama, Ishhiki, damn even Shizuka, his teacher, seem to have the hots for Hachiman; don’t you dare tell me you overlooked the harem in this anime…

After that otaku tangent, getting back to the topic at hand, we can see how representing viewers and anime as a vector can prove useful to determine a ranking of how a viewer would like all the anime or which users will like a particular anime and that’s a very valuable insight which streaming companies like Netflix are exploiting.

There’s also an additional component to both the viewer and the anime vectors which is called bias. It is a quantity used to represent unique information about that particular user or that particular anime respectively. So, if there’s a user who on an average rates all anime highly or there’s an anime which is a favourite across all/most viewers irrespective of their usual tastes, such information is captured by a term called bias. So, every viewer and every anime is represented in this structure using an n-dimensional vector and a bias term.