Building a hybrid recommendation system for Jokes recommendations

We see that both content-based filtering and collaborative filtering have their strengths and weaknesses. To overcome the issues, organizations build recommender systems that combine two or more technique and they are termed hybrid recommendation models. An example of this is a combination of content-based, IBCF, UBCF, and model-based recommender engine. This takes into account all the possible aspects that contribute to making the most relevant recommendation to the user. The following diagram shows an example approach followed in hybrid recommendation engines:

Sample approach to hybrid recommendation engine

We need to note that there is no standard approach to achieving a hybrid recommendation engine. In order to combine recommendations, here are some suggested strategies:

  • Voting: Apply voting among the recommendation output obtained from individual recommender systems.
  • Rules-based selection: We could devise rules that suggest weighting the output recommendations obtained from individual recommender systems. In this case, the output from recommender systems that got higher weights will be dominant and have more influence on the final recommendation outcome.
  • Combination: Recommendations from all the recommender engines are presented together. A final list of recommendations is just the union of all recommendation output obtained from individual recommender systems.
  • Attribute integration: Taking metadata from all recommender system to infuse it as input to another recommender.

Again, what works for a problem may not work for another, therefore these strategies need to be tested individually prior to coming up with final recommendation strategy.

The recommenderlab library offers the HybridRecommender function which allows users to train multiple recommender engines on the same set of data in one go and combine the predictions. The function has a weights parameter that offers a way to specify the weight of each of the models that will be used to combine individual predictions to arrive at the final recommendation predictions on unseen data. Implementing a hybrid recommendation-engine-based project is super straightforward and not too different from the code we learned in item-based collaborative filtering or user-based collaborative filtering projects. Anyway, let's write the code and build a hybrid recommendation engine for the Jester5k dataset:

# including the required libraries
library(recommenderlab)
# accessing the Jester5k dataset that is a part of recommenderlab library
data(Jester5k)
# split the data into the training and the test set
Jester5k_es <- evaluationScheme(Jester5k, method="split", train=0.8, given=20, goodRating=0)

The preceding code is what trains a hybrid recommender. This is where it differs from the ITCF or UBCF recommenders we've built. We can observe from the code that we have used four different recommender methods that will constitute the hybrid recommender. Let's discuss each of these methods:

  • The popular recommendation method simply recommends the popular jokes (determined by the number of ratings received) to users.
  • The second recommender method we have used is item-based collaborative filtering method with non-normalized data but with distance being computed between items through cosine similarity.
  • User-based collaborative filtering on Z-score normalized data with Euclidean distance being computed between users in the data.
  • A random recommendation method that provides a random recommendation to the users.

By no means, we finalize that the combination of these four recommender methods is the best hybrid for this problem. The intention of this project is to demonstrate the implementation of the hybrid recommender. The choice of the methods involved is purely arbitrary. In reality, we may need to try multiple combinations to identify the best hybrid. The hybrid classifier is built using the following code:

#train a hybrid recommender model
hybrid_recom <- HybridRecommender(
Recommender(getData(Jester5k_es, "train"), method = "POPULAR"),
Recommender(getData(Jester5k_es, "train"), method="IBCF",
param=list(normalize = NULL, method="Cosine")),
Recommender(getData(Jester5k_es, "train"), method="UBCF",
param=list(normalize = "Z-score",method="Euclidean")),
Recommender(getData(Jester5k_es, "train"), method = "RANDOM"),
weights = c(.2, .3, .3,.2)
)
# Observe the model that is built
print (getModel(hybrid_recom)

This will result in the following output:

$recommender
$recommender[[1]]
Recommender of type ‘POPULAR’ for ‘realRatingMatrix’
learned using 4000 users.
$recommender[[2]]
Recommender of type ‘IBCF’ for ‘realRatingMatrix’
learned using 4000 users.
$recommender[[3]]
Recommender of type ‘UBCF’ for ‘realRatingMatrix’
learned using 4000 users.
$recommender[[4]]
Recommender of type ‘RANDOM’ for ‘realRatingMatrix’
learned using 4000 users.
$weights
[1] 0.2 0.3 0.3 0.2

Observe the weights assignment in the hybrid model. We see that the popular and random recommenders are assigned 20% weight each, whereas the ITCF and UBCF methods involved in the preceding hybrid are assigned 30% weight each. It is not mandatory to set the weights while building a hybrid recommender, in which case, equal weights are assigned to each of the methods involved in the hybrid recommender. Now that our model is ready, let's make predictions and evaluate the performance with the following code:

# making predictions
pred <- predict(hybrid_recom, getData(Jester5k_es, "known"), type="ratings")
# # set the predictions that fall outside the valid range to the boundary values
pred@data@x[pred@data@x[] < -10] <- -10
pred@data@x[pred@data@x[] > 10] <- 10
# calculating performance measurements
hybrid_recom_pred = calcPredictionAccuracy(pred, getData(Jester5k_es, "unknown"))
# printing the performance measurements
library(knitr)
print(kable(hybrid_recom_pred))

This will result in the following output:

|     |         x|
|:----|---------:|
|RMSE | 4.468849|
|MSE | 19.970611|
|MAE | 3.493577|
..................Content has been hidden....................

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