#coding=utf-8
#树节点结构定义
class treeNode:
def __init__(self, nameValue, numOccur, parentNode):
self.name = nameValue
self.count = numOccur
self.nodeLink = None
self.parent = parentNode
self.children = {}
#方法inc()对count变量值增加
def inc(self, numOccur):
self.count += numOccur
#将树以文本形式显示
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 = 1):
headerTable = {} #创建字典
for trans in dataSet: #第一次遍历扫描数据集并统计每个元素出现的次数
for item in trans:
headerTable[item] = headerTable.get(item, 0) + dataSet[trans]
for k in headerTable.keys(): #keys将字典中的建以列表形式返回,去除不满足minSup的项
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(): #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 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 ascendTree(leafNode, prefixPath):
if leafNode.parent != None:
prefixPath.append(leafNode.name)
ascendTree(leafNode.parent, prefixPath)
#遍历链表,直到结尾
def findPrefixPath(basePat, treeNode):
condPaths = {}
while treeNode != None:
prefixPath = []
ascendTree(treeNode, prefixPath)
if len(prefixPath) > 1:
condPaths[frozenset(prefixPath[1:])] = treeNode.count
treeNode = treeNode.nodeLink
return condPaths
#递归查找频繁项集的minTree函数
def mineTree(inTree, headerTable, minSup, preFix, freqItemList):
bigL = [v[0] for v in sorted(headerTable.items(), key = lambda p:p[1])] #lambda是一个隐函数,是固定写法
#对指针表中的元素项按其出现频率排序
for basePat in bigL:
newFreqSet = preFix.copy()
newFreqSet.add(basePat)
freqItemList.append(newFreqSet) #将每一个频繁项添加到频繁项集列表中
condPattBases = findPrefixPath(basePat, headerTable[basePat][1]) #步骤1寻找其条件模式基
myCondTree, myHead = createTree(condPattBases, minSup) #步骤2,利用条件模式基创建条件树
if myHead != None:
mineTree(myCondTree, myHead, minSup, newFreqSet, freqItemList) #递归调用,重复步骤1,2直至只有一个元素
总结