发布网友 发布时间:2022-04-18 13:14
共1个回答
热心网友 时间:2022-04-18 14:44
classApriori(object):def__init__(self,filename,min_support,item_start,item_end):self.filename=filenameself.min_support=min_support#最小支持度self.min_confidence=50self.line_num=0#item的行数self.item_start=item_start#取哪行的itemself.item_end=item_endself.location=[[i]foriinrange(self.item_end-self.item_start+1)]self.support=self.sut(self.location)self.num=list(sorted(set([jforiinself.locationforjini])))#记录itemself.pre_support=[]#保存前一个support,location,numself.pre_location=[]self.pre_num=[]self.item_name=[]#项目名self.find_item_name()self.loop()self.confidence_sup()defdeal_line(self,line):"提取出需要的项"return[i.strip()foriinline.split('')ifi][self.item_start-1:self.item_end]deffind_item_name(self):"根据第一行抽取item_name"withopen(self.filename,'r')asF:forindex,lineinenumerate(F.readlines()):ifindex==0:self.item_name=self.deal_line(line)breakdefsut(self,location):"""输入[[1,2,3],[2,3,4],[1,3,5]]输出每个位置集的support[123,435,234]"""withopen(self.filename,'r')asF:support=[0]*len(location)forindex,lineinenumerate(F.readlines()):ifindex==0:continue#提取每信息item_line=self.deal_line(line)forindex_num,iinenumerate(location):flag=0forjini:ifitem_line[j]!='T':flag=1breakifnotflag:support[index_num]+=1self.line_num=index#一共多少行,出去第一行的item_namereturnsupportdefselect(self,c):"返回位置"stack=[]foriinself.location:forjinself.num:ifjini:iflen(i)==c:stack.append(i)else:stack.append([j]+i)#多重列表去重importitertoolss=sorted([sorted(i)foriinstack])location=list(sfors,_initertools.groupby(s))returnlocationdefdel_location(self,support,location):"清除不满足条件的候选集"#小于最小支持度的剔除forindex,iinenumerate(support):ifiself.min_confidence:print','.join(s),'->>',self.item_name[each_location[index]],'min_support:',str(support)+'%','min_confidence:',str(confidence)+'%'defmain():c=Apriori('basket.txt',14,3,13)d=Apriori('simple.txt',50,2,6)if__name__=='__main__':main()Apriori(filename,min_support,item_start,item_end)参数说明filename:(路径)文件名min_support:最小支持度item_start:item起始位置item_end:item结束位置importaprioric=apriori.Apriori('basket.txt',11,3,13)输出: