2

我有一个相当简单的推荐问题:我想在 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 在训练数据中,这是可能的。但是,当我尝试使用useridin 进行预测时,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.

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

4

1 回答 1

2

您只需要准确说明您正在从训练数据集中向一个或多个用户推荐项目,如下所示:

> 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")
于 2018-12-19T13:59:37.230 回答