APRIORI算法就是关联分析的一种算法
主要概念:频繁项集,关联规则,支持度,置信度。
频繁项集:经常出现的一些集合
关联规则:意味这两种元素具有某种强烈的联系
支持度:数据集中包含该项集的记录占总记录的比例
置信度:对应支持度相除
详细代码
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):
ssCnt[can] = ssCnt.get(can, 0) + 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:
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 rulesFromConseq(freqSet, H, supportData, brl, minConf=0.7):
m = len(H[0])
if(len(freqSet) > (m + 1)):
Hmpl = aprioriGen(H, m + 1)
Hmpl = calcConf(freqSet, Hmpl, supportData, brl, minConf)
if (len(Hmpl) > 1):
rulesFromConseq(freqSet, Hmpl, supportData, brl, minConf)
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 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
if __name__ == '__main__':
dataSet = loadDataSet()
print "数据集是: ", dataSet
C1 = createC1(dataSet)
D = map(set, dataSet)
L1, suppDat = scanD(D, C1, 0.5)
print "只含一个元素的频繁项集", L1
L, suppData = apriori(dataSet)
print "所有频繁项集", L
print "===========================完美分割线========================="
rules = generateRules(L, suppData, minConf = 0.5)
print "===========================完美分割线========================="
print "关联规则树", rules
print "===========================完美分割线========================="
print "===========================完美分割线========================="
L, suppData = apriori(dataSet, minSupport=0.7)
print "所有频繁项集", L
print "===========================完美分割线========================="
rules = generateRules(L, suppData, minConf = 0.5)
print "===========================完美分割线========================="
print "关联规则树", rules
print "===========================完美分割线========================="