def loadDataSet():
return [[1, 3, 4], [2, 3, 5], [1, 2, 3, 5], [2, 5]]
def createC1(dataSet):
C1 = []
for transaction in dataSet:
for item in transaction:
if not [item] in C1:
C1.append([item])
C1.sort()
# map(frozenset, C1]用[frozenset(fset) for fset in C1]代替
代替原来的 if not ssCnt.has_key(can):
return [frozenset(fset) for fset in C1]# D = [set(fset) for fset in dataSet]def scanD(D, Ck, minSupport): ssCnt = {} for tid in D: for can in Ck: if can.issubset(tid): if not can in ssCnt:
#修改,代替原来的 if not ssCnt.has_key(can): ssCnt[can] = 1 else: ssCnt[can] += 1 numItems = float(len(D)) retList = [] supportData = {} for key in ssCnt: support = ssCnt[key] / numItems if support >= minSupport: retList.insert(0, key) supportData[key] = support return retList, supportDatadef aprioriGen(Lk, k): retList = [] lenLk = len(Lk) for i in range(lenLk): # 前k-2项相同时, 将两个集合合并 for j in range(i + 1, lenLk): L1 = list(Lk[i])[:k - 2] # k-2减少重复操作 L2 = list(Lk[j])[:k - 2] L1.sort() L2.sort() if L1 == L2: retList.append(Lk[i] | Lk[j]) return retListdef apriori(dataSet, minSupport=0.5): C1 = createC1(dataSet) D = [s for s in dataSet]
#修改处,原来是map(set,dataSet),估计是作者重写的函数 L1, supportData = scanD(D, C1, minSupport) L = [L1] k = 2 while (len(L[k - 2])) > 0: Ck = aprioriGen(L[k - 2], k) Lk, supK = scanD(D, Ck, minSupport) supportData.update(supK) L.append(Lk) k += 1 return L, supportData
问题1:
TypeError: object of type 'map' has no len()
原因:
map(set,dataSet),估计是作者重写的函数
问题2:
AttributeError: 'dict' object has no attribute 'has_key'
原因:
python3
dict没有hash\_key()方法,用
if not can in ssCnt:代替原来的 if not ssCnt.has_key