2013-05-21 32 views
5

我使用R中的包arules生成關聯規則。我想 限制規則,以便在左側只有一個 特定元素,我們稱之爲「potatoe」。R中的arules:在左側只有一個項目獲取規則

如果我這樣做:

rules <- apriori(dtm.mat, parameter = list(sup = 0.4, conf = 
0.9,target="rules"), appearance = list(lhs = c("potatoe"))) 

我上LHS「土豆」,而且所有其他種類的東西。我如何 強制規則只包含一個元素?參數maxlen不要做 我想要的,因爲,據我所知,我不能指定maxlen到 適用於左邊的元素。

回答

-5

這裏有一個方法:

  1. 使用檢查生成的規則列表()。
  2. 將所有規則複製到文本編輯器中。
  3. 保存爲.txt文件。
  4. 在excel中以固定寬度分隔文件打開。
  5. 過濾LHS只包含「potatoe」[原文如此]。

可能有更簡單的方法,但至少你不必手動搜索在左側「土豆」(原文如此)。

6

假設您已經生成了規則(「規則」,在您的問題中),以下是如何對其進行分類。基本上,您必須將數據強制轉換爲數據框,然後將其分組。

#Here are the original rules generated with some data I created 
# categories are "G", "T", "D", and "potatoe" 

> inspect(rules); 
lhs  rhs  support confidencelift 
1 {} => {T}  0.3333333 0.3333333 1.0000000 
2 {} => {G}  0.5000000 0.5000000 1.0000000 
3 {} => {potatoe} 0.5000000 0.5000000 1.0000000 
4 {} => {D}  0.5000000 0.5000000 1.0000000 
5 {T} => {G}  0.1666667 0.5000000 1.0000000 
6 {G} => {T}  0.1666667 0.3333333 1.0000000 
7 {T} => {D}  0.1666667 0.5000000 1.0000000 
8 {D} => {T}  0.1666667 0.3333333 1.0000000 
9 {G} => {potatoe} 0.1666667 0.3333333 0.6666667 
10 {potatoe} => {G} 0.1666667 0.3333333 0.6666667 
11 {potatoe} => {D} 0.3333333 0.6666667 1.3333333 
12 {D} => {potatoe} 0.3333333 0.6666667 1.3333333 

#Coerce into data frame 
as(rules, "data.frame"); 

#Restrict LHS to only certain value (here, "potatoe") 
rules_subset <- subset(rules, (lhs %in% c("potatoe"))); 

#Check to see subset rules 
inspect(rules_subset); 
lhs   rhs support confidencelift 
1 {potatoe} => {G} 0.1666667 0.3333333 0.6666667 
2 {potatoe} => {D} 0.3333333 0.6666667 1.3333333 

該方法還允許任意多個LHS值,而不僅僅是一個。比我以前提出的答案容易得多。

3

伴侶,你只需在外觀參數列表中添加default="rhs",就完成了。

rules <- apriori(dtm.mat, parameter = list(sup = 0.4, conf = 
0.9,target="rules"), appearance = list(lhs = c("potatoe"), default="rhs")) 

lhs=c("potatoe")參數告訴該LHS應該只包含「土豆」的算法,和default="rhs"參數告訴算法,該RHS應該是默認的一個(即,沒有任何限制將被施加有)。

0

subset()函數提供了一種方法來搜索事務,項目或規則的子集。

potatoerules <- subset(rules , items %in% "potatoe") 
相關問題