-
Notifications
You must be signed in to change notification settings - Fork 1
/
01_recommend.R
135 lines (101 loc) · 5.04 KB
/
01_recommend.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
library(recommenderlab)
library(tidyverse)
load("data/BookRatings.RData")
# min(rowCounts(BookRatingsMatrix)) # 1
# max(rowCounts(BookRatingsMatrix)) # 101
set.seed(202205)
eval_books <- evaluationScheme(data = BookRatingsMatrix,
method = "cross-validation",
k = 10,
given = 1,
goodRating = 7)
eval_books
# valuation scheme with 1 items given
# Method: ‘cross-validation’ with 10 run(s).
# Good ratings: >=7.000000
# Data set: 2531 x 5554 rating matrix of class ‘realRatingMatrix’ with 18520 ratings.
train_books <- getData(eval_books, "train")
known_books <- getData(eval_books, "known")
unknown_books <- getData(eval_books, "unknown")
# Randomly chosen items ---------------------------------------------------
random <- train_books %>% Recommender(method = "RANDOM")
random_eval <- random %>% predict(known_books, type = "ratings") %>%
calcPredictionAccuracy(unknown_books)
print(random_eval)
# Popular items -----------------------------------------------------------
pop <- train_books %>% Recommender(method = "POPULAR")
pop_eval <- pop %>% predict(known_books, type = "ratings") %>%
calcPredictionAccuracy(unknown_books)
print(pop_eval)
# Singular Value Decomposition based collaborative filtering --------------
svd <- train_books %>% Recommender(method = "SVD",
parameter = list(k = 10,
maxiter = 200,
normalize = NULL,
verbose = TRUE))
svd_eval <- svd %>% predict(known_books, type = "ratings") %>%
calcPredictionAccuracy(unknown_books)
print(svd_eval)
# Funk SVD ----------------------------------------------------------------
svdf <- train_books %>% Recommender(method = "SVDF",
parameter = list(k = 10,
maxiter = 200,
normalize = NULL,
verbose = TRUE))
svdf_eval <- svdf %>% predict(known_books, type = "ratings") %>%
calcPredictionAccuracy(unknown_books)
print(svdf_eval)
# Item-based collaborative filtering --------------------------------------
ibcf <- train_books %>% Recommender(method = "IBCF",
parameter = list(k = 10,
method = "Cosine",
normalize = NULL,
normalize_sim_matrix = FALSE,
alpha = 0.5,
na_as_zero = FALSE,
verbose = TRUE))
ibcf_eval <- ibcf %>% predict(known_books, type = "ratings") %>%
calcPredictionAccuracy(unknown_books)
print(ibcf_eval)
# User-based collaborative filtering --------------------------------------
# ubcf <- train_books %>% Recommender(method = "UBCF", param = list(method = "Cosine", normalize = "Center"))
# ubcf_eval <- ubcf %>% predict(known_books, type = "ratings") %>%
# calcPredictionAccuracy(unknown_books)
# print(ubcf_eval)
# Alternating least squares -----------------------------------------------
# als <- train_books %>% Recommender(method = "ALS")
# als_eval <- als %>% predict(known_books, type = "ratings") %>%
# calcPredictionAccuracy(unknown_books)
# print(ibcf_eval)
# matrix factorization ----------------------------------------------------
libmf <- train_books %>% Recommender(method = "LIBMF")
libmf_eval <- libmf %>% predict(known_books, type = "ratings") %>%
calcPredictionAccuracy(unknown_books)
print(libmf_eval)
rbind(random_eval, pop_eval,
svd_eval, svdf_eval,
ibcf_eval, libmf_eval) %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "method") %>%
mutate(across(RMSE:MAE, round, 2),
method = recode(method,
random_eval = "Random Evaluation",
pop_eval = "Popular Evaluation",
svd_eval = "Singular Value Decomposition",
svdf_eval = "Funk Singular Value Decomposition",
ibcf_eval = "Item-Based Collaborative Filtering",
libmf_eval = "Matrix factorization")) %>%
ggplot(aes(x = method, y = RMSE)) +
geom_col() +
labs(x = "Method", y = "RMSE",
title = "Model Evaluation Metrics",
subtitle = "Recommender Performance") +
theme_minimal() + theme(axis.text.x = element_text(angle = 20))
recos_pop <- pop %>% predict(known_books, n = 5)
as(recos_pop, "list") %>% head(4)
recos_ran <- random %>% predict(known_books, n = 5)
as(recos_ran, "list") %>% head(4)
recos_svdf <- svdf %>% predict(known_books, n = 5)
as(recos_svdf, "list") %>% head(4)
recos_ibcf <- ibcf %>% predict(known_books, n = 5)
as(recos_ibcf, "list") %>% head(4)