关联规则apriori算法python代码
时间: 2023-09-15 21:24:15 浏览: 134
Apriori算法(基于Python编程语言实现)
5星 · 资源好评率100%
当然,我可以为您提供关联规则apriori算法的Python代码。以下是一个简单的示例:
```python
from itertools import combinations
from collections import defaultdict
def generate_candidates(freq_sets, k):
candidates = set()
for itemset1 in freq_sets:
for itemset2 in freq_sets:
union = itemset1.union(itemset2)
if len(union) == k:
candidates.add(union)
return candidates
def prune_candidates(candidates, freq_sets, k):
pruned_candidates = set()
for candidate in candidates:
subsets = combinations(candidate, k)
if all(subset in freq_sets for subset in subsets):
pruned_candidates.add(candidate)
return pruned_candidates
def apriori(transactions, min_support=0.5, min_confidence=0.5):
item_counts = defaultdict(int)
freq_sets = set()
for transaction in transactions:
for item in transaction:
item_counts[item] += 1
num_transactions = len(transactions)
for item, count in item_counts.items():
support = count / num_transactions
if support >= min_support:
freq_sets.add(frozenset([item]))
k = 2
while freq_sets:
candidates = generate_candidates(freq_sets, k)
freq_counts = defaultdict(int)
for transaction in transactions:
for candidate in candidates:
if candidate.issubset(transaction):
freq_counts[candidate] += 1
freq_sets = set()
for candidate, count in freq_counts.items():
support = count / num_transactions
if support >= min_support:
freq_sets.add(candidate)
freq_sets = prune_candidates(freq_sets, freq_sets, k-1)
k += 1
rules = []
for freq_set in freq_sets:
for item in freq_set:
antecedent = frozenset([item])
consequent = freq_set.difference(antecedent)
if len(consequent) > 0:
confidence = item_counts[freq_set] / item_counts[antecedent]
if confidence >= min_confidence:
rules.append((antecedent, consequent, confidence))
return rules
# 示例数据
transactions = [
['A', 'B', 'C'],
['A', 'B'],
['B', 'C'],
['A', 'C'],
['B', 'D']
]
# 调用apriori函数
rules = apriori(transactions, min_support=0.5, min_confidence=0.5)
# 打印结果
for antecedent, consequent, confidence in rules:
print(f"{antecedent} => {consequent}: {confidence}")
```
这个示例代码演示了如何使用Apriori算法来查找频繁项集和关联规则。您可以根据自己的数据和要求进行调整和修改。希望对您有所帮助!
阅读全文