Recommenderlab: Receiving Duplicate Predictions for Multiple Users

211 Views Asked by At

I am using Recommenderlab in R to build a recommendation system to provide craft-beer suggestions to new users.

However, upon running the model, I am receiving the same predictions per user for a majority of the training dataset, or receiving 'character(0)' as the output. How can I receive the predictions that are associated with each user and not duplicated?

The dataset I'm using can be found here: https://www.kaggle.com/rdoume/beerreviews/version/1

I have tried converting the data frame directly into a matrix, then into a realRatingMatrix.

In order to receive any recommendations, I need to use the 'dcast' function from the data.table library before converting the data frame into a matrix.

I have also tried removing the first column from the matrix to drop the user ids.

One thing to note is that when the data is sampled, there can be a few rows where the 'reviewer' is blank, but the rating and beer id is there.

library(dplyr)
library(tidyverse)
library(recommenderlab)
library(reshape2)
library(data.table)

beer <- read.csv('beer.csv', stringsAsFactors = FALSE)

#Take sample of data(1000) 
beer_sample <- sample_n(beer, 1000)
#Select relevant columns & rename 
beer_ratings <- select(beer_sample, reviewer = review_profilename, beerId = beer_beerid, rating = review_overall)

#Add unique id for reviewers
beer_ratings$userId <- group_indices_(beer_ratings, .dots = 'reviewer') 

#Create ratings matrix
rating_matrix <- dcast(beer_ratings, userId ~ beerId, value.var = 'rating')
rating_matrix <- as.matrix(rating_matrix)
rating_matrix <- as(rating_matrix, 'realRatingMatrix')

#UBCF Model
recommender_model <- Recommender(rating_matrix, method = 'UBCF', param=list(method='Cosine',nn=10))

#Predict top 5 beers for first 10 users
recom <- predict(recommender_model, rating_matrix[1:10], n=5)

#Return top recommendations as a list 
recom_list<- as(recom,'list')
recom_list

The above code will result in:

[[1]]
[1] "48542" "2042"  "6"     "10"    "19"   

[[2]]
[1] "10277" "2042"  "6"     "10"    "19"   

[[3]]
[1] "10277" "48542" "6"     "10"    "19"   

[[4]]
[1] "10277" "48542" "2042"  "6"     "10"   

[[5]]
[1] "10277" "48542" "2042"  "6"     "10"   

[[6]]
[1] "10277" "48542" "2042"  "6"     "10" 

Converting the data frame to a matrix then realRatingMatrix without casting first into a table results in the user's recommendation as:

 `886093`
 `character(0)`

Using the 'dcast' function first then converting the data frame into a matrix and removing the first column, then into a realRatingMatrix returns the same predictions for almost every user:

[[1]]
[1] "6"  "7"  "10" "12" "19"

[[2]]
[1] "6"  "7"  "10" "12" "19"

[[3]]
[1] "6"  "7"  "10" "12" "19" 

Any help is greatly appreciated.

0

There are 0 best solutions below