简体   繁体   English

R中的Apriori算法,不是负规则

[英]Apriori algorithm in R, not negative rules

I have a large binary data set where I wish to run an apriori algorithm in R. The problem is at the algorithm is making rules of all the 0's, where I only wish to look at the 1's. 我有一个很大的二进制数据集,希望在R中运行一个先验算法。问题在于算法正在制定所有0的规则,而我只希望看一下1。 As for example get these rules: 例如获取以下规则:

        lhs                                                        rhs     support   confidence lift      count
[1]    {SPA=0,SPD=0,SPE=0,SPF=1,SPJ=0}                         => {SPC=0} 0.2036065 0.9866727  1.0174854  6515
[2]    {SPA=0,SPD=0,SPE=0,SPF=1}                               => {SPC=0} 0.2163885 0.9864653  1.0172715  6924
[3]    {SPA=0,SPD=0,SPF=1,SPJ=0}                               => {SPC=0} 0.2070754 0.9852788  1.0160479  6626

Does anyone know how to only look for the rules where the variables are 1 and not 0? 有谁知道如何只寻找变量为1而不是0的规则? Thank you! 谢谢!

You can control this using the appearance argument to apriori . 您可以使用aprioriappearance参数来控制它。 Since you do not provide data, I will use the built-in Adult data as an example, but I think that you need to add appearance=list(rhs = "SPC=1") to your apriori statement. 由于您不提供数据,因此我将使用内置的Adult数据作为示例,但是我认为您需要在您的apriori语句中添加appearance=list(rhs = "SPC=1")

Example

I will generate only rules for which the rhs is native-country=United-States 我将仅生成其rhs为native-country = United States的规则

rules <- apriori(Adult, 
    parameter = list(supp = 0.4, conf = 0.6, 
        minlen=2, target = "rules"),
    appearance=list(rhs = "native-country=United-States")
)

inspect(rhs(rules[1:5]))
    items                         
[1] {native-country=United-States}
[2] {native-country=United-States}
[3] {native-country=United-States}
[4] {native-country=United-States}
[5] {native-country=United-States}

Addition 加成

I thought that you only wanted SPC=1 on the rhs. 我以为您只希望rhs上的SPC = 1。 Based on your comments, I now think that you want to generate rules that contain no XYZ=0 items at all. 根据您的评论,我现在认为您想生成完全不包含XYZ = 0项目的规则。 You can also get this with appearance . 您也可以通过appearance获得此效果。 First identify the possible items with XYZ=0, then use appearance to exclude these. 首先用XYZ = 0识别可能的项目,然后使用外观排除这些项目。 I do not know what your variables are called, so I am calling the transactions TransactionData 我不知道您的变量叫什么,所以我将事务称为TransactionData

## identify items to exclude
excluded <- grep("=0", itemLabels(TransactionData), value = TRUE)

Then add this to your apriori statement. 然后将其添加到您的apriori语句中。

appearance=list(none = excluded)

The easiest way to fix this is to make the matrix logical before you create the transactions. 解决此问题的最简单方法是在创建事务之前使矩阵具有逻辑性。 For matrix m you can do the following: 对于矩阵m您可以执行以下操作:

storage.mode(m) <- "logical"
trans <- as(m, transactions)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM