项集挖掘——Eclat->dEclat的python实现

def Sigma(sequence):
sigma=set()
for itemset in sequence:
for item in itemset:
if item not in sigma:
sigma.add(item)
return sigma

def VirticalDataset(sequence,sigma,minsup):
dic={}
for item in sigma:
itemset=set()
for i in range(len(sequence)):
if item in sequence[i]:
itemset.add(i)
if len(itemset)>=minsup:
dic[item]=itemset
return dic

def UniversalSet(sequence):
dic={}
list=[]
s=set()
for i in range(len(sequence)):
s.add(i)
list.append(s)
list.append(len(s))
dic[’ ']=list
return dic

def changed(tao,dic):
for name in dic.keys():
list=[]
list.append(tao[’ '][0]-dic[name])
list.append(len(dic[name]))
dic[name]=list
return dic

def dElact(minsup,wait,dic):
kdic={}
sup=0
d=set()
for waitname in wait.keys():
for name in dic.keys():
if name not in waitname and name>waitname[len(waitname)-1]:
list = []
d = dic[name][0]
for i in range(len(waitname)):
d=d-dic[waitname[i]][0]
sup=wait[waitname][1]-len(d)
if sup>=minsup:
list.append(d)
list.append(sup)
kdic[waitname+name]=list
return kdic;

if name == ‘main’:
minsup=3
sequence=[[‘A’,‘B’,‘D’,‘E’],[‘B’,‘C’,‘E’],[‘A’,‘B’,‘D’,‘E’],[‘A’,‘B’,‘C’,‘E’],[‘A’,‘B’,‘C’,‘D’,‘E’],[‘B’,‘C’,‘D’]]
sigma=Sigma(sequence)
dic=VirticalDataset(sequence,sigma,minsup)
tao=UniversalSet(sequence)
dic=changed(tao,dic)
print(dic)
sequentialitem = dElact(minsup, dic, dic)
print(sequentialitem)
sequentialitem = dElact(minsup,sequentialitem,dic)
print(sequentialitem)
sequentialitem = dElact(minsup, sequentialitem, dic)
print(sequentialitem)

最后几行完全可以使用递归,这里懒得写了。

本人才疏学浅,如有问题还望即使纠正。

你可能感兴趣的:(初学萌新,项集挖掘)