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("IBCF", dataType="realRatingMatrix")
## Recommender method: IBCF
## Description: Recommender based on item-based collaborative filtering (real data).
## Parameters:
##    k method normalize normalize_sim_matrix alpha na_as_zero minRating
## 1 30 Cosine    center                FALSE   0.5      FALSE        NA

Create Recommender (dafault settings)

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

create top-N list

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

getList(pred)
## $u3464
## [1] "j98" "j34" "j47" "j6"  "j52"
## 
## $u15005
## [1] "j89" "j77" "j72" "j71" "j83"
## 
## $u9658
## [1] "j84" "j71" "j33" "j99" "j89"
## 
## $u13396
## character(0)
## 
## $u9565
## [1] "j77" "j78" "j82" "j84" "j73"
#getRatings(pred) ### will work in next release
pred@ratings
## $u3464
## [1] 5.237681 5.062852 5.050949 5.001605 4.985454
## 
## $u15005
## [1] -2.901379 -4.030919 -4.702520 -4.784793 -5.159521
## 
## $u9658
## [1] 13.32916 13.28768 13.26216 13.20423 12.99422
## 
## $u13396
## numeric(0)
## 
## $u9565
## [1] 6.861442 6.698817 6.562698 6.545676 6.523344
getList(bestN(pred, 3))[[1]]
## [1] "j98" "j34" "j47"
cat(JesterJokes[getList(bestN(pred, 3))[[1]]], sep = "\n\n")
## Age and Womanhood 1. Between the ages of 13 and 18 ... She is like Africa, virgin and unexplored. 2. Between the ages of 19 and 35 ... She is like Asia, hot and exotic. 3. Between the ages of 36 and 45 ... She is like America, fully explored, breathtakingly beautiful,and free with her resources. 4. Between the ages of 46 and 56 ... She is like Europe, exhausted but still has points of interest. 5. After 56 she is like Australia ... Everybody knows it's down there, but who gives a damn?
## 
## 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!"
## 
## 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   3.089325  4.534542  3.624939  2.993221  1.55  5.001605  3.25 -7.62
## u15005 -6.070000 -7.040000  8.640000 -6.840000 -8.88 -6.840000  0.53  5.10
## u9658  12.238895 12.409928 11.219492 12.498591  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   3.006894  3.999626 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  10.948340 -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  3.852085  1.100132  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 11.064622  9.423984 12.28477  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   4.82445 -1.31  4.039064 5.73 8.74  2.905857  5.062852  2.14 -1.94
## u15005 -6.80000  6.80  4.470000 6.02 1.84  7.720000 -5.680000  6.80 -3.35
## u9658   5.00000  7.72 12.416360 9.13 4.51 13.262163  1.750000  8.20  5.15
## u13396  4.71000  7.18  5.190000 3.69 4.51 -5.150000  3.250000 -0.24  7.52
## u9565   3.01000  1.94  3.880000 0.97 0.68  3.160000  3.690000  4.13  2.77
##                                                                       
## u3464   0.9038603  2.52 6.07  1.55  4.740731  9.32  1.711007 -0.783195
## u15005 -7.8600000 -5.83 5.53 -6.75 -6.500000  8.74 -6.840000 -5.920000
## u9658   8.5404190  5.73 8.35  9.13  8.400000  7.62 11.181921  5.514663
## u13396 -3.9800000  3.83 2.48 -7.23  1.650000 -0.78 -9.710000 -9.660000
## u9565   3.9800000  2.77 2.23  1.99  3.690000  1.50  3.690000  3.830000
##                                                                           
## u3464   8.64  4.85  5.050949 -3.79 -2.18  4.32  3.35  4.985454  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   3.736245  4.03 -1.241613 -0.3397463 -2.96000  2.452475  3.25  3.64
## u15005 -7.090000 -6.50 -7.570000 -7.4300000 -6.36000 -6.890000 -6.60 -7.96
## u9658  11.266081  8.79  7.236418  7.0886913 11.85706 11.350662  8.59  8.45
## u13396 -0.580000  2.48 -9.710000 -9.5100000 -5.92000 -9.030000  6.26 -4.76
## u9565  -1.750000 -0.83  2.180000  3.3000000  1.41000  3.640000  0.73  1.17
##                                                                     
## u3464   3.640689  2.261364  7.62  5.78  1.292979 2.09 -2.33 -2.04000
## u15005 -7.090000 -6.500000  5.24 -5.34 -9.610000 4.51 -5.87 -7.09000
## u9658   8.500000 11.426472  5.73  8.83  8.422399 6.84  8.98 11.83836
## u13396  0.000000 -6.700000 -6.50  8.88  6.170000 6.02 -8.45 -7.28000
## u9565   1.550000 -0.190000  2.67  0.83  3.590000 0.05  0.83  3.25000
##                                                                      
## u3464  -0.9793187  3.540444  2.766418  0.6388934  0.8865985  2.903134
## u15005 -4.7847931 -4.702520 -5.423629 -7.9739187 -7.1166060 -6.693464
## u9658  13.2876802 12.609780 12.024250 12.0331042  4.5100000 11.865466
## u13396 -9.4700000  3.160000 -7.820000 -7.6700000 -7.9100000  8.200000
## u9565   6.0366419  6.155122  6.523344  6.2147661  6.4654647  6.295963
##                                                                      
## u3464  -0.4244576  1.175449  0.7523708  4.300717  3.515891 -0.5022529
## u15005 -4.0309188 -6.811899 -6.2618063 -5.809076 -5.356689 -7.2760676
## u9658  12.2370239 11.673327 12.3144728 12.209427 12.243114 11.5182246
## u13396  4.4200000  5.630000 -1.8900000 -5.100000  6.500000  6.5500000
## u9565   6.8614421  6.698817  6.4649901  0.000000  6.153512  6.5626980
##                                                                    
## u3464   3.582286 -2.430000  3.592425 -0.6983043  2.534275  1.355138
## u15005 -5.159521 -5.885049 -5.000000 -8.5230443 -7.376892 -6.125639
## u9658   8.980000 13.329156  9.270000  9.5377747 12.103396 12.254169
## u13396  1.750000  8.740000 -9.370000  4.3200000 -8.590000  7.380000
## u9565   6.432629  6.545676  6.242842  4.0800000 -0.440000  3.880000
##                                                                      
## u3464   3.175537 -0.6088928  1.402645 -0.4327797  3.570511  0.5683921
## u15005 -2.901379 -6.6688181 -5.779706 -7.0437244  2.820000 -8.4757520
## u9658  12.994221 12.0379969  3.500000 11.1497370 12.353126  9.4095352
## u13396  5.190000 -9.2200000  5.390000  2.6700000  7.040000  4.1700000
## u9565   6.990000  3.2000000  6.020000  4.8500000  4.850000  4.2200000
##                                                                    
## u3464   0.5791033  1.232909  3.031462  5.237681 -1.650000  4.732291
## u15005 -5.3847655 -5.870000 -5.566484 -5.459798 -7.012745 -5.797776
## u9658  12.6531162 11.761082 11.913439 12.860405 13.204227 12.913517
## u13396  2.8600000  2.280000  3.740000 -0.150000 -7.280000 -8.060000
## u9565   4.0800000  4.370000  3.350000  4.900000  4.420000  4.710000

Use different parameters

recommenderRegistry$get_entry("IBCF", dataType="realRatingMatrix")
## Recommender method: IBCF
## Description: Recommender based on item-based collaborative filtering (real data).
## Parameters:
##    k method normalize normalize_sim_matrix alpha na_as_zero minRating
## 1 30 Cosine    center                FALSE   0.5      FALSE        NA
recom2 <- Recommender(train, method = "IBCF",
  parameter = list(method = "Euclidean", k = 5, normalize = "Z-score"))
recom2
## Recommender of type 'IBCF' for 'realRatingMatrix' 
## learned using 4000 users.
pred2 <- predict(recom2, test[1:5,], n = 5)

# Compare predictions to default recommender
getList(pred2)[[1]]
## [1] "j83" "j98" "j81" "j52" "j89"
getList(pred)[[1]]
## [1] "j98" "j34" "j47" "j6"  "j52"
# Compare model size
object.size(recom2)
## 49728 bytes
object.size(recom)
## 79720 bytes
# Compare prediction speed
system.time(predict(recom2, test, n = 5))
##    user  system elapsed 
##   0.196   0.008   0.204
system.time(predict(recom, test, n = 5))
##    user  system elapsed 
##   0.188   0.000   0.188