Apriori algorithm and Python implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.