CC This work is licensed under the Creative Commons Attribution 4.0 International License. For questions please contact Michael Hahsler.

library("recommenderlab")
## Loading required package: Matrix
## Loading required package: arules
## 
## Attaching package: 'arules'
## 
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
## 
## Loading required package: proxy
## 
## Attaching package: 'proxy'
## 
## The following object is masked from 'package:Matrix':
## 
##     as.matrix
## 
## The following objects are masked from 'package:stats':
## 
##     as.dist, dist
## 
## The following object is masked from 'package:base':
## 
##     as.matrix
## 
## Loading required package: registry

Rating Data

data("Jester5k")
Jester5k
## 5000 x 100 rating matrix of class 'realRatingMatrix' with 362106 ratings.
train <- Jester5k[1:4000,]
test <- Jester5k[4001:5000,]

recommenderRegistry$get_entry("UBCF", dataType="realRatingMatrix")
## Recommender method: UBCF
## Description: Recommender based on user-based collaborative filtering (real data).
## Parameters:
##   method nn sample normalize minRating
## 1 cosine 25  FALSE    center        NA

Create Recommender (dafault settings)

recom <- Recommender(train, method = "UBCF")
recom
## Recommender of type 'UBCF' for 'realRatingMatrix' 
## learned using 4000 users.

create top-N list

pred <- predict(recom, test[1:5,], n = 5)

getList(pred)
## $u3464
## [1] "j34" "j28" "j47" "j52" "j6" 
## 
## $u15005
## [1] "j72" "j78" "j71" "j76" "j84"
## 
## $u9658
## [1] "j25" "j70" "j59" "j23" "j30"
## 
## $u13396
## character(0)
## 
## $u9565
## [1] "j76" "j81" "j83" "j85" "j82"
#getRatings(pred) ### will work in next release
pred@ratings
## $u3464
## [1] 4.853139 4.572573 4.008094 3.845098 3.475339
## 
## $u15005
## [1] -2.745883 -2.899130 -2.913312 -2.914370 -3.017364
## 
## $u9658
## [1] 9.704629 7.922162 7.822445 7.207393 6.997372
## 
## $u13396
## numeric(0)
## 
## $u9565
## [1] 8.525541 8.339878 8.247198 8.148084 8.049773
getList(bestN(pred, 3))[[1]]
## [1] "j34" "j28" "j47"
cat(JesterJokes[getList(bestN(pred, 3))[[1]]], sep = "\n\n")
## Out in the backwoods of some midwestern state, little Johnny arrives at school an hour late. Teacher: "Why are you so late, John?" Johny : "My big brother got shot in the ass." (the teacher corrects his speech) Teacher: "Rectum." Johnny : "Wrecked him!? Hell, It damn near killed him!"
## 
## A mechanical, electrical and a software engineer from Microsoft were driving through the desert when the car broke down. The mechanical engineer said "It seems to be a problem with the fuel injection system, why don't we pop the hood and I'll take a look at it." To which the electrical engineer replied, "No I think it's just a loose ground wire, I'll get out and take a look." Then, the Microsoft engineer jumps in. "No, no, no. If we just close up all the windows, get out, wait a few minutes, get back in, and then reopen the windows everything will work fine."
## 
## There was an engineer who had an exceptional gift for fixing all things mechanical. After serving his company loyally for over 30 years, he happily retired. Several years later the company contacted him regarding a seemingly impossible problem they were having with one of their multi-million dollar machines. They had tried everything and everyone else to get the machine fixed, but to no avail. In desperation, they called on the retired engineer who had solved so many of their problems in the past. The engineer reluctantly took the challenge. He spent a day studying the huge machine. At the end of the day, he marked a small "x" in chalk on a particular component of the machine and proudly stated, "This is where your problem is". The part was replaced and the machine worked perfectly again. The company received a bill for $50,000 from the engineer for his service.They demanded an itemized accounting of his charges. The engineer responded briefly: One chalk mark $1 Knowing where to put it $49,999 It was paid in full and the engineer retired again in peace.

predict Ratings

pred_ratings <- predict(recom, test[1:5,], type = "ratingMatrix")
getRatingMatrix(pred_ratings)
## 5 x 100 sparse Matrix of class "dgCMatrix"
##    [[ suppressing 100 column names 'j1', 'j2', 'j3' ... ]]
##                                                                           
## u3464   1.564452  2.157592  2.098768  1.809539  1.55  3.475339  3.25 -7.62
## u15005 -6.070000 -7.040000  8.640000 -6.840000 -8.88 -6.840000  0.53  5.10
## u9658   6.473188  6.560736  6.372235  6.398629  7.67 -1.550000  3.25  7.77
## u13396  3.830000  2.620000 -8.790000 -5.050000 -8.16  6.410000  4.13  5.19
## u9565   4.950000  2.960000  4.610000  3.250000  1.60  3.500000 -0.49  1.75
##                                                                          
## u3464   1.921760  2.838335 2.77  1.46 -5.24  4.37 -6.17 -4.37  3.30 -2.38
## u15005 -6.840000 -5.730000 8.64  6.99 -8.88 -5.92 -8.88 -8.88  5.29 -8.83
## u9658   6.092173 -8.590000 8.64  6.75 -9.47  9.22 -0.83 -6.36  3.35 -1.21
## u13396 -7.520000  4.850000 7.09 -9.08  3.98 -8.98 -5.73  0.34  3.98  6.94
## u9565   3.590000  3.400000 3.54  2.04 -0.15  3.01  3.59  3.30 -3.20  3.30
##                                                                       
## u3464  -0.49 -3.93  1.31  2.28 2.276227  2.043999  3.060000  5.29 2.52
## u15005  7.09  2.23 -9.32 -5.78 5.680000 -6.890000 -6.360000 -5.78 2.23
## u9658   1.94  8.93  7.28  6.80 7.207393  6.053931  9.704629  8.79 8.11
## u13396  5.63  6.02  3.59  3.64 3.400000 -9.270000  3.640000  5.68 6.84
## u9565   0.97 -2.38  2.18  3.98 2.820000  3.450000  3.640000  3.54 1.94
##                                                                          
## u3464   4.572573 -1.31 2.573304 5.73 8.74  1.664914  4.853139  2.14 -1.94
## u15005 -6.800000  6.80 4.470000 6.02 1.84  7.720000 -5.680000  6.80 -3.35
## u9658   5.000000  7.72 6.997372 9.13 4.51  6.332706  1.750000  8.20  5.15
## u13396  4.710000  7.18 5.190000 3.69 4.51 -5.150000  3.250000 -0.24  7.52
## u9565   3.010000  1.94 3.880000 0.97 0.68  3.160000  3.690000  4.13  2.77
##                                                                          
## u3464   1.703431  2.52 6.07  1.55  1.2433  9.32  1.458136  1.127051  8.64
## u15005 -7.860000 -5.83 5.53 -6.75 -6.5000  8.74 -6.840000 -5.920000 -6.41
## u9658   6.649497  5.73 8.35  9.13  8.4000  7.62  6.717492  6.085366  6.12
## u13396 -3.980000  3.83 2.48 -7.23  1.6500 -0.78 -9.710000 -9.660000 -4.56
## u9565   3.980000  2.77 2.23  1.99  3.6900  1.50  3.690000  3.830000  3.59
##                                                                     
## u3464   4.85  4.008094 -3.79 -2.18  4.32  3.35  3.845098  5.73  5.39
## u15005 -5.34 -6.940000 -5.87  6.17 -4.71 -6.75 -7.090000 -6.31  4.95
## u9658   6.80  7.330000  7.72  8.93  8.16  4.17  8.450000  4.17  8.50
## u13396 -7.33  2.230000 -0.34  8.88  7.09 -6.55 -9.030000  5.87 -8.64
## u9565   3.79  1.700000  1.46  1.55  5.53  3.35  3.160000  1.99  3.98
##                                                                      
## u3464   3.313509  4.03  1.366958  0.5518497 -2.960000  1.208349  3.25
## u15005 -7.090000 -6.50 -7.570000 -7.4300000 -6.360000 -6.890000 -6.60
## u9658   6.334425  8.79  6.199366  5.6851019  7.822445  6.597614  8.59
## u13396 -0.580000  2.48 -9.710000 -9.5100000 -5.920000 -9.030000  6.26
## u9565  -1.750000 -0.83  2.180000  3.3000000  1.410000  3.640000  0.73
##                                                                  
## u3464   3.64  1.526831  1.334495  7.62  5.78  1.678582 2.09 -2.33
## u15005 -7.96 -7.090000 -6.500000  5.24 -5.34 -9.610000 4.51 -5.87
## u9658   8.45  8.500000  6.138316  5.73  8.83  5.729655 6.84  8.98
## u13396 -4.76  0.000000 -6.700000 -6.50  8.88  6.170000 6.02 -8.45
## u9565   1.17  1.550000 -0.190000  2.67  0.83  3.590000 0.05  0.83
##                                                                   
## u3464  -2.040000  1.632391  1.925808  1.889215  1.209510  2.179797
## u15005 -7.090000 -2.913312 -2.745883 -3.744061 -3.733136 -3.205597
## u9658   7.922162  5.573820  6.030881  5.856879  6.067306  4.510000
## u13396 -7.280000 -9.470000  3.160000 -7.820000 -7.670000 -7.910000
## u9565   3.250000  7.538621  7.947107  7.107528  7.974124  7.298583
##                                                                   
## u3464   1.632391  1.745424  1.619547  2.076467  1.632391  2.060299
## u15005 -2.914370 -3.406095 -2.899130 -3.483863 -3.308265 -3.276663
## u9658   5.887348  6.631955  6.170223  6.005450  5.707547  6.300010
## u13396  8.200000  4.420000  5.630000 -1.890000 -5.100000  6.500000
## u9565   8.525541  7.545486  7.345188  7.212815  0.000000  8.339878
##                                                                   
## u3464   1.689041  1.689041 -2.430000  1.652407  1.286336  2.073077
## u15005 -3.229821 -3.068475 -3.017364 -5.000000 -3.668309 -3.125761
## u9658   5.778029  8.980000  6.001817  9.270000  5.707547  5.838178
## u13396  6.550000  1.750000  8.740000 -9.370000  4.320000 -8.590000
## u9565   8.049773  8.247198  7.508332  8.148084  4.080000 -0.440000
##                                                                  
## u3464   1.598965  1.851064  1.691181  1.866726  1.623327 1.901210
## u15005 -3.081833 -3.101469 -3.273029 -3.985427 -3.262399 2.820000
## u9658   6.143330  5.707547  5.884862  3.500000  6.338373 5.763234
## u13396  7.380000  5.190000 -9.220000  5.390000  2.670000 7.040000
## u9565   3.880000  6.990000  3.200000  6.020000  4.850000 4.850000
##                                                                   
## u3464   1.623327  1.804972  1.657523  1.785432  1.568557 -1.650000
## u15005 -3.809483 -4.375060 -5.870000 -3.710410 -3.675827 -3.815907
## u9658   6.281971  6.130597  6.645124  6.699071  6.221249  5.917689
## u13396  4.170000  2.860000  2.280000  3.740000 -0.150000 -7.280000
## u9565   4.220000  4.080000  4.370000  3.350000  4.900000  4.420000
##                 
## u3464   1.953440
## u15005 -3.430101
## u9658   5.778637
## u13396 -8.060000
## u9565   4.710000

Use different parameters

recommenderRegistry$get_entry("UBCF", dataType="realRatingMatrix")
## Recommender method: UBCF
## Description: Recommender based on user-based collaborative filtering (real data).
## Parameters:
##   method nn sample normalize minRating
## 1 cosine 25  FALSE    center        NA
recom2 <- Recommender(train, method = "UBCF",
  parameter = list(method = "Euclidean", nn = 10, normalize = "Z-score"))
recom2
## Recommender of type 'UBCF' for 'realRatingMatrix' 
## learned using 4000 users.
pred2 <- predict(recom2, test[1:5,], n = 5)

# Compare predictions to default recommender
getList(pred2)[[1]]
## [1] "j6"  "j34" "j47" "j52" "j55"
getList(pred)[[1]]
## [1] "j34" "j28" "j47" "j52" "j6"
# Compare model size
object.size(recom2)
## 4257400 bytes
object.size(recom)
## 4001200 bytes
# Compare prediction speed
system.time(predict(recom2, test, n = 5))
##    user  system elapsed 
##   6.068   0.028   6.100
system.time(predict(recom, test, n = 5))
##    user  system elapsed 
##   5.104   0.012   5.120