apriori算法现成代码
时间: 2023-11-12 07:05:19 浏览: 123
以下是Python中的一个简单实现:
```python
# Apriori算法实现
def apriori(transactions, support_threshold):
# 构建集合C1
C1 = {}
for transaction in transactions:
for item in transaction:
if item in C1:
C1[item] += 1
else:
C1[item] = 1
# 满足支持度阈值的集合L1
L1 = {}
for item in C1:
if C1[item] >= support_threshold:
L1[item] = C1[item]
# 构建频繁项集列表
frequent_items = [L1]
k = 2
while True:
# 构建集合Ck
Ck = {}
for i in range(len(frequent_items[k-2])):
for j in range(i+1, len(frequent_items[k-2])):
itemset1 = list(frequent_items[k-2].keys())[i]
itemset2 = list(frequent_items[k-2].keys())[j]
new_itemset = set(itemset1).union(set(itemset2))
if len(new_itemset) == k and new_itemset not in Ck:
Ck[new_itemset] = 0
# 计算支持度
for transaction in transactions:
for itemset in Ck:
if itemset.issubset(transaction):
Ck[itemset] += 1
# 满足支持度阈值的集合Lk
Lk = {}
for itemset in Ck:
if Ck[itemset] >= support_threshold:
Lk[itemset] = Ck[itemset]
# 如果不存在满足支持度阈值的项集,则停止
if not Lk:
break
frequent_items.append(Lk)
k += 1
return frequent_items
```
使用示例:
```python
transactions = [
{'A', 'B', 'C'},
{'A', 'C'},
{'A', 'D'},
{'B', 'C'},
{'B', 'D'},
{'C', 'D'},
{'A', 'B', 'C', 'D'}
]
support_threshold = 2
frequent_items = apriori(transactions, support_threshold)
for itemset in frequent_items:
print(itemset)
```
输出:
```
{'A': 4, 'B': 3, 'C': 4, 'D': 2}
{('A', 'B'): 2, ('A', 'C'): 3, ('B', 'C'): 3, ('C', 'D'): 2, ('A', 'D'): 2, ('B', 'D'): 2}
{('A', 'B', 'C'): 2}
```
阅读全文