机器学习之频繁模式树(二)

上一篇博客主要介绍了频繁模式树的建立过程,那么这颗频繁模式树究竟能够表现出什么哪?先看下图的一颗频繁模式树。

频繁项头表的每一行都是相同的频繁项,用一个有向链表表示相互联系,用于表示树中频繁项的映射。每一个数据项都有一组前缀路径,由根节点到所有该数据项节点的路径及该数据项节点的次数组成,如下图所示:
机器学习之频繁模式树(二)_第1张图片
那么前缀路径表示什么意思?很明显,对于t的一个前缀路径{z,x,y,s} 2,就能表示一个数据集中的两个事务是{z,x,y,s,t},对于所有的前缀路径,我们可以建造一颗频繁模式树,这棵树叫做条件频繁模式树,所谓的条件就是在已知前缀路径能够推出t满足最小门限值的意思,因此上述问题可以描述为{z,x,y,s}->t的支持度是2,{z,x,y,r}->t的支持度是1,因此我们可以根据前缀路径建造一颗频繁模式树(和前面建造的方式是一样的),如果我们设置门限值是3很显然,在构造树的时候s与r会被去除,这样我们就得到{z,x,y,t}({z,x,y}->t)的支持度是3,接着{x,t}支持度3{{x}->{y}},在接着{z,x,t}支持度3{{z,x}->{t}}和{y,x,t}支持度3{{y,x}->{t}}……..,同样对新生成的数据(条件频繁模式树,频繁项头表)继续进行挖掘,知道仅剩一个事务项为止。还是建议仔细研究代码,在读代码的过程中可以加深理解。

''' Created on Jun 14, 2011 FP-Growth FP means frequent pattern the FP-Growth algorithm needs: 1. FP-tree (class treeNode) 2. header table (use dict) This finds frequent itemsets similar to apriori but does not find association rules. @author: Peter '''
class treeNode:
    def __init__(self, nameValue, numOccur, parentNode):
        self.name = nameValue
        self.count = numOccur
        self.nodeLink = None
        self.parent = parentNode      #needs to be updated
        self.children = {}

    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): #create FP-tree from dataset but don't mine
    headerTable = {}
    #go over dataSet twice
    for trans in dataSet:#first pass counts frequency of occurance
        for item in trans:
            headerTable[item] = headerTable.get(item, 0) + dataSet[trans]
    for k in headerTable.keys():  #remove items not meeting minSup
        if headerTable[k] < minSup:
            del(headerTable[k])
    freqItemSet = set(headerTable.keys())
    if len(freqItemSet) == 0: return None, None  #if no items meet min support -->get out
    for k in headerTable:
        headerTable[k] = [headerTable[k], None] #reformat headerTable to use Node link
    retTree = treeNode('Null Set', 1, None) #create tree
    for tranSet, count in dataSet.items():  #go through dataset 2nd time
        localD = {}
        for item in tranSet:  #put transaction items in order
            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)#populate tree with ordered freq itemset
    return retTree, headerTable #return tree and header table

def updateTree(items, inTree, headerTable, count):
    if items[0] in inTree.children:#check if orderedItems[0] in retTree.children
        inTree.children[items[0]].inc(count) #incrament count
    else:   #add items[0] to inTree.children
        inTree.children[items[0]] = treeNode(items[0], count, inTree)
        if headerTable[items[0]][1] == None: #update header table
            headerTable[items[0]][1] = inTree.children[items[0]]
        else:
            updateHeader(headerTable[items[0]][1], inTree.children[items[0]])
    if len(items) > 1:#call updateTree() with remaining ordered items
        updateTree(items[1::], inTree.children[items[0]], headerTable, count)

def updateHeader(nodeToTest, targetNode):   #this version does not use recursion
    while (nodeToTest.nodeLink != None):    #Do not use recursion to traverse a linked list!
        nodeToTest = nodeToTest.nodeLink
    nodeToTest.nodeLink = targetNode

def ascendTree(leafNode, prefixPath): #ascends from leaf node to root
    if leafNode.parent != None:
        prefixPath.append(leafNode.name)
        ascendTree(leafNode.parent, prefixPath)

def findPrefixPath(basePat, treeNode): #treeNode comes from header table
    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, i=1):
    bigL = [v[0] for v in sorted(headerTable.items(), key=lambda p: p[1])]#(sort header table)
    # print 'bigL',sorted(headerTable.items(), key=lambda p: p[1])

    for basePat in bigL:  #start from bottom of header table
        newFreqSet = preFix.copy()
        newFreqSet.add(basePat)
        print 'newFreqSet',newFreqSet
        freqItemList.append(newFreqSet)
        print 'freqItemList',freqItemList
        condPattBases = findPrefixPath(basePat, headerTable[basePat][1])
        # print 'condPattBases',condPattBases
        print 'create ', i
        i = i+1
        print 'basePat', basePat
        print 'condPattBases', condPattBases
        myCondTree, myHead = createTree(condPattBases, minSup)
        # myCondTree.disp()
        # print 'myCondTree',myCondTree
        print 'myHead', myHead
        if myHead != None: #3. mine cond. FP-tree
            # print 'yy'
            mineTree(myCondTree, myHead, minSup, newFreqSet, freqItemList, i)
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

minSup = 3
simpDat = loadSimpDat()
initSet = createInitSet(simpDat)
myFPtree, myHeaderTab = createTree(initSet, minSup)
# myFPtree.disp()
# print findPrefixPath('x', myHeaderTab['x'][1])
# print findPrefixPath('z', myHeaderTab['z'][1])
# print findPrefixPath('r', myHeaderTab['r'][1])
freqItems = []
mineTree(myFPtree, myHeaderTab, 3, set([]), freqItems)
print freqItems
# myFreqList = []
# mineTree(myFPtree, myHeaderTab, minSup, set([]), myFreqList)
# {x,s}, {z,x,y}, and {z}

你可能感兴趣的:(python,机器学习,FP-Tree,频繁模式树)