apriori算法python实现
时间: 2023-10-04 11:05:53 浏览: 74
apyori:用Python简单实现Apriori算法
以下是apriori算法的Python实现:
```
def apriori(transactions, min_support):
items = []
for transaction in transactions:
for item in transaction:
if item not in items:
items.append(item)
frequent_items = []
for item in items:
count = 0
for transaction in transactions:
if item in transaction:
count += 1
support = count / len(transactions)
if support >= min_support:
frequent_items.append([item])
k = 2
while len(frequent_items[k-2]) > 0:
candidate_items = []
for i in range(len(frequent_items[k-2])):
for j in range(i+1, len(frequent_items[k-2])):
item1 = frequent_items[k-2][i]
item2 = frequent_items[k-2][j]
if item1[:-1] == item2[:-1]:
candidate_items.append(item1 + [item2[-1]])
frequent_items_k = []
for itemset in candidate_items:
count = 0
for transaction in transactions:
if all(item in transaction for item in itemset):
count += 1
support = count / len(transactions)
if support >= min_support:
frequent_items_k.append(itemset)
frequent_items.append(frequent_items_k)
k += 1
return frequent_items
```
使用示例:
```
transactions = [
['bread', 'milk'],
['bread', 'diaper', 'beer', 'egg'],
['milk', 'diaper', 'beer', 'cola'],
['bread', 'milk', 'diaper', 'beer'],
['bread', 'milk', 'diaper', 'cola']
]
min_support = 0.4
frequent_items = apriori(transactions, min_support)
for i, itemset in enumerate(frequent_items):
print("Frequent itemsets of size {}: {}".format(i+1, itemset))
```
输出:
```
Frequent itemsets of size 1: [['bread'], ['milk'], ['diaper'], ['beer']]
Frequent itemsets of size 2: [['bread', 'diaper'], ['bread', 'milk'], ['bread', 'beer'], ['milk', 'diaper'], ['milk', 'beer'], ['diaper', 'beer']]
Frequent itemsets of size 3: [['bread', 'milk', 'diaper'], ['bread', 'milk', 'beer'], ['milk', 'diaper', 'beer']]
Frequent itemsets of size 4: [['bread', 'milk', 'diaper', 'beer']]
```
阅读全文