简单实现FP-Growth算法,要求用Python实现,数据集可以手动输入
时间: 2024-05-06 16:14:49 浏览: 85
好的,我可以为您提供一个简单的 Python 实现 FP-Growth 算法的代码示例,并且可以手动输入数据集。
```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)
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.keys()):
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])]
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)
dataSet = {'1':4, '2':4, '3':3, '4':1, '5':1, '6':2, '7':2, '8':1, '9':1, '10':1}
minSup = 3
retTree, headerTable = createTree(dataSet, minSup)
freqItems = []
mineTree(retTree, headerTable, minSup, set([]), freqItems)
print(freqItems)
```
以上代码实现了 FP-Growth 算法,并且可以手动输入数据集。您只需将数据集存储在一个字典中,其中键为事务,值为该事务出现的次数。在本例中,数据集为:
```python
dataSet = {'1':4, '2':4, '3':3, '4':1, '5':1, '6':2, '7':2, '8':1, '9':1, '10':1}
```
其中,键为事务,值为该事务出现的次数。最小支持度为 3。通过运行上述代码,可以得到频繁项集列表:
```python
[{'1'}, {'2'}, {'3'}, {'1', '2'}, {'1', '3'}, {'2', '3'}]
```
这表示项集 {1, 2}、{1, 3} 和 {2, 3} 都是频繁项集,它们的支持度都大于等于最小支持度 3。
阅读全文