99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)大數(shù)據(jù)時(shí)代如何快速簡(jiǎn)單的理解FP-Growth關(guān)聯(lián)分析算法?
如何快速簡(jiǎn)單的理解FP-Growth關(guān)聯(lián)分析算法?
2020-07-23
收藏

最近小編了解到了一個(gè)的概念: FP-growth,廢話就不多說(shuō)了,直接把整理的FP-growth的干貨分享給大家。

一、FP-growth是什么

 FP-Growth(頻繁模式增長(zhǎng))算法是由韓家煒老師在2000年提出的關(guān)聯(lián)分析算法,它的分治策略為:將提供頻繁項(xiàng)集的數(shù)據(jù)庫(kù)壓縮到一棵頻繁模式樹(shù)(FP-Tree),但仍保留項(xiàng)集關(guān)聯(lián)信息。

FP-growth算法通常被用來(lái)挖掘頻繁項(xiàng)集,即從已給的多條數(shù)據(jù)記錄中,挖掘出哪些項(xiàng)是頻繁一起出現(xiàn)的。這種算法算法適用于標(biāo)稱型數(shù)據(jù),也就是離散型數(shù)據(jù)。其實(shí)我們經(jīng)常能接觸到FP-growth算法,就比如,我們?cè)诎俣鹊乃阉骺騼?nèi)輸入某個(gè)字或者詞,搜索引擎就會(huì)會(huì)自動(dòng)補(bǔ)全查詢?cè)~項(xiàng),往往這些詞項(xiàng)都是與搜索詞經(jīng)常一同出現(xiàn)的。

FP-growth算法源于Apriori的,是通過(guò)將數(shù)據(jù)集存儲(chǔ)在FP(Frequent Pattern)樹(shù)上發(fā)現(xiàn)頻繁項(xiàng)集,但缺點(diǎn)是,不能發(fā)現(xiàn)數(shù)據(jù)之間的關(guān)聯(lián)規(guī)則。與Apriori相比,F(xiàn)P-growth算法更為高效,因?yàn)镕P-growth算法只需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行兩次掃描,而Apriori算法在求每個(gè)潛在的頻繁項(xiàng)集時(shí)都需要掃描一次數(shù)據(jù)集。

二、FP-Tree算法基本結(jié)構(gòu)

FPTree算法的基本數(shù)據(jù)結(jié)構(gòu),包含一個(gè)一棵FP樹(shù)和一個(gè)項(xiàng)頭表,每個(gè)項(xiàng)通過(guò)一個(gè)結(jié)點(diǎn)鏈指向它在樹(shù)中出現(xiàn)的位置?;窘Y(jié)構(gòu)如下所示。需要注意的是項(xiàng)頭表需要按照支持度遞減排序,在FPTree中高支持度的節(jié)點(diǎn)只能是低支持度節(jié)點(diǎn)的祖先節(jié)點(diǎn)。

FP-Tree:即上面的那棵樹(shù),是把事務(wù)數(shù)據(jù)表中的各個(gè)事務(wù)數(shù)據(jù)項(xiàng)按照支持度排序后,把每個(gè)事務(wù)中的數(shù)據(jù)項(xiàng)按降序依次插入到一棵以NULL為根結(jié)點(diǎn)的樹(shù)中,同時(shí)在每個(gè)結(jié)點(diǎn)處記錄該結(jié)點(diǎn)出現(xiàn)的支持度。

條件模式基:包含F(xiàn)P-Tree中與后綴模式一起出現(xiàn)的前綴路徑的集合。即同一個(gè)頻繁項(xiàng)在PF樹(shù)中的所有節(jié)點(diǎn)的祖先路徑的集合。例如I3在FP樹(shù)中總共出現(xiàn)了3次,其祖先路徑分別是{I2.I1:2(頻度為2)},{I2:2}和{I1:2}。這3個(gè)祖先路徑的集合就是頻繁項(xiàng)I3的條件模式基。

條件樹(shù):將條件模式基按照FP-Tree的構(gòu)造原則形成的一個(gè)新的FP-Tree。比如上圖中I3的條件樹(shù)就是。

三、FP-growth算法

FP-growth算法挖掘頻繁項(xiàng)集的基本過(guò)程分為兩步:

(1)構(gòu)建FP樹(shù)。

首先構(gòu)造FP樹(shù),然后利用它來(lái)挖掘頻繁項(xiàng)集。在構(gòu)造FP樹(shù)時(shí),需要對(duì)數(shù)據(jù)集掃描兩邊,第一遍掃描用來(lái)統(tǒng)計(jì)頻率,第二遍掃描至考慮頻繁項(xiàng)集。

(2)從FP樹(shù)中挖掘頻繁項(xiàng)集。

首先,獲取條件模式基。條件模式基是以所查找元素項(xiàng)為結(jié)尾的路徑集合,表示的是所查找的元素項(xiàng)與樹(shù)根節(jié)點(diǎn)之間的所有內(nèi)容。

其次,構(gòu)建條件模式基。對(duì)于每一個(gè)頻繁項(xiàng),都需要?jiǎng)?chuàng)建一棵條件FP樹(shù),使用創(chuàng)建的條件模式基作為輸入,采用相同的建樹(shù)代碼來(lái)構(gòu)建樹(shù),相應(yīng)的遞歸發(fā)現(xiàn)頻繁項(xiàng)、發(fā)現(xiàn)條件模式基和另外的條件樹(shù)。

四、python代碼實(shí)現(xiàn)


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):
        print '  '*ind, self.name, ' ', self.count
        for child in self.children.values():
            child.disp(ind+1)
def updateHeader(nodeToTest, targetNode):
    while nodeToTest.nodeLink != None:
        nodeToTest = nodeToTest.nodeLink
    nodeToTest.nodeLink = targetNode
def updateFPtree(items, inTree, headerTable, count):
    if items[0] in inTree.children:
        # 判斷items的第一個(gè)結(jié)點(diǎn)是否已作為子結(jié)點(diǎn)
        inTree.children[items[0]].inc(count)
    else:
        # 創(chuàng)建新的分支
        inTree.children[items[0]] = treeNode(items[0], count, inTree)
        # 更新相應(yīng)頻繁項(xiàng)集的鏈表,往后添加
        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:
        updateFPtree(items[1::], inTree.children[items[0]], headerTable, count)
 
def createFPtree(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():
        if headerTable[k] < minSup:
            del(headerTable[k]) # 刪除不滿足最小支持度的元素
    freqItemSet = set(headerTable.keys()) # 滿足最小支持度的頻繁項(xiàng)集
    if len(freqItemSet) == 0:
        return None, None
    for k in headerTable:
        headerTable[k] = [headerTable[k], None] # element: [count, node]
 
    retTree = treeNode('Null Set', 1, None)
    for tranSet, count in dataSet.items():
        # dataSet:[element, count]
        localD = {}
        for item in tranSet:
            if item in freqItemSet: # 過(guò)濾,只取該樣本中滿足最小支持度的頻繁項(xiàng)
                localD[item] = headerTable[item][0] # element : count
        if len(localD) > 0:
            # 根據(jù)全局頻數(shù)從大到小對(duì)單樣本排序
            orderedItem = [v[0] for v in sorted(localD.items(), key=lambda p:p[1], reverse=True)]
            # 用過(guò)濾且排序后的樣本更新樹(shù)
            updateFPtree(orderedItem, retTree, headerTable, count)
    return retTree, headerTable
def loadSimpDat():
    simDat = [['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 simDat
# 構(gòu)造成 element : count 的形式
def createInitSet(dataSet):
    retDict={}
    for trans in dataSet:
        key = frozenset(trans)
        if retDict.has_key(key):
            retDict[frozenset(trans)] += 1
        else:
            retDict[frozenset(trans)] = 1
    return retDict
# 數(shù)據(jù)集
def loadSimpDat():
    simDat = [['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 simDat
# 構(gòu)造成 element : count 的形式
def createInitSet(dataSet):
    retDict={}
    for trans in dataSet:
        key = frozenset(trans)
        if retDict.has_key(key):
            retDict[frozenset(trans)] += 1
        else:
            retDict[frozenset(trans)] = 1
    return retDict
# 遞歸回溯
def ascendFPtree(leafNode, prefixPath):
    if leafNode.parent != None:
        prefixPath.append(leafNode.name)
        ascendFPtree(leafNode.parent, prefixPath)
# 條件模式基
def findPrefixPath(basePat, myHeaderTab):
    treeNode = myHeaderTab[basePat][1] # basePat在FP樹(shù)中的第一個(gè)結(jié)點(diǎn)
    condPats = {}
    while treeNode != None:
        prefixPath = []
        ascendFPtree(treeNode, prefixPath) # prefixPath是倒過(guò)來(lái)的,從treeNode開(kāi)始到根
        if len(prefixPath) > 1:
            condPats[frozenset(prefixPath[1:])] = treeNode.count # 關(guān)聯(lián)treeNode的計(jì)數(shù)
        treeNode = treeNode.nodeLink # 下一個(gè)basePat結(jié)點(diǎn)
    return condPats
def mineFPtree(inTree, headerTable, minSup, preFix, freqItemList):
    # 最開(kāi)始的頻繁項(xiàng)集是headerTable中的各元素
    bigL = [v[0] for v in sorted(headerTable.items(), key=lambda p:p[1])] # 根據(jù)頻繁項(xiàng)的總頻次排序
    for basePat in bigL: # 對(duì)每個(gè)頻繁項(xiàng)
        newFreqSet = preFix.copy()
        newFreqSet.add(basePat)
        freqItemList.append(newFreqSet)
        condPattBases = findPrefixPath(basePat, headerTable) # 當(dāng)前頻繁項(xiàng)集的條件模式基
        myCondTree, myHead = createFPtree(condPattBases, minSup) # 構(gòu)造當(dāng)前頻繁項(xiàng)的條件FP樹(shù)
        if myHead != None:
            # print 'conditional tree for: ', newFreqSet
            # myCondTree.disp(1)
            mineFPtree(myCondTree, myHead, minSup, newFreqSet, freqItemList) # 遞歸挖掘條件FP樹(shù)


數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }