1 Apriori Introduction
The Apriori algorithm uses a priori knowledge of frequent itemsets, using an iterative approach called layer-wise search, and K-itemsets are used to explore (k+1) itemsets. First, by scanning the transaction (transaction) records, find all the frequent 1 itemsets, the collection is L1, and then use L1 to find the collection of frequent 2 itemsets l2,l2 find L3, so go on until you can no longer find any frequent k itemsets. Finally, the strong rules are found in all the frequent concentrations, that is, the association rules that generate the user's interest.
2. Algorithm Simulation
3. Pseudo-code
4.python implementations
#-*-coding:gb2312-*-ImportSYSImportCopydefInit_pass (T): C= {}#c is a dictionary forTinchT: forIinchT:ifIinchC.keys (): C[i]+ = 1Else: C[i]= 1returnCdefGenerate (F): C=[] k= Len (f[0]) + 1 forF1inchF: forF2inchF:ifF1[k-2] < F2[k-2]: C=copy.copy (F1) c.append (f2[k-2]) Flag=True forIinchRange (0,k-1): S=copy.copy (c) s.pop (i)ifS not inchF:flag=False Break ifFlag andC not inchc:c.append (C)returnCdefComparelist (A, b):ifLen (A) <=Len (B): forAinchA:ifA not inchB:returnFalseElse: forBinchB:ifB not inchA:returnFalsereturnTruedefApriori (t,minsupport): D=[] C=Init_pass (T) keys=c.keys ();#the. Keys () method to find the index in the dictionaryKeys.sort () d.append (keys)#Join D concentrationf=[[]] forFinchD[0]:ifc[f]>=minsupport:f[0].append ([F]) K=1 whilef[k-1]!=[]: D.append (Generate (F[k-1])) f.append ([]) forCinchD[k]: Count=0; forTinchT:ifcomparelist (c,t): Count+ = 1ifcount>=Minsupport:f[k].append (c) k+ = 1U= [] forFinchF: forXinchf:u.append (x)returnUT= [['A','C','D'],['B','C','E'],['A','B','C','E'],['B','E']]z= Apriori (t,2)PrintZ
4. Output results
Apriori algorithm and Python implementation