编写程序完成下列算法: 1、apriori算法 输入: 数据集d;最小支持数minsup_count;
时间: 2024-03-23 13:36:20 浏览: 50
实现apriori算法
5星 · 资源好评率100%
以下是 Python 代码实现 Apriori 算法:
```python
from collections import defaultdict
def load_data():
data = [['A', 'B', 'C', 'E'], ['A', 'B', 'C', 'D'], ['A', 'B', 'C'], ['A', 'B'], ['B', 'C', 'E']]
return data
def create_C1(data):
C1 = set()
for transaction in data:
for item in transaction:
itemset = frozenset([item])
C1.add(itemset)
return C1
def support_count(data, candidate, minsup_count):
count = 0
for transaction in data:
if candidate.issubset(transaction):
count += 1
return count >= minsup_count
def generate_candidates(Lk, k):
candidates = set()
for i in Lk:
for j in Lk:
union = i.union(j)
if len(union) == k:
candidates.add(union)
return candidates
def apriori(data, minsup_count):
C1 = create_C1(data)
L1 = {}
for candidate in C1:
if support_count(data, candidate, minsup_count):
L1[candidate] = support_count(data, candidate, minsup_count)
L = [L1]
k = 2
while len(L[k-2]) > 0:
Ck = generate_candidates(L[k-2].keys(), k)
Lk = defaultdict(int)
for candidate in Ck:
if support_count(data, candidate, minsup_count):
Lk[candidate] = support_count(data, candidate, minsup_count)
L.append(Lk)
k += 1
return L
if __name__ == '__main__':
data = load_data()
minsup_count = 3
L = apriori(data, minsup_count)
print(L)
```
在这个例子中,数据集为一个列表,每个元素表示一个事务,每个事务由若干个项组成。`create_C1()` 函数用于生成初始候选项集,`support_count()` 函数用于计算项集的支持度,`generate_candidates()` 函数用于生成候选项集,`apriori()` 函数实现了 Apriori 算法,返回项集的列表。在这个例子中,最小支持数为3,即项集出现的次数至少为3次才会被保留。
阅读全文