预测在Recommendationerlab中使用userid

时间:2018-12-19 12:27:26

标签: r recommendation-engine recommenderlab

我有一个很简单的推荐问题:我想在R中使用recommenderlab构建一个混合推荐器,在这里我推荐MovieLense数据集中已经很喜欢的电影以及热门电影和一些随机电影电影。

library(recommenderlab)

data("MovieLense")

MovieLense100 <- MovieLense[rowCounts(MovieLense) > 100,]
train <- MovieLense100[1:100]
test <- MovieLense100[101:103]

## mix popular movies with a random recommendations for diversity and
## rerecommend some movies the user liked.
recom <- HybridRecommender(
  Recommender(train, method = "POPULAR"),
  Recommender(train, method = "RANDOM"),
  Recommender(train, method = "RERECOMMEND"),
  weights = c(.6, .1, .3)
)

训练模型后,我想使用userId为给定的用户“预测”电影。 Recommenderlab文档建议,只要用户ID包含在培训数据中,就可以做到这一点。但是,当我尝试在userid中使用newdata进行预测时,收到以下错误消息:

as(predict(object = recom, newdata = 1), "list")
#> Error in object@predict(object@model, newdata, n = n, data = data, type = type, : If newdata is a user id then data needes to be the training dataset.

足够公平,我尝试使用向量:

as(predict(recom, newdata = c(1,5)), "list")
#> Error in object@predict(object@model, newdata, n = n, data = data, type = type, : If newdata is a user id then data needes to be the training dataset.

无法真正弄清楚此错误消息的含义。有人知道吗?

1 个答案:

答案 0 :(得分:1)

您只需要确定要从培训数据集中向用户推荐项目,如下所示:

> as(predict(recom, 1, data = train), "list")
$`1`
 [1] "Hearts and Minds (1996)"                     "Boys, Les (1997)"                            "Great Day in Harlem, A (1994)"              
 [4] "Dangerous Beauty (1998)"                     "Tough and Deadly (1995)"                     "Two or Three Things I Know About Her (1966)"
 [7] "Santa with Muscles (1996)"                   "Margaret's Museum (1995)"                    "Brassed Off (1996)"                         
[10] "Thousand Acres, A (1997)"                   

> as(predict(recom, 1:10, data = train), "list")
$`1`
 [1] "Dangerous Beauty (1998)"                     "Great Day in Harlem, A (1994)"               "Hearts and Minds (1996)"                    
 [4] "Boys, Les (1997)"                            "Santa with Muscles (1996)"                   "Two or Three Things I Know About Her (1966)"
 [7] "Tough and Deadly (1995)"                     "Saint of Fort Washington, The (1993)"        "Whole Wide World, The (1996)"               
[10] "Margaret's Museum (1995)"                   

$`5`
 [1] "Boys, Les (1997)"                            "Two or Three Things I Know About Her (1966)" "Santa with Muscles (1996)"                  
 [4] "Dangerous Beauty (1998)"                     "Great Day in Harlem, A (1994)"               "Whole Wide World, The (1996)"               
 [7] "Hearts and Minds (1996)"                     "Brassed Off (1996)"                          "Saint of Fort Washington, The (1993)"       
[10] "Celestial Clockwork (1994)"                 

$`6`
 [1] "Dangerous Beauty (1998)"                     "Two or Three Things I Know About Her (1966)" "Santa with Muscles (1996)"                  
 [4] "Tough and Deadly (1995)"                     "Brassed Off (1996)"                          "Crooklyn (1994)"                            
 [7] "Great Day in Harlem, A (1994)"               "Hearts and Minds (1996)"                     "Saint of Fort Washington, The (1993)"       
[10] "Boys, Les (1997)"                           

$`7`
 [1] "Two or Three Things I Know About Her (1966)" "Tough and Deadly (1995)"                     "Santa with Muscles (1996)"                  
 [4] "Dangerous Beauty (1998)"                     "Hearts and Minds (1996)"                     "Great Day in Harlem, A (1994)"              
 [7] "Boys, Les (1997)"                            "Brassed Off (1996)"                          "Margaret's Museum (1995)"                   
[10] "Crooklyn (1994)"                            

$`10`
 [1] "Dangerous Beauty (1998)"                     "Great Day in Harlem, A (1994)"               "Hearts and Minds (1996)"                    
 [4] "Tough and Deadly (1995)"                     "Two or Three Things I Know About Her (1966)" "Boys, Les (1997)"                           
 [7] "Santa with Muscles (1996)"                   "Brassed Off (1996)"                          "Margaret's Museum (1995)"                   
[10] "Crooklyn (1994)"                            

$`11`
 [1] "Two or Three Things I Know About Her (1966)" "Hearts and Minds (1996)"                     "Great Day in Harlem, A (1994)"              
 [4] "Tough and Deadly (1995)"                     "Brassed Off (1996)"                          "Dangerous Beauty (1998)"                    
 [7] "Boys, Les (1997)"                            "Santa with Muscles (1996)"                   "Thousand Acres, A (1997)"                   
[10] "Horse Whisperer, The (1998)"                

$`13`
 [1] "Santa with Muscles (1996)"        "Brassed Off (1996)"               "Tough and Deadly (1995)"          "Boys, Les (1997)"                
 [5] "Thousand Acres, A (1997)"         "Margaret's Museum (1995)"         "Total Eclipse (1995)"             "Love! Valour! Compassion! (1997)"
 [9] "Withnail and I (1987)"            "Prefontaine (1997)"              

$`15`
 [1] "Two or Three Things I Know About Her (1966)" "Boys, Les (1997)"                            "Great Day in Harlem, A (1994)"              
 [4] "Tough and Deadly (1995)"                     "Dangerous Beauty (1998)"                     "Saint of Fort Washington, The (1993)"       
 [7] "Santa with Muscles (1996)"                   "Hearts and Minds (1996)"                     "Celestial Clockwork (1994)"                 
[10] "Whole Wide World, The (1996)"               

$`16`
 [1] "Great Day in Harlem, A (1994)"               "Dangerous Beauty (1998)"                     "Hearts and Minds (1996)"                    
 [4] "Two or Three Things I Know About Her (1966)" "Boys, Les (1997)"                            "Santa with Muscles (1996)"                  
 [7] "Tough and Deadly (1995)"                     "Brassed Off (1996)"                          "Crooklyn (1994)"                            
[10] "Thousand Acres, A (1997)"                   

$`18`
 [1] "Great Day in Harlem, A (1994)"               "Two or Three Things I Know About Her (1966)" "Dangerous Beauty (1998)"                    
 [4] "Hearts and Minds (1996)"                     "Santa with Muscles (1996)"                   "Tough and Deadly (1995)"                    
 [7] "Boys, Les (1997)"                            "Whole Wide World, The (1996)"                "Margaret's Museum (1995)"                   
[10] "Saint of Fort Washington, The (1993)" 

否则,预测函数不知道您指的是哪个用户。

您还可以按照以下方式预测测试数据:

> as(predict(recom, test), "list")