from numpy import *
def loadDataSet():
return [['A','C','D'],
['B','C','E'],
['A','B','C','E'],
['B','E']]
def create(dataSet):
C1 = []
for transaction in dataSet:
for item in transaction:
if not[item] in C1:
C1.append([item])
C1.sort()
return list(map(frozenset,C1))
def scanD(dataSet,Ck,minSupport):
ssCnt = {}
for tid in dataSet:
for can in Ck:
if can.issubset(tid):
ssCnt[can] = ssCnt.get(can,0) +1
retList=[]
supportData={}
for key in ssCnt:
support = ssCnt[key]
if support>=minSupport:
retList.insert(0,key)
supportData[key]=support
print(supportData)
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=2):
C1=create(dataSet)
D=list(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)
L.append(Lk);supportData.update(supK)
k+=1
return L,supportData
if __name__ == '__main__':
dataSet=loadDataSet()
L,supportData=apriori(dataSet,minSupport=2)
# print(L,supportData)
【无标题】
最新推荐文章于 2024-10-30 13:16:11 发布