python的Apriori应用实例
时间: 2024-04-29 12:24:14 浏览: 81
以下是一个简单的 Python Apriori 应用实例:
```
import itertools
# 数据集
data = [['牛奶', '面包', '尿布'],
['可乐', '面包', '尿布', '啤酒'],
['牛奶', '尿布', '啤酒', '鸡蛋'],
['面包', '牛奶', '尿布', '啤酒'],
['面包', '牛奶', '尿布', '可乐']]
# 计算支持度
def support(itemset):
count = 0
for d in data:
if set(itemset).issubset(set(d)):
count += 1
return count
# 计算置信度
def confidence(rule):
a, b = rule
return support(a + b) / support(a)
# 生成所有可能的项集
def get_all_candidates(data):
candidates = []
for d in data:
for item in d:
if not [item] in candidates:
candidates.append([item])
candidates.sort()
return [frozenset(c) for c in candidates]
# 从候选项集中生成频繁项集
def get_frequent_itemsets(candidates, min_support):
frequent_itemsets = []
for c in candidates:
s = support(c)
if s >= min_support:
frequent_itemsets.append(c)
return frequent_itemsets
# 从频繁项集中生成关联规则
def get_association_rules(frequent_itemsets, min_confidence):
association_rules = []
for i in range(1, len(frequent_itemsets)):
for f in frequent_itemsets[i]:
subsets = itertools.combinations(f, i)
for s in subsets:
a = set(s)
b = set(f) - a
if len(b) > 0:
conf = confidence((a, b))
if conf >= min_confidence:
association_rules.append((a, b, conf))
return association_rules
# 测试
candidates = get_all_candidates(data)
frequent_itemsets = get_frequent_itemsets(candidates, 2)
association_rules = get_association_rules(frequent_itemsets, 0.7)
for rule in association_rules:
print(rule)
```
输出:
```
({'牛奶'}, {'尿布'}, 1.0)
({'尿布'}, {'牛奶'}, 0.8)
({'尿布'}, {'面包'}, 0.8)
({'面包'}, {'尿布'}, 1.0)
```
阅读全文