Must know ML techniques for digital analysts: Part 3— Recommendation Engines
By: Abhinav Sharma
Tags: Machine Learning, Data Science, Recommendation System

Each of the techniques shown in the diagram may be used to build a recommender system model. Let’s briefly explore the various recommendation engine categories.

Content-based filtering, recommends items by comparing product attributes and customer profile attributes. The attributes of each product are represented as a set of tags or terms — typically the words that occur in a product description document. The customer profile is represented with the same terms and built by analyzing the content of products that have been seen or rated by the customer. Typically, the content-based filtering method provides a list of top N recommendations based on some similarity scores.

Collaborative filtering filters information by using the recommendations of other people. Given a database of user ratings for products, where a set of users have rated a set of products, collaborative filtering algorithms can give ratings for products yet to be rated by a particular user. This leverages the neighborhood information of the user to provide such recommendations. The underlying premise of the collaborative filtering algorithm is that if two users agree on ratings for a large set of items, they may tend to agree for other items too.

Collaborative filtering can be further classified into:

Memory-based : In this method, user rating information is used to compute the likeness between users or items. This computed likeness is then used to come up with recommendations. This differentiates from content-based recommendations where item/user metadata is used to calculate similarity scores rather than their feedback (eg. ratings).

: In this method, user rating information is used to compute the likeness between users or items. This computed likeness is then used to come up with recommendations. This differentiates from content-based recommendations where item/user metadata is used to calculate similarity scores rather than their feedback (eg. ratings). Model-based : Data mining methods are applied to recognize patterns in the data, and the learned patterns are then used to generate recommendations. We have already covered a popular technique of association mining using the apriori algorithm in Part 1.

: Data mining methods are applied to recognize patterns in the data, and the learned patterns are then used to generate recommendations. We have already covered a popular technique of association mining using the apriori algorithm in Part 1. The latent factor approach leverages matrix factorization techniques to arrive at recommendations. Recently these methods have proved themselves superior to item-based and user-based recommender systems. This was one of the winning solutions in the famous Netflix recommendation competition.

Finally, Hybrid filtering is the system where we combine more than one type of recommendation system to come up with final recommendations.

Singular-value decomposition approximation, most popular items, and SlopeOne are some other popular techniques that may be employed to build recommendation systems. Further learning on recommendation engines could be in the direction of exploring and studying these rarely-used techniques and applying them to real-world problems.

Example Implementation

For standalone implementation, both Python and R have packages that include the most popular recommendation techniques bundled together. Recommenderlab package in R is a one-stop-shop for building recommendation engines and provides awesome functionality to convert datasets into the required format and train/test ensemble of models. The recommender function contains the option to include parameters for different similarity scores that we want to choose.

As an example for this article, we will take a more organic approach and try to implement a content-based recommendation for a news aggregator website.

Consider the following use case -

A news aggregator wants to solve the following problem: When a customer browses a particular article, what other articles should we suggest to him? The challenge is we don’t have any information about customer preferences. We are either looking at the customer for the first time or we don’t have any mechanism set up yet to capture customer interaction with our products/items.

We will be pulling up data from a news aggregator dataset from UCI public repository.

when a user is browsing a particular news article, we need to give him other news articles as recommendations, based on:

The text content of the title of the article he is currently reading

The publisher of this document

The category to which document belongs

The polarity of the document (something we will calculate based on the text content of the title)

Polarity identification algorithms use text mining to get the document's opinion. We will use one such algorithm to get the polarity of our texts.

We need multiple similarity measures for this use case:

Cosine distance/similarity for comparing words in two documents

For the polarity, a Manhattan distance measure

For the publisher and category, Jaccard’s distance

Refer GitHub for the complete markdown of this project.

Here is the break down of the code -

The first part of the code (up to line 62) focusses on data wrangling, and then refining the dataset to contain only a fraction of data (for scaling down the project) and further focussing only on the top 100 publishers with the maximum number of articles.

Similarity Index

We use a bag-of-words representation of all article titles to measure their similarity scores. We use cosine distance as a similarity scoring measure because it is non-invariant to changes in the magnitude of values and will change if there are changes in the article.

The process involves:

using tm package in R, creating a document term matrix (dtm) of all the articles.

use it to measure cosine distance between articles and return a document matrix.

# cosine distance

sim.score <- tcrossprod_simple_triplet_matrix(dtm)/(sqrt( row_sums(dtm^2) %*% t(row_sums(dtm^2)) )) sim.score[1:10,1:10]

image by author

Search

In this section, our aim is to filter the top 30 articles based on cosine similarity matches. So for example our current document is article ID 16947 titled “UPDATE 1-Ukraine crisis worries hammer German investor morale” by Reuters, then we pick up the top 30 matching articles based on cosine distance.