import numpy as np
class treeNode:
def __init__(self, nameValue, numOccur, parentNode):
self.name = nameValue
self.count = numOccur
self.nodeLink = None
self.parent = parentNode
self.children = {}
def inc(self, numOccur):#增加当前节点的值
self.count += numOccur
def disp(self, ind = 1):#打印树,ind设置成0也可以吗?????
print(' '*ind, self.name, ' ', self.count)
for child in self.children.values():
child.disp(ind + 1)#递归
def createTree(dataSet, minSup = 1):#扫描头指针表,删掉哪些出现次数少于minSup的项
headerTable = {}
for trans in dataSet:
for item in trans:
#https://www.runoob.com/python/att-dictionary-get.html
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())#通过minSup筛选的频繁集
if len(freqItemSet) == 0:#如果每个元素只出现一次,那就直接返回None(没有规律可循)
return None, None
for k in headerTable:
headerTable[k] = [headerTable[k], None]#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:#如果在节点中,直接更新节点count
inTree.children[items[0]].inc(count)
else:
inTree.children[items[0]] = treeNode(items[0], count, inTree)#如果没有则新增节点
if headerTable[items[0]][1] == None:#当前item不属于频繁集中,更新headerTable
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): #headerTable中存储的是元素链表的起始指针
condPats = {}
while treeNode != None:
prefixPath = []
ascendTree(treeNode, prefixPath)
if len(prefixPath) > 1:
condPats[frozenset(prefixPath[1:])] = treeNode.count
treeNode = treeNode.nodeLink
return condPats
def loadSimpDat():
simpDat = [['r', 'z', 'h', 'j', 'p'],
['z', 'y', 'x', 'w', 'v', 'u', 't', 's'],
['z'],
['r', 'x', 'n', 'o', 's'],
['y', 'r', 'x', 'z', 'q', 't', 'p'],
['y', 'z', 'x', 'e', 'q', 's', 't', 'm']]
return simpDat
def createInitSet(dataSet):
retDict = {}
for trans in dataSet:
retDict[frozenset(trans)] = 1
return retDict
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)#根据前缀路径构建条件FP树
if myHead != None:
mineTree(myCondTree, myHead, minSup, newFreqSet, freqItemList)#从二元扩展到三元,从三元扩展到四元,以此类推递归
return
if __name__ == "__main__":
simpDat = loadSimpDat()
initSet = createInitSet(simpDat)
myFPtree, myHeaderTab = createTree(initSet, 3)
#myFPtree.disp()
#res = findPrefixPath('r', myHeaderTab['r'][1])
freqItem = []
mineTree(myFPtree, myHeaderTab, 3, set(), freqItem)
print(freqItem)