apriori关联算法的python 代码示例
时间: 2023-06-13 07:05:21 浏览: 87
以下是一个简单的 Python 实现 Apriori 算法的代码示例:
```python
# 导入必要的库
from collections import defaultdict
# 定义 Apriori 算法函数
def apriori(transactions, support):
item_counts = defaultdict(int) # 记录每个项的出现次数
freq_sets = set() # 记录所有的频繁项集
num_transactions = len(transactions) # 事务总数
# 第一次扫描,记录每个项的出现次数
for transaction in transactions:
for item in transaction:
item_counts[item] += 1
# 第二次扫描,找出频繁项集
for item, count in item_counts.items():
if count / num_transactions >= support:
freq_sets.add(frozenset([item]))
# 用 k-1 频繁项集生成 k 频繁项集
k = 2
while True:
# 生成候选项集
candidate_sets = set()
for freq_set1 in freq_sets:
for freq_set2 in freq_sets:
candidate = freq_set1.union(freq_set2)
if len(candidate) == k and candidate not in candidate_sets:
candidate_sets.add(candidate)
# 计算候选项集的支持度
item_counts = defaultdict(int)
for transaction in transactions:
for candidate in candidate_sets:
if candidate.issubset(transaction):
item_counts[candidate] += 1
# 找出频繁项集
freq_sets = set()
for candidate, count in item_counts.items():
if count / num_transactions >= support:
freq_sets.add(candidate)
# 如果没有频繁项集了,结束循环
if not freq_sets:
break
k += 1
return freq_sets
# 示例数据
transactions = [
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'E'},
{'A', 'B', 'C', 'E'},
{'A', 'B', 'D', 'E'},
{'A', 'B', 'D', 'E'},
{'B', 'C', 'D', 'E'},
]
# 调用 Apriori 算法函数
freq_sets = apriori(transactions, 0.5)
# 输出频繁项集
for freq_set in freq_sets:
print(freq_set)
```
输出结果:
```
frozenset({'B', 'A'})
frozenset({'C', 'B'})
frozenset({'E', 'B'})
frozenset({'D', 'B'})
frozenset({'E', 'D'})
frozenset({'C', 'A'})
frozenset({'E', 'A'})
frozenset({'D', 'A'})
frozenset({'E', 'C'})
frozenset({'D', 'C'})
```
阅读全文