Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
data_analysis:recommendation_bpr [2020/04/29 11:29] – prgram | data_analysis:recommendation_bpr [2025/07/07 14:12] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 36: | Line 36: | ||
* $S \subseteq U \times I$ : implicit feedback | * $S \subseteq U \times I$ : implicit feedback | ||
* $>_u \subset I^2$ : personalied total ranking of all items | * $>_u \subset I^2$ : personalied total ranking of all items | ||
- | > properties of total order: | + | > properties of total order: |
- | | + | > (totality) : $\text{if } i \neq j \text{, then } i >_u j \text{ or } j >_u i$ |
- | | + | > (antisymmetry) : $\text{if } i>_uj \text{ and } j>_ui \text{, then } i = j$ |
- | | + | > (transitivity) : $\text{if } i>_uj \text{ and } j>_uk \text{, then } i>_u k$ |
* $I_u^+:=\{i \in I : (u,i) \in S\}$, $U_i^+:=\{u \in U : (u,i) \in S\}$ | * $I_u^+:=\{i \in I : (u,i) \in S\}$, $U_i^+:=\{u \in U : (u,i) \in S\}$ | ||
Line 202: | Line 202: | ||
 |  | ||
+ | {{: | ||
+ | {{: | ||
###### remove rating scores, repeated 5 times, $\lambda = 0.0025$, $\alpha = 0.05$ | ###### remove rating scores, repeated 5 times, $\lambda = 0.0025$, $\alpha = 0.05$ | ||
--- | --- | ||
Line 208: | Line 209: | ||
# END | # END | ||
+ | |||
+ | |||
+ | ==== Code ==== | ||
+ | < | ||
+ | # | ||
+ | |||
+ | |||
+ | test_index< | ||
+ | index_test< | ||
+ | for (i in 1: | ||
+ | if(na) indeX_i <- which(!is.na(data[i, | ||
+ | else index_i< | ||
+ | | ||
+ | index_test[i]< | ||
+ | } | ||
+ | test_index <- index_test | ||
+ | } | ||
+ | |||
+ | |||
+ | train <- function (data, index_test, na=TRUE) { | ||
+ | |||
+ | train_data< | ||
+ | for (i in 1: | ||
+ | train_data[i, | ||
+ | } | ||
+ | train_data[is.na(train_data)]< | ||
+ | train< | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | calc_AUC <- function(data, | ||
+ | AUC_u <- 0 | ||
+ | for (i in 1: | ||
+ | i=1 | ||
+ | if(na) index_j< | ||
+ | else index_j< | ||
+ | | ||
+ | AUC_u <- AUC_u + 1/ | ||
+ | } | ||
+ | | ||
+ | calc_AUC <- AUC_u / dim(data)[1] | ||
+ | } | ||
+ | |||
+ | ############################################ | ||
+ | ############################################ | ||
+ | ############################################ | ||
+ | ############################################ | ||
+ | ############################################ | ||
+ | |||
+ | |||
+ | # check data | ||
+ | which(apply(is.na(data2), | ||
+ | |||
+ | which(apply(is.na(data2), | ||
+ | sum(!is.na(data2)) | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ############################################ | ||
+ | ############################################ | ||
+ | |||
+ | ml100k_ori< | ||
+ | ml100k[ml100k> | ||
+ | |||
+ | index_test< | ||
+ | data2_train< | ||
+ | ############################################ | ||
+ | ############################################ | ||
+ | |||
+ | ############################################ | ||
+ | ############################################ | ||
+ | ####BPR | ||
+ | library(" | ||
+ | setwd(" | ||
+ | source(" | ||
+ | |||
+ | |||
+ | showDeltaError() | ||
+ | # | ||
+ | |||
+ | ratings< | ||
+ | |||
+ | |||
+ | |||
+ | AUC_total< | ||
+ | for (i in 1:10) { | ||
+ | AUC<-0 | ||
+ | for( j in 1:5){ | ||
+ | model_bpr< | ||
+ | pred_bpr< | ||
+ | | ||
+ | AUC< | ||
+ | } | ||
+ | AUC_total[i]< | ||
+ | } | ||
+ | AUC_total_bpr< | ||
+ | |||
+ | |||
+ | setStoppingCriteria(autoConverge = FALSE, | ||
+ | deltaErrorThreshold = 1e-05, nrLoops = NULL, minNrLoops = 10) | ||
+ | showStoppingCriteria() | ||
+ | |||
+ | |||
+ | |||
+ | ############################################ | ||
+ | ############################################ | ||
+ | ####WRMF | ||
+ | |||
+ | # | ||
+ | # | ||
+ | |||
+ | install.packages(" | ||
+ | ####R CMD INSTALL rsparse-master/ | ||
+ | devtools:: | ||
+ | library(rsparse) | ||
+ | |||
+ | |||
+ | data(" | ||
+ | |||
+ | library(Matrix) | ||
+ | Matrix(movielens100k) | ||
+ | |||
+ | ml100k< | ||
+ | |||
+ | AUC_total< | ||
+ | for (i in 1:10) { | ||
+ | AUC<-0 | ||
+ | for( j in 1:5){ | ||
+ | fb< | ||
+ | # | ||
+ | model = WRMF$new(rank = i*10, lambda = 0.0025, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | x< | ||
+ | # | ||
+ | n_users_rank< | ||
+ | rank_n_items< | ||
+ | | ||
+ | pred_mmmf< | ||
+ | | ||
+ | AUC< | ||
+ | } | ||
+ | AUC_total[i]< | ||
+ | } | ||
+ | |||
+ | AUC_total_wrmf< | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ############################################ | ||
+ | ############################################ | ||
+ | ####SVD-MF | ||
+ | # | ||
+ | library(" | ||
+ | |||
+ | |||
+ | ml <- as(data2_train," | ||
+ | r_svd <- Recommender(ml, | ||
+ | p.svd <- predict(r_svd, | ||
+ | |||
+ | |||
+ | pred_svd< | ||
+ | |||
+ | AUC< | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | r <- Recommender(data = ml, method = " | ||
+ | | ||
+ | n_iterations=10, | ||
+ | r | ||
+ | recom_ratings <- predict(r, newdata = ml, type = " | ||
+ | as(recom_ratings, | ||
+ | |||
+ | getModel(r) | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | AUC_total< | ||
+ | for (i in 1:10) { | ||
+ | AUC<-0 | ||
+ | for( j in 1:2){ | ||
+ | fsvd< | ||
+ | | ||
+ | | ||
+ | | ||
+ | pred_svdf <- tcrossprod(fsvd$U, | ||
+ | |||
+ | AUC< | ||
+ | | ||
+ | } | ||
+ | AUC_total[i]< | ||
+ | } | ||
+ | |||
+ | AUC_total_svdf< | ||
+ | </ | ||