Recommenderlab - 排序评估结果(列表清单?)

时间:2014-12-22 19:02:03

标签: r list sorting

我试图从suggestlab包中评估几种推荐方法的结果,并通过MAE获得一个有序列表(或data.frame)。我尝试了几种方法,但没有一种能像我预期的那样工作。

我正在使用的代码:

library(recommenderlab)
data(Jester5k)
Jester5k
r <- sample(Jester5k, 1000) 
rec_pop <- Recommender(Jester5k[1:1000], method = "POPULAR")
#rec_pop
recom_pop <- predict(rec_pop, Jester5k[1001:1002], n=100, type="ratings")
#recom_pop
as(recom_pop, "matrix")
getList(recom_pop,decode=TRUE,ratings=TRUE)
getData.frame(recom_pop,decode=TRUE,ratings=TRUE)
user_id and item_id are displayed correctly with each of the 3 alternatives
rec_ib <- Recommender(r[1:100],method="IBCF", param=list(normalize = "Z-score",method="Jaccard",minRating=1))
#rec_ib
recom_ib <- predict(rec_ib, Jester5k[1001:1002], n=100, type="ratings")
#recom_ib 
as(recom_ib, "matrix")
getList(recom_ib,decode=TRUE,ratings=TRUE)
getData.frame(recom_ib,decode=TRUE,ratings=TRUE)
user_id and item_id are displayed correctly with each of the 3 alternatives
user <- c("u20089")
subset<-Jester5k[user,]
subset_rownames <- rownames(subset)
subset_rownames
as(subset,"matrix")
subset
rec_ub <- Recommender(r[1:400],method="UBCF", param=list(normalize = "Z-score",method="Cosine",nn=5, minRating=1))
#rec_ub
recom_ub <- predict(rec_ub, subset, n=100, type="ratings")
recom_ub
as(recom_ub, "matrix")

rownames(recom_ub) <- subset_rownames
getList(recom_ub,decode=TRUE,ratings=TRUE)
getData.frame(recom_ub,decode=TRUE,ratings=TRUE)

---head(as(Jester5k[1001:1001], "matrix"))
rownames(subset)

str(recom_ub)
str(subset)

Jester5k[cod_]

str(rec_ub)
str(recom_ub)

(myUsers <- rownames(recom_ub@data))
myUsers

str(rec_ub)
str(recom_ib)
# #
scheme <- evaluationScheme(r,method="split",train=0.7,k=1,given=5,goodRating=4)

algorithms <- list(
  "popular items" = list(name="POPULAR",  param=list(normalize="Z-score")),
  "random items" = list(name="RANDOM",  param=list(normalize="Z-score")),

  "item-based CF  sm:Jaccard  nn:10  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Jaccard",k=10,minRating=4)   ) ,     
  "item-based CF  sm:Jaccard  nn:10  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Cosine",k=10,minRating=4)   ) ,      
  "item-based CF  sm:Jaccard  nn:10  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Pearson",k=10,minRating=4)   ) ,       

  "item-based CF  sm:Jaccard  nn:5  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Jaccard",k=5,minRating=4)   ) ,     
  "item-based CF  sm:Jaccard  nn:5  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Cosine",k=5,minRating=4)   ) ,      
  "item-based CF  sm:Jaccard  nn:5  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Pearson",k=5,minRating=4)   ) ,  

  "item-based CF  sm:Jaccard  nn:2  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Jaccard",k=2,minRating=4)   ) ,     
  "item-based CF  sm:Jaccard  nn:2  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Cosine",k=2,minRating=4)   ) ,      
  "item-based CF  sm:Jaccard  nn:2  minr:4" = list(name="IBCF",  param=list(normalize="Z-score",method="Pearson",k=2,minRating=4)   ) ,  


  "user-based CF  sm:Jaccard  nn:10  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Jaccard",nn=10,minRating=4)   )  ,
  "user-based CF  sm:Jaccard  nn:10  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Cosine",nn=10,minRating=4)   ),
  "user-based CF  sm:Jaccard  nn:10  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Pearson",nn=10,minRating=4)   ),

  "user-based CF  sm:Jaccard  nn:5  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Jaccard",nn=5,minRating=4)   )  ,
  "user-based CF  sm:Jaccard  nn:5  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Cosine",nn=5,minRating=4)   ),
  "user-based CF  sm:Jaccard  nn:5  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Pearson",nn=5,minRating=4)   ),

  "user-based CF  sm:Jaccard  nn:2  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Jaccard",nn=2,minRating=4)   )  ,
  "user-based CF  sm:Jaccard  nn:2  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Cosine",nn=2,minRating=4)   ),
  "user-based CF  sm:Jaccard  nn:2  minr:4" = list(name="UBCF", param=list(normalize="Z-score",method="Pearson",nn=2,minRating=4)   )
  #   ,
  #   "PCA" = list(name="PCA", param=NULL),  
  #   "SVD" = list(name="SVD", param=NULL)  
)

# ?evaluate
results <- evaluate(scheme, algorithms, n=c(1, 3, 5, 10, 15, 20),type="ratings")

results_avg<-avg(results)

results_avg

如何通过MAE订购此列表?

1 个答案:

答案 0 :(得分:2)

使用dplyr

library(dplyr)
df <- data.frame(matrix(unlist(results_avg), nrow=20, byrow=T)) %>% 
  transmute(RMSE=X1, MSE=X2, MAE=X3) %>%
  mutate(names = names(results_avg)) %>%
  arrange(MAE)

这将导致:

> head(df)
      RMSE      MSE      MAE                                    names
1 4.906217 24.07097 3.788543 user-based CF  sm:Jaccard  nn:10  minr:4
2 4.961822 24.61968 3.851592 user-based CF  sm:Jaccard  nn:10  minr:4
3 5.031447 25.31546 3.867063  user-based CF  sm:Jaccard  nn:5  minr:4
4 4.981574 24.81608 3.872608 user-based CF  sm:Jaccard  nn:10  minr:4
5 5.060753 25.61122 3.916104  user-based CF  sm:Jaccard  nn:5  minr:4
6 5.086503 25.87251 3.939044  user-based CF  sm:Jaccard  nn:5  minr:4

或者你可以这样做:(感谢@RichardScriven)

x <- do.call(rbind, results_avg) 
rownames(x) <- NULL 
df <- as.data.frame(cbind(x, names(results_avg)))
df[order(df$MAE),]