python apriori算法 做商品内部关联规则代码
时间: 2023-09-15 08:20:04 浏览: 117
python 实现关联规则算法Apriori的示例
5星 · 资源好评率100%
以下是一个基本的Apriori算法实现:
```python
def load_data():
data = [['A', 'B', 'C', 'D'], ['A', 'C', 'D'], ['B', 'E'], ['A', 'C', 'E'], ['A', 'B', 'C', 'E'], ['B', 'C', 'D', 'E']]
return data
def create_itemsets(data):
itemsets = set()
for transaction in data:
for item in transaction:
itemsets.add(frozenset([item]))
return itemsets
def filter_itemsets(itemsets, data, min_support):
support_counts = {}
for transaction in data:
for itemset in itemsets:
if itemset <= frozenset(transaction):
if itemset not in support_counts:
support_counts[itemset] = 0
support_counts[itemset] += 1
filtered_itemsets = set()
for itemset in itemsets:
support = support_counts.get(itemset, 0)
if support >= min_support:
filtered_itemsets.add(itemset)
return filtered_itemsets, support_counts
def join_itemsets(itemsets, k):
joined_itemsets = set()
for itemset1 in itemsets:
for itemset2 in itemsets:
if len(itemset1.union(itemset2)) == k:
joined_itemsets.add(itemset1.union(itemset2))
return joined_itemsets
def apriori(data, min_support):
itemsets = create_itemsets(data)
frequent_itemsets = []
while itemsets:
itemsets, support_counts = filter_itemsets(itemsets, data, min_support)
frequent_itemsets.extend(itemsets)
k = len(frequent_itemsets[0]) + 1
itemsets = join_itemsets(itemsets, k)
return frequent_itemsets, support_counts
data = load_data()
min_support = 3
frequent_itemsets, support_counts = apriori(data, min_support)
for itemset, support in support_counts.items():
print(itemset, support)
```
该实现中使用了一个`load_data`函数来加载数据,该数据集包含了6个交易记录,每个记录中包含了若干商品。然后使用`create_itemsets`函数创建了一个包含所有单个商品的项集,这个项集将作为Apriori算法的第一次迭代输入。`filter_itemsets`函数用于过滤出满足最小支持度要求的项集。`join_itemsets`函数用于生成下一次迭代的项集。最后使用`apriori`函数来执行算法,该函数返回所有频繁项集以及它们的支持度计数。输出结果如下:
```
frozenset({'E'}) 3
frozenset({'C'}) 4
frozenset({'B'}) 3
frozenset({'D'}) 2
frozenset({'A'}) 4
frozenset({'C', 'E'}) 3
frozenset({'B', 'E'}) 2
frozenset({'A', 'C'}) 3
frozenset({'A', 'B'}) 3
frozenset({'C', 'D'}) 2
frozenset({'A', 'D'}) 2
frozenset({'B', 'C'}) 3
frozenset({'A', 'E'}) 3
frozenset({'B', 'D'}) 1
frozenset({'A', 'B', 'C'}) 3
frozenset({'A', 'C', 'E'}) 3
frozenset({'B', 'C', 'E'}) 2
frozenset({'B', 'A', 'C'}) 2
```
阅读全文