deploying apriori rulsets to the dataset in R

2019-07-27 15:39发布

问题:

I have a question regarding apriori rule deployment in R. I basically want to assign a predcition(item) and a confidence value to each customer so I can create a simple recommending system, so below is a subset of my rule set which I have obtained,

bread&wine -> meat (confidence 54%)
cheese -> fruit (confidence 43%)
bread&cheese -> frozveg (confidence 24%)

and the following is simple representation of what I want to achieve with just 1 customer; this is in a basket or truth-table data.

ID|Bread|Wine| Cheese Pred1 Conf1 Pred2 Conf2

1 | 1 | 1 | 1 meat| 0.54| fruit| 0.43

This can be done by simply connecting the dataset to the model nugget in IBM SPSS Modeler, but it does not seem easy in R.

Can anyone provide me with a solution in R code on this or a simple guide in doing this?

回答1:

Package recommenderlab does what you want (minus showing the confidence). Here is some code (adapted from the documentation of recommenerlab) which learns a recommender model from the Groceries data set and applies it to the first 10 transactions:

 library(recommenderlab)
 data(Groceries)
 dat <- as(Groceries, "binaryRatingMatrix")
 rec <- Recommender(dat, method = "AR", 
    parameter=list(support = 0.0005, conf = 0.5, maxlen = 5))
 getModel(rec)

   $description
   [1] "AR: rule base"

   $rule_base
   set of 38365 rules 

   $support
   [1] 5e-04

   $confidence
   [1] 0.5

   $maxlen
   [1] 5

   $measure
   [1] "confidence"

   $verbose
   [1] FALSE

   $decreasing
   [1] TRUE


 pred <- predict(rec, dat[1:5,])
 as(pred, "list")
   [[1]]
   [1] "whole milk"     "rolls/buns"     "tropical fruit"

   [[2]]
   [1] "whole milk"

   [[3]]
   character(0)

   [[4]]
   [1] "yogurt"        "whole milk"    "cream cheese " "soda"         

   [[5]]
   [1] "whole milk"

Here are the parameters you can use when you create the recommender.

recommenderRegistry$get_entry("AR", dataType = "binaryRatingMatrix")
  Recommender method: AR
  Description: Recommender based on association rules.
  Parameters:
    support confidence maxlen    measure verbose decreasing
  1     0.1        0.3      2 confidence   FALSE       TRUE