4

我正在努力实现下面所述的目标并且有很多错误。我花了很多时间尝试对规则进行排序并打印前十名。我知道如何打印出整个列表。

使用 R,探索在较大数据文件中生成规则。考虑成人数据(在 R 中可通过 >data(Adult)命令获得)。生成置信度阈值为 0.8 的关联规则

  1. 打印出按支持排序的前 10 条规则。考虑使用检查命令以及对排序规则进行排序和索引。
  2. 打印出按置信度排序的前 10 条规则。
  3. 查看生成规则,这些规则被限制为在规则的 lhs 上获得收入。请注意,收入选项有两个值:小和大。考虑包括apriori 函数的外观参数。打印按提升排序的前 10 条规则。

到目前为止,这是我的代码:

library(arules)    
library(arulesViz)

data(Adult)
head(Adult)

rules <- apriori(Adult, parameter = list(supp = 0.5, conf = 0.8))

top.support <- sort(rules, decreasing = TRUE, na.last = NA, by = "support")
top.ten.support <- sort.list(top.support, partial=10)
inspect(top.ten.support)

top.confidence <- sort(rules, decreasing = TRUE, na.last = NA, by = "confidence")
top.ten.confidence <- sort.list(top.support,partial=10)
inspect(top.ten.confidence)

rules2 <- apriori(Adult, parameter=list(supp = 0.5, conf = 0.8), appearance = income)

top.lift <- sort(rules2, decreasing = TRUE, na.last = NA, by = "lift")
top.ten.lift <- sort.list(top.lift, partial=10)
inspect(top.ten.lift)
4

1 回答 1

4

1) 打印出按支持度排序的前 10 条规则:

R> top.support <- sort(rules, decreasing = TRUE, na.last = NA, by = "support")
R> inspect(head(top.support, 10))  # or inspect(sort(top.support)[1:10])
   lhs                               rhs                            support confidence   lift
1  {}                             => {capital-loss=None}             0.9533     0.9533 1.0000
2  {}                             => {capital-gain=None}             0.9174     0.9174 1.0000
3  {}                             => {native-country=United-States}  0.8974     0.8974 1.0000
4  {capital-gain=None}            => {capital-loss=None}             0.8707     0.9491 0.9956
5  {capital-loss=None}            => {capital-gain=None}             0.8707     0.9133 0.9956
...

2) 打印出按置信度排序的前 10 条规则:

R> top.confidence <- sort(rules, decreasing = TRUE, na.last = NA, by = "confidence")
R> inspect(head(top.confidence, 10))
   lhs                               rhs                 support confidence   lift
1  {hours-per-week=Full-time}     => {capital-loss=None}  0.5607     0.9583 1.0052
2  {workclass=Private}            => {capital-loss=None}  0.6640     0.9565 1.0034
3  {workclass=Private,                                                            
    native-country=United-States} => {capital-loss=None}  0.5897     0.9555 1.0023
4  {capital-gain=None,                                                            
    hours-per-week=Full-time}     => {capital-loss=None}  0.5192     0.9551 1.0019
5  {workclass=Private,                                                            
    race=White}                   => {capital-loss=None}  0.5675     0.9550 1.0018
...

3)

R> rules2 <- apriori(Adult, parameter=list(supp = 0.1, conf = 0.8),
                     appearance = list(lhs = c("income=small", "income=large"), 
                                       default = "rhs"))
R> top.lift <- sort(rules2, decreasing = TRUE, na.last = NA, by = "lift")
R> inspect(head(subset(top.lift, lhs %pin% "income"), 10))
lhs               rhs                                 support confidence  lift
1 {income=large} => {marital-status=Married-civ-spouse}  0.1370     0.8535 1.8627
2 {income=large} => {sex=Male}                           0.1364     0.8496 1.2710
3 {income=large} => {race=White}                         0.1457     0.9077 1.0615
4 {income=small} => {capital-gain=None}                  0.4849     0.9581 1.0444
5 {income=large} => {native-country=United-States}       0.1468     0.9146 1.0191
...
于 2014-04-08T20:33:51.937 回答