我正在尝试通过 Apriori 算法从 XML 文档中挖掘关联规则。这样做有两种通用方法:1) 将 XML 映射到关系格式并应用经典 Apriori,2) 直接挖掘 XML。我想使用后者,但有几个问题。
经过研究,我发现了两个不完整的解决方案:
SO中的这篇文章提供了生成k-itemset的解决方案
本文提出用XQuery模拟Apriori(提供的代码不完整)
请让我知道您对如何做到这一点的想法和建议(对于这两种方法)?
更新:
根据上述论文的另一个版本,数据集是这样的:
<transactions>
<transaction id="1">
<items>
<item>a</item>
<item>d</item>
<item>e</item>
</items>
</transaction>
<transaction id="2">
<items>
<item>b</item>
<item>c</item>
<item>d</item>
</items>
</transaction>
<transaction id="3">
<items>
<item>a</item>
<item>c</item>
</items>
</transaction>
<transaction id="4">
<items>
<item>b</item>
<item>c</item>
<item>d</item>
</items>
</transaction>
<transaction id="5">
<items>
<item>a</item>
<item>b</item>
</items>
</transaction>
</transactions>
那么,函数如下
define function join(element $X, element $Y) returns element {
let $items := (for $item in $Y
where every $i in $X satisfies
$i != $item
return $item)
return $X union $items
}
define function commonIts(element $X, element $Y) returns
element {
for $item in $X
where some $i in $Y satisfies $i = $item
return $item
}
define function removeIts(element $X, element $Y) returns
element {
for $item in $X
where every $i in $Y satisfies $i != $item
return $item
}
define function candidateGen(element $l) returns element {
for $freqSet1 in $l
let $items1 := $freqSet1//items/*
for $freqSet2 in $l
let $items2 := $freqSet2//items/*
where $freqSet2 >> $freqSet1 and
count($items1)+1 = count($items1 union $items2)
and prune(join($items1,$items2), $l)
return <items>
{join($items1,$items2)}
</items>
}
define function prune(element $X, element $Y) returns boolean
{
every $item in $X satisfies
some $items in $Y//items satisfies
count(commonIts(removeIts($X,$item),$items/*))
= count($X) - 1
}
define function removeDuplicate(element $C) returns element
{
for $itemset1 in $C
let $items1 := $itemset1/*
let $items :=(for $itemset2 in $C
let $items2 := $itemset2/*
where $itemset2>>$itemset1 and
count($items1) =
count(commonIts($items1, $items2))
return $items2)
where count($items) = 0
return $itemset1
}
define function getLargeItemsets(element $C, element $minsup,
element $total, element $src) returns element {
for $items in $C
let $trans := (for $tran in $src
where every $item1 in $items/* satisfies
some $item2 in $tran/*
satisfies $item1 = $item2
return $tran)
let $sup := (count($trans) * 1.00) div $total
where $sup >= $minsup
return <largeItemset> {$items}
<support> {$sup} </support>
</largeItemset>
}
define function apriori(element $l, element $L, element $minsup,
element $total, element $src) returns element {
let $C := removeDuplicate(candidateGen($l))
let $l := getLargeItemsets($C, $minsup, $total, $src)
let $L := $l union $L
return if (empty($l)) then
$L
else
apriori($l, $L, $minsup, $total, $src)
}
let $src := document(“/transactions.xml”)//items
let $minsup := 0.4
let $items := (for $item in $src/*
where $itemset = $item
let $total := count($src) * 1.00
let $C := distinct-values($src/*)
let $l :=(for $itemset in $C
return $item)
let $sup := (count($items) * 1.00) div $total
where $sup >= $minsup
return <largeItemset>
<items> {$itemset} </items>
<support> {$sup} </support>
</largeItemset>)
let $L := $l
return <largeItemsets> { apriori($l, $L,$minsup, $total, $src) }
</largeItemsets>
为了计算规则文档,他们引入了这个表达式:
let $minconf := 1.00
let $src := document(“/large.xml”)//largeItemset
for $itemset1 in $src
let $items1 := $itemset1/items/*
for $itemset2 in $src
let $items2 := $itemset2/items/*
where count($items1) > count($items2) and
count(commonIts($items1, $items2)) =
count($items2) and $itemset1/support div
$itemset2/support >= $minconf
return <rule support =“{$itemset1/support}”
confidence = “{($itemset1/support*1.0) div
($itemset2/support*1.0)}”>
<antecedent> {$items2} </antecedent>
<consequent>
{removeItems($items1,$items2)}
</consequent>
</rule>
现在,对我来说最大的挑战是将这些功能集成在一起工作。