apriori算法的代码,python实现,参考《机器学习实战》

Posted 杨东冀@pku

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apriori算法的代码,python实现,参考《机器学习实战》相关的知识,希望对你有一定的参考价值。

from numpy import *
def loadDataSet():
    return [[1,3,4],[2,3,5],[1,2,3,5],[2,5]]

def createC1(dataSet):
    C1 = []
    for transaction in dataSet:
        for item in transaction:
            if not [item] in C1:
                C1.append([item])
    C1.sort()
    return map(frozenset , C1)

def scanD(D,Ck,minSupport):
    ssCnt = 
    for tid in D:
        for can  in Ck:
            if can.issubset(tid):
                if not ssCnt.has_key(can): 
                    ssCnt[can] =1
                else:
                    ssCnt[can] += 1
    numItems = float(len(D)) 
    retList = []
    supportData = 
    for key in ssCnt:
        support = ssCnt[key]/numItems
        if support >=minSupport:
            retList.insert(0,key)
        supportData[key] = support
    return retList,supportData

def aprioriGen(Lk,k): #create CK
    retList = []
    lenLk = len(Lk)
    for i in range(lenLk):
        for j in range(i+1,lenLk):
            L1 = list(Lk[i])[:k-2] ; L2 = list(Lk[j])[:k-2]
            L1.sort(); L2.sort()
            if L1==L2:
                retList.append(Lk[i] | Lk[j])
    return retList

def apriori(dataSet , minSupport =0.5):
    C1 = createC1(dataSet)
    D = map(set,dataSet)
    L1,supportData = scanD(D,C1,minSupport)
    L = [L1]
    k = 2
    while (len(L[k-2]) >0 ):
        Ck = aprioriGen(L[k - 2],k)
        Lk ,supK = scanD(D , Ck,minSupport)
        supportData.update(supK)
        L.append(Lk)
        k += 1
    return L, supportData 

def generateRules(L, supportData, minConf=0.7):  #supportData is a dict coming from scanD
    bigRuleList = []
    for i in range(1, len(L)):#only get the sets with two or more items
        for freqSet in L[i]:
            H1 = [frozenset([item]) for item in freqSet]
            if (i > 1):
                rulesFromConseq(freqSet, H1, supportData, bigRuleList, minConf)
            else:
                calcConf(freqSet, H1, supportData, bigRuleList, minConf)
    return bigRuleList         

def calcConf(freqSet, H, supportData, brl, minConf=0.7):
    prunedH = [] #create new list to return
    for conseq in H:
        conf = supportData[freqSet]/supportData[freqSet-conseq] #calc confidence
        if conf >= minConf: 
            print freqSet-conseq,'-->',conseq,'conf:',conf
            brl.append((freqSet-conseq, conseq, conf))
            prunedH.append(conseq)
    return prunedH

def rulesFromConseq(freqSet, H, supportData, brl, minConf=0.7):
    m = len(H[0])
    if (len(freqSet) > (m + 1)): #try further merging
        Hmp1 = aprioriGen(H, m+1)#create Hm+1 new candidates
        Hmp1 = calcConf(freqSet, Hmp1, supportData, brl, minConf)
        if (len(Hmp1) > 1):    #need at least two sets to merge
            rulesFromConseq(freqSet, Hmp1, supportData, brl, minConf)

以上是关于apriori算法的代码,python实现,参考《机器学习实战》的主要内容,如果未能解决你的问题,请参考以下文章

急需C++实现的Apriori算法代码

Apriori 算法python实现

实现Apriori算法(python)

Apriori算法--Python实现

Apriori算法思想和其python实现

python实现apriori算法的关联规则之支持度置信度提升度