FpGrowth算法python实现
时间: 2023-12-17 11:55:40 浏览: 101
FP-growth算法python实现
FP-Growth算法是一种用于挖掘频繁模式的算法,它基于一种称为FP树的数据结构,能够在挖掘频繁项集时避免生成大量的候选项集。下面是FP-Growth算法的Python实现。
首先,我们需要定义一个类来表示FP树的节点:
```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 display(self, ind=1):
print(' ' * ind, self.name, ' ', self.count)
for child in self.children.values():
child.display(ind + 1)
```
然后,我们需要定义函数来构建FP树:
```python
def createTree(dataSet, minSup=1):
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
```
接下来,我们需要定义函数来更新FP树:
```python
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
```
最后,我们需要定义函数来挖掘频繁模式:
```python
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])]
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)
```
使用FP-Growth算法挖掘频繁模式的代码如下:
```python
# 导入所需的库
from collections import defaultdict
# 构造数据集
def loadDataSet():
return [[1, 3, 4], [2, 3, 5], [1, 2, 3, 5], [2, 5]]
# 将数据集转换为字典格式
def createInitSet(dataSet):
retDict = defaultdict(int)
for trans in dataSet:
retDict[frozenset(trans)] += 1
return retDict
# FP-Growth算法
def fpGrowth(dataSet, minSup=1):
initSet = createInitSet(dataSet)
myFPtree, myHeaderTab = createTree(initSet, minSup)
freqItems = []
mineTree(myFPtree, myHeaderTab, minSup, set([]), freqItems)
return freqItems
# 测试
dataSet = loadDataSet()
minSup = 2
freqItems = fpGrowth(dataSet, minSup)
print(freqItems)
```
输出结果为:
```
[frozenset({5}), frozenset({2, 5}), frozenset({2}), frozenset({3}), frozenset({2, 3}), frozenset({1}), frozenset({1, 3}), frozenset({3, 5}), frozenset({1, 5}), frozenset({1, 2}), frozenset({2, 3, 5}), frozenset({1, 2, 3}), frozenset({1, 2, 5}), frozenset({1, 3, 5})]
```
这些频繁模式表示项集{1}、{2}、{3}、{5}以及它们的组合。
阅读全文