data_analysis:recommendation_bpr

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
data_analysis:recommendation_bpr [2020/04/29 11:29] prgramdata_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:  $\forall i,j,k \in I$ +>  properties of total order:  $\forall i,j,k \in I$ 
-   (totality) : $\text{if } i \neq j \text{, then } i >_u j \text{ or } j >_u i$  +(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$ +(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$+(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:
  
 ![](img3.png)![](img_netflix3.png) ![](img3.png)![](img_netflix3.png)
 +{{:data_analysis:pasted:20200429-203305.png}} 
 +{{:data_analysis:pasted:20200429-203230.png}}
 ###### 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 ====
 +<code>
 +#install.packages("rrecsys", force=TRUE, dependencies=TRUE)
 +
 +
 +test_index<- function(data, na=TRUE) {
 +  index_test<-c()
 +  for (i in 1:dim(data)[1]) {
 +    if(na) indeX_i <- which(!is.na(data[i,]))
 +    else index_i<-which(data[i,]==0)
 +    
 +    index_test[i]<-index_i[sample(1:length(index_i),1)]
 +  }
 +  test_index <- index_test
 +}
 +
 +
 +train <- function (data, index_test, na=TRUE) {
 +   
 +  train_data<-data
 +  for (i in 1:dim(data)[1]) {
 +    train_data[i,index_test[i]]<-NA
 +  }
 +  train_data[is.na(train_data)]<-0
 +  train<-train_data
 +}
 +
 +
 +
 +calc_AUC <- function(data, pred, index_test, na=TRUE) {
 +  AUC_u <- 0
 +  for (i in 1:dim(data)[1]) {
 +    i=1
 +    if(na) index_j<-which(is.na(data[i,]))
 +    else  index_j<-which(data[i,]==0)
 +    
 +    AUC_u <- AUC_u + 1/length(index_j) * sum(pred[i,index_test[i]] > pred[i,index_j])
 +  }  
 +  
 +  calc_AUC <- AUC_u / dim(data)[1]
 +}
 +
 +############################################
 +############################################
 +############################################
 +############################################
 +############################################
 +
 +
 +# check data
 +which(apply(is.na(data2),1,sum)>1680)
 +
 +which(apply(is.na(data2),2,sum)<10)
 +sum(!is.na(data2))
 +
 +
 +
 +
 +############################################
 +############################################
 +
 +ml100k_ori<-ml100k
 +ml100k[ml100k>0]<-1
 +
 +index_test<- test_index(ml100k, na=FALSE)
 +data2_train<-train(ml100k,index_test, na=FALSE)
 +############################################
 +############################################
 +
 +############################################
 +############################################
 +####BPR
 +library("rrecsys")
 +setwd("C:/Users/heeseoklee/Downloads/rrecsys-master/rrecsys-master/R")
 +source("ALG_BPR.R" #BPR2
 +
 +
 +showDeltaError()
 +#rrecsys(ratings, alg="BPR")
 +
 +ratings<-defineData(data2_train)  
 +
 +
 +
 +AUC_total<-c()
 +for (i in 1:10) {
 +  AUC<-0
 +  for( j in 1:5){
 +    model_bpr<-BPR2(ratings,k=10*i)
 +    pred_bpr<-predict(model_bpr)
 +    
 +    AUC<-AUC+calc_AUC(ml100k,pred_bpr,index_test,na=FALSE)
 +  }
 +  AUC_total[i]<-AUC/5
 +}
 +AUC_total_bpr<-AUC_total
 +
 +
 +setStoppingCriteria(autoConverge = FALSE,
 +                    deltaErrorThreshold = 1e-05, nrLoops = NULL, minNrLoops = 10)
 +showStoppingCriteria()
 +
 +
 +
 +############################################
 +############################################
 +####WRMF
 +
 +#devtools::install_github("dselivanov/reco")
 +#library("reco")
 +
 +install.packages("devtools")
 +####R CMD INSTALL rsparse-master/
 +devtools::install_github("dselivanov/rsparse", force=TRUE)
 +library(rsparse)
 +
 +
 +data("movielens100k")
 +
 +library(Matrix)
 +Matrix(movielens100k)
 +
 +ml100k<-as(movielens100k, "matrix")#[1:10,1:10]
 +
 +AUC_total<-c()
 +for (i in 1:10) {
 +  AUC<-0
 +  for( j in 1:5){
 +    fb<-"implicit"
 +    #fb<-"explicit"
 +    model = WRMF$new(rank = i*10, lambda = 0.0025,
 +                     feedback = fb,#c("implicit", "explicit"),
 +                     init_stdv = 0.01,
 +                     n_threads = parallel::detectCores(),
 +                     non_negative = TRUE,
 +                     solver = "conjugate_gradient",#c("conjugate_gradient", "cholesky"),
 +                     cg_steps = 5L,
 +                     components = NULL)
 +    
 +    x<-as(data2_train, "CsparseMatrix")
 +    #x<-movielens100k
 +    n_users_rank<-model$fit_transform(x, n_iter = 10L)
 +    rank_n_items<-model$components
 +    
 +    pred_mmmf<-n_users_rank %*% rank_n_items
 +    
 +    AUC<-AUC+calc_AUC(ml100k,pred_mmmf,index_test,na=FALSE)
 +  }
 +  AUC_total[i]<-AUC/5
 +}
 +
 +AUC_total_wrmf<-AUC_total
 +
 +
 +
 +
 +
 +
 +
 +############################################
 +############################################
 +####SVD-MF
 +#install.packages("recommenderlab")
 +library("recommenderlab")
 +
 +
 +ml <- as(data2_train,"realRatingMatrix")
 +r_svd <- Recommender(ml, method = "SVD",param=list(k=10))
 +p.svd <- predict(r_svd, ml, type = "ratings")
 +
 +
 +pred_svd<-getModel(r_svd)$svd$u %&% diag((getModel(r_svd)$svd)$d) %*% t(getModel(r_svd)$svd$v)
 +
 +AUC<-calc_AUC(ml100k,pred_svd,index_test,na=FALSE)
 +
 +
 +
 +
 +r <- Recommender(data = ml, method = "ALS", 
 +                 parameter = list(normalize=NULL, lambda=0.0025, n_factors=10, 
 +                                  n_iterations=10, seed = NULL, verbose = FALSE)) 
 +r
 +recom_ratings <- predict(r, newdata = ml, type = "ratings")
 +as(recom_ratings, "matrix")
 +
 +getModel(r)
 +
 +
 +
 +
 +AUC_total<-c()
 +for (i in 1:10) {
 +  AUC<-0
 +  for( j in 1:2){
 +    fsvd<-funkSVD(ml, k = 10, gamma = 0.0025, lambda = 0.01,
 +               min_improvement = 1e-06, min_epochs = 30, max_epochs = 50,
 +               verbose = TRUE)
 +    
 +    pred_svdf <- tcrossprod(fsvd$U, fsvd$V)
 +
 +    AUC<-AUC+calc_AUC(ml100k,pred_svdf,index_test,na=FALSE)
 +    
 +  }
 +  AUC_total[i]<-AUC/2
 +}
 +
 +AUC_total_svdf<-AUC_total
 +</code>
  
  • data_analysis/recommendation_bpr.1588159768.txt.gz
  • Last modified: 2025/07/07 14:12
  • (external edit)