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("SVD", dataType="realRatingMatrix")
## Recommender method: SVD
## Description: Recommender based on SVD approximation with column-mean imputation (real data).
## Parameters:
##    k maxiter normalize minRating
## 1 10     100    center        NA

Create Recommender (dafault settings)

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

create top-N list

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

getList(pred)
## $u3464
## [1] "j34" "j2"  "j52" "j98" "j47"
## 
## $u15005
## [1] "j89"  "j83"  "j100" "j72"  "j78" 
## 
## $u9658
## [1] "j25"  "j30"  "j70"  "j2"   "j100"
## 
## $u13396
## character(0)
## 
## $u9565
## [1] "j81" "j72" "j76" "j83" "j82"
#getRatings(pred) ### will work in next release
pred@ratings
## $u3464
## [1] 4.987662 4.131557 4.084587 3.244464 3.168171
## 
## $u15005
## [1] -1.264037 -1.889757 -1.956735 -1.961419 -2.274325
## 
## $u9658
## [1] 9.619840 8.701800 8.467550 8.312018 8.131494
## 
## $u13396
## numeric(0)
## 
## $u9565
## [1] 5.192564 5.013385 4.911481 4.879027 4.798041
getList(bestN(pred, 3))[[1]]
## [1] "j34" "j2"  "j52"
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!"
## 
## This couple had an excellent relationship going until one day he came home from work to find his girlfriend packing. He asked her why she was leaving him and she told him that she had heard awful things about him. "What could they possibly have said to make you move out?" "They told me that you were a pedophile." He replied, "That's an awfully big word for a ten year old."
## 
## Q: What do Monica Lewinsky and Bob Dole have in common? A: They were both upset when Bill finished first.

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.575395  4.131557  1.552707  2.645210  1.55  3.150772  3.25 -7.62
## u15005 -6.070000 -7.040000  8.640000 -6.840000 -8.88 -6.840000  0.53  5.10
## u9658   6.524396  8.312018  7.879424  7.815046  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.691941  1.518236 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   5.059466 -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 1.606888  1.221513  3.06000  5.29 2.52
## u15005  7.09  2.23 -9.32 -5.78 5.680000 -6.890000 -6.36000 -5.78 2.23
## u9658   1.94  8.93  7.28  6.80 7.345673  4.867081  9.61984  8.79 8.11
## u13396  5.63  6.02  3.59  3.64 3.400000 -9.270000  3.64000  5.68 6.84
## u9565   0.97 -2.38  2.18  3.98 2.820000  3.450000  3.64000  3.54 1.94
##                                                                         
## u3464   2.742261 -1.31 1.98254 5.73 8.74  1.507772  4.987662  2.14 -1.94
## u15005 -6.800000  6.80 4.47000 6.02 1.84  7.720000 -5.680000  6.80 -3.35
## u9658   5.000000  7.72 8.70180 9.13 4.51  7.863319  1.750000  8.20  5.15
## u13396  4.710000  7.18 5.19000 3.69 4.51 -5.150000  3.250000 -0.24  7.52
## u9565   3.010000  1.94 3.88000 0.97 0.68  3.160000  3.690000  4.13  2.77
##                                                                       
## u3464   0.9412581  2.52 6.07  1.55  2.77584  9.32  1.862515  0.7503074
## u15005 -7.8600000 -5.83 5.53 -6.75 -6.50000  8.74 -6.840000 -5.9200000
## u9658   5.3474620  5.73 8.35  9.13  8.40000  7.62  6.562275  2.7308223
## u13396 -3.9800000  3.83 2.48 -7.23  1.65000 -0.78 -9.710000 -9.6600000
## u9565   3.9800000  2.77 2.23  1.99  3.69000  1.50  3.690000  3.8300000
##                                                                           
## u3464   8.64  4.85  3.168171 -3.79 -2.18  4.32  3.35  4.084587  5.73  5.39
## u15005 -6.41 -5.34 -6.940000 -5.87  6.17 -4.71 -6.75 -7.090000 -6.31  4.95
## u9658   6.12  6.80  7.330000  7.72  8.93  8.16  4.17  8.450000  4.17  8.50
## u13396 -4.56 -7.33  2.230000 -0.34  8.88  7.09 -6.55 -9.030000  5.87 -8.64
## u9565   3.59  3.79  1.700000  1.46  1.55  5.53  3.35  3.160000  1.99  3.98
##                                                                        
## u3464   1.814523  4.03  0.6373542  0.5350956 -2.960000  0.3342928  3.25
## u15005 -7.090000 -6.50 -7.5700000 -7.4300000 -6.360000 -6.8900000 -6.60
## u9658   6.728967  8.79  2.9093032  3.2882517  7.318266  6.4871276  8.59
## u13396 -0.580000  2.48 -9.7100000 -9.5100000 -5.920000 -9.0300000  6.26
## u9565  -1.750000 -0.83  2.1800000  3.3000000  1.410000  3.6400000  0.73
##                                                                   
## u3464   3.64  1.711716  0.3297461  7.62  5.78  2.132053 2.09 -2.33
## u15005 -7.96 -7.090000 -6.5000000  5.24 -5.34 -9.610000 4.51 -5.87
## u9658   8.45  8.500000  6.7822988  5.73  8.83  5.310209 6.84  8.98
## u13396 -4.76  0.000000 -6.7000000 -6.50  8.88  6.170000 6.02 -8.45
## u9565   1.17  1.550000 -0.1900000  2.67  0.83  3.590000 0.05  0.83
##                                                                     
## u3464  -2.04000  0.7621772  2.441439  1.639728  0.5672966  0.9737543
## u15005 -7.09000 -3.9317521 -1.961419 -2.552419 -4.2734664 -3.1321557
## u9658   8.46755  5.9282419  7.722796  6.714268  5.1443323  4.5100000
## u13396 -7.28000 -9.4700000  3.160000 -7.820000 -7.6700000 -7.9100000
## u9565   3.25000  3.9737356  5.013385  4.325204  3.8453334  3.8168162
##                                                                    
## u3464   1.965532  0.9837515  1.545749  1.205759  1.920576  2.068459
## u15005 -2.393167 -2.8708470 -2.274325 -3.812951 -2.980385 -3.394204
## u9658   7.075552  6.0722171  6.861088  5.837776  6.863643  6.503956
## u13396  8.200000  4.4200000  5.630000 -1.890000 -5.100000  6.500000
## u9565   4.911481  4.2715507  4.633307  4.182007  0.000000  5.192564
##                                                                     
## u3464   0.7274662  2.411405 -2.430000  1.527955  0.4144934  1.781758
## u15005 -3.2229261 -1.889757 -3.189310 -5.000000 -4.9507582 -3.819142
## u9658   6.0758022  8.980000  6.250916  9.270000  4.5137315  6.104477
## u13396  6.5500000  1.750000  8.740000 -9.370000  4.3200000 -8.590000
## u9565   4.7980412  4.879027  4.720540  4.688567  4.0800000 -0.440000
##                                                                   
## u3464   1.079720  2.032852  0.5596207  1.452210  1.290285 1.704945
## u15005 -3.802293 -1.264037 -2.8557021 -3.655446 -4.333840 2.820000
## u9658   5.898747  7.797081  6.5197454  3.500000  5.795261 7.016935
## u13396  7.380000  5.190000 -9.2200000  5.390000  2.670000 7.040000
## u9565   3.880000  6.990000  3.2000000  6.020000  4.850000 4.850000
##                                                                    
## u3464   0.7910223  1.053789  1.260356  1.484952  3.244464 -1.650000
## u15005 -5.4850986 -5.245451 -5.870000 -3.374770 -2.437752 -3.909234
## u9658   4.7744555  5.347375  5.987673  6.517023  8.096510  6.122270
## u13396  4.1700000  2.860000  2.280000  3.740000 -0.150000 -7.280000
## u9565   4.2200000  4.080000  4.370000  3.350000  4.900000  4.420000
##                 
## u3464   2.572028
## u15005 -1.956735
## u9658   8.131494
## u13396 -8.060000
## u9565   4.710000

Use different parameters

recommenderRegistry$get_entry("SVD", dataType="realRatingMatrix")
## Recommender method: SVD
## Description: Recommender based on SVD approximation with column-mean imputation (real data).
## Parameters:
##    k maxiter normalize minRating
## 1 10     100    center        NA
recom2 <- Recommender(train, method = "SVD",
  parameter = list(k = 20, normalize = "Z-score"))
recom2
## Recommender of type 'SVD' for 'realRatingMatrix' 
## learned using 4000 users.
pred2 <- predict(recom2, test[1:5,], n = 5)

# Compare predictions to default recommender
getList(pred2)[[1]]
## [1] "j34" "j52" "j6"  "j4"  "j67"
getList(pred)[[1]]
## [1] "j34" "j2"  "j52" "j98" "j47"
# Compare model size
object.size(recom2)
## 702504 bytes
object.size(recom)
## 374472 bytes
# Compare prediction speed
system.time(predict(recom2, test, n = 5))
##    user  system elapsed 
##   0.188   0.004   0.190
system.time(predict(recom, test, n = 5))
##    user  system elapsed 
##   0.152   0.008   0.161