import pandas as pd
dataSet =[['A','C','D'],['B','C','E'],['A','B','C','E'],['B','E']]
1、生成候选的1-项集
import itertools
defcreateC1(dataSet):# 'C1' for Candidate-itemset of 1 item.# Flatten the dataSet, leave unique item
C1 =set(itertools.chain(*dataSet))# Transform to a list of frozensetreturn[frozenset([i])for i in C1]
defscanD(dataSet, Ck, min_support):# 'Ck' for Candidate-set of k items.
support ={
}# Calculate the support of all itemsetsfor i in dataSet:for j in Ck:if j.issubset(i):
support[j]= support.get(j,0)+1
n =len(dataSet)# Return litemset with supportreturn{
k: v/n for k, v in support.items()if v/n >= min_support}