Apriori算法

来源:互联网 发布:自媒体是什么 知乎 编辑:程序博客网 时间:2024/06/05 03:15
假如有4条记录[[1, 3, 4], [2, 3, 5], [1, 2, 3, 5], [2, 5]]那么集合{1}的支持度为2/4,即出现记次数/总记录数。一条规则P-H的可信度为 support(P&&H)/support(P) 机器学习实战的算法首先枚举了所有计算出来满足支持度的记录,然后以枚举的为P&&H,然后再枚举子集,用P&&H/(P&&H-子集)计算可信度。
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, supportDatadef aprioriGen(Lk, k):    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 retListdef 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, supportDatadef generateRules(L, supportData, minConf=0.7):    bigRuleList = []    for i in range(1, len(L)):        for freqSet in L[i]:            H1 = [frozenset([item]) for item in freqSet]            print(H1)            if (i > 1):                rulesFromConseq(freqSet, H1, supportData, bigRuleList, minConf)            else:                calcConf(freqSet, H1, supportData, bigRuleList, minConf)    return bigRuleListdef calcConf(freqSet, H, supportData, brl, minConf=0.7):    prunedH = []    for conseq in H:        conf = supportData[freqSet]/supportData[freqSet-conseq]        if conf >= minConf:            print freqSet-conseq,'-->',conseq,'conf:',conf            brl.append((freqSet-conseq, conseq, conf))            prunedH.append(conseq)    return prunedHdef rulesFromConseq(freqSet, H, supportData, brl, minConf=0.7):    m = len(H[0])    if (len(freqSet) > (m + 1)):        Hmp1 = aprioriGen(H, m+1)        Hmp1 = calcConf(freqSet, Hmp1, supportData, brl, minConf)        if (len(Hmp1) > 1):            rulesFromConseq(freqSet, Hmp1, supportData, brl, minConf)def pntRules(ruleList, itemMeaning):    for ruleTup in ruleList:        for item in ruleTup[0]:            print itemMeaning[item]        print "           -------->"        for item in ruleTup[1]:            print itemMeaning[item]        print "confidence: %f" % ruleTup[2]        print
0 0
原创粉丝点击