Making a recommendation engine

Recommendation engines are a powerful way to boost sales on e-commerce websites, since they allow us to suggest to customers products that are likely to meet their preferences.

These suggestions are produced by looking at previous purchases (or wish lists and visited products) and comparing them with other customers and their purchases.

Basically, recommendation engines state that if you bought those products, you are similar to these other customers who also bought these products. So, probably, you will like these products as well.

Getting ready

In this recipe, we will compute the cosine measure of a matrix in order to measure similarity of vectors composing the matrix.

The lsa package provides a specific cosine() function for this purpose. In order to use it, we first have to install and load the package:

install.packages("lsa")
library(lsa)

Our recommendation engine will be applied to a data frame that stores movie reviews from five critics, ranging from 1 to 4.

Let's create the data.frame list with the following script:

reviews <- data.frame("movie" = c("the_chronicles_of_narnia","star_wars_IV","star_wars_VI","beautiful_mind"),
  
"Thomas" = c(1,4,4,2),
  "Jannine" = c(3,2,4,4),
  "Francis"  = c(2.4,3,2,1),
  "Mary" = c(3,2,4,3),
  "Zachary" = c(0,2,0,4))

How to do it...

  1. Compute the cosine similarity measure between critics:
    similarity_matrix <- cosine(as.matrix(reviews[,2:6]))
    
  2. Transpose the original data frame list:
    t_reviews <- as.data.frame(t(reviews))
    colnames(t_reviews) <- c("the_chronicles_of_narnia","star_wars_IV","star_wars_VI","beautiful_mind")
    t_reviews[,1]       <- as.numeric(t_reviews[,1])
    t_reviews[,2]       <- as.numeric(t_reviews[,2])
    t_reviews[,3]       <- as.numeric(t_reviews[,3])
    t_reviews[,4]       <- as.numeric(t_reviews[,4])
    
  3. Define the weighted score by multiplying reviews for the similarity score of each critic with Zachary:
    weighted_scores <- similarity_matrix[,5]*t_reviews[,1:4]
    
  4. Define movies that Zachary will probably like:
    zachary_suggestions <- colSums(weighted_scores)
    > zachary_suggestions
    the_chronicles_of_narnia             star_wars_IV            star_wars_VI           beautiful_mind
                   12.615616                 8.010968            10.432623                11.751864 
    

According to our recommendation engine, Zachary is most likely to appreciate the_chronicles_of_narnia from the movies provided.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset