Apriori关联算法简介

算法,机器学习 2016-01-31

Wiki链接:关联规则

  Apriori算法是基于关联分析的产生算法,关联分析即从数据集中寻找物品的隐含关系。比如超市对顾客的购买记录数据库进行关联规则挖掘,可以发现顾客的购买习惯。经典的有购物篮分析。Apriori算法提供了一种更高效的搜寻方法。   Apriori算法的主要作用在于发现频繁项集和关联规则,首先需要发现频繁项集(链接)。Apriori算法接受接受两个参数,最小支持度和数据集,通过扫描数据集发现满足最小支持度的项集。

  在找到频繁项集之后,可以根据任意其中一个发现关联规则,比如0,1,2和3不满足最小可信度,那么它们的子集也不满足,这样就有效地减少了计算。

  下面我们来看具体实现:

#ecoding:utf-8

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)#对于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): 
    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: #如果前k-2个项相同,则将他们合并
                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)#扫描数据集,从Ck得到Lk
        supportData.update(supK)
        L.append(Lk)
        k += 1
    return L, supportData
#用关联规则生成函数
def 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]
            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 = [] 
    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 prunedH

def rulesFromConseq(freqSet, H, supportData, brl, minConf=0.7):
    m = len(H[0])
    if (len(freqSet) > (m + 1)): #尝试进一步合并
        Hmp1 = aprioriGen(H, m+1)#创建 Hm+1 条新候选规则 
        Hmp1 = calcConf(freqSet, Hmp1, supportData, brl, minConf)
        if (len(Hmp1) > 1):    #至少需要两列合并
            rulesFromConseq(freqSet, Hmp1, supportData, brl, minConf)

本文由 Tony 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

如果对您有用,您的支持将鼓励我继续创作!