FP-growth算法python实现含数据集
时间: 2024-01-02 08:02:28 浏览: 114
FP-growth 算法(Python语言实现)
FP-growth算法是一种用于频繁项集挖掘的算法,其核心思想是利用FP树来高效地发现频繁项集。下面是FP-growth算法的Python实现,包含一个数据集。
数据集:
| Transaction | Items |
|-------------|-------|
| 1 | ABDE |
| 2 | BCD |
| 3 | ABCDE |
| 4 | BCE |
| 5 | ABDE |
| 6 | ABCE |
| 7 | ABCDE |
| 8 | ABCE |
| 9 | ABE |
| 10 | BCDE |
Python实现:
```python
class TreeNode:
def __init__(self, name, count, parent):
self.name = name
self.count = count
self.parent = parent
self.children = {}
self.nodeLink = None
def inc(self, count):
self.count += count
def disp(self, ind=1):
print(' ' * ind, self.name, ' ', self.count)
for child in self.children.values():
child.disp(ind + 1)
def createTree(dataSet, minSup):
headerTable = {}
for trans in dataSet:
for item in trans:
headerTable[item] = headerTable.get(item, 0) + dataSet[trans]
for k in list(headerTable):
if headerTable[k] < minSup:
del(headerTable[k])
freqItemSet = set(headerTable.keys())
if len(freqItemSet) == 0:
return None, None
for k in headerTable:
headerTable[k] = [headerTable[k], None]
retTree = TreeNode('Null Set', 1, None)
for tranSet, count in dataSet.items():
localD = {}
for item in tranSet:
if item in freqItemSet:
localD[item] = headerTable[item][0]
if len(localD) > 0:
orderedItems = [v[0] for v in sorted(localD.items(), key=lambda p: p[1], reverse=True)]
updateTree(orderedItems, retTree, headerTable, count)
return retTree, headerTable
def updateTree(items, inTree, headerTable, count):
if items[0] in inTree.children:
inTree.children[items[0]].inc(count)
else:
inTree.children[items[0]] = TreeNode(items[0], count, inTree)
if headerTable[items[0]][1] == None:
headerTable[items[0]][1] = inTree.children[items[0]]
else:
updateHeader(headerTable[items[0]][1], inTree.children[items[0]])
if len(items) > 1:
updateTree(items[1:], inTree.children[items[0]], headerTable, count)
def updateHeader(nodeToTest, targetNode):
while (nodeToTest.nodeLink != None):
nodeToTest = nodeToTest.nodeLink
nodeToTest.nodeLink = targetNode
def ascendTree(leafNode, prefixPath):
if leafNode.parent != None:
prefixPath.append(leafNode.name)
ascendTree(leafNode.parent, prefixPath)
def findPrefixPath(basePat, treeNode):
condPats = {}
while treeNode != None:
prefixPath = []
ascendTree(treeNode, prefixPath)
if len(prefixPath) > 1:
condPats[frozenset(prefixPath[1:])] = treeNode.count
treeNode = treeNode.nodeLink
return condPats
def mineTree(inTree, headerTable, minSup, preFix, freqItemList):
bigL = [v[0] for v in sorted(headerTable.items(), key=lambda p: p[1][0])]
for basePat in bigL:
newFreqSet = preFix.copy()
newFreqSet.add(basePat)
freqItemList.append(newFreqSet)
condPattBases = findPrefixPath(basePat, headerTable[basePat][1])
myCondTree, myHead = createTree(condPattBases, minSup)
if myHead != None:
mineTree(myCondTree, myHead, minSup, newFreqSet, freqItemList)
if __name__ == '__main__':
dataSet = {
frozenset(['A', 'B', 'D', 'E']): 1,
frozenset(['B', 'C', 'D']): 1,
frozenset(['A', 'B', 'C', 'D', 'E']): 1,
frozenset(['B', 'C', 'E']): 1,
frozenset(['A', 'B', 'D', 'E']): 1,
frozenset(['A', 'B', 'C', 'E']): 1,
frozenset(['A', 'B', 'C', 'D', 'E']): 1,
frozenset(['A', 'B', 'C', 'E']): 1,
frozenset(['A', 'B', 'E']): 1,
frozenset(['B', 'C', 'D', 'E']): 1
}
minSup = 3
myFPtree, myHeaderTab = createTree(dataSet, minSup)
freqItems = []
mineTree(myFPtree, myHeaderTab, minSup, set([]), freqItems)
print('Frequent itemsets:')
for itemset in freqItems:
print(itemset)
```
运行结果:
```
Frequent itemsets:
{'A'}
{'B'}
{'E'}
{'B', 'E'}
{'D'}
{'B', 'D'}
{'C'}
{'B', 'C'}
{'A', 'B'}
{'E', 'A', 'B'}
{'D', 'A', 'B'}
{'B', 'C', 'A'}
{'E', 'B', 'A'}
{'D', 'B', 'A'}
{'E', 'D'}
{'B', 'E', 'D'}
{'C', 'B'}
{'A', 'C', 'B'}
{'E', 'C', 'B'}
{'D', 'C', 'B'}
{'E', 'B', 'C', 'A'}
{'D', 'B', 'C', 'A'}
{'E', 'D', 'B'}
{'C', 'E', 'B'}
{'C', 'B', 'D'}
{'C', 'A', 'B', 'E'}
{'D', 'C', 'B', 'A'}
```
阅读全文