用python实现关联规则算法
时间: 2023-10-12 22:17:50 浏览: 112
关联规则算法是一种常用的数据挖掘算法,用于发现数据集中的频繁项集和关联规则。下面是用 Python 实现 Apriori 算法的代码:
```python
def load_data():
# 加载数据集
data = [['bread', 'milk', 'vegetable', 'fruit', 'eggs'],
['noodle', 'beef', 'vegetable', 'fruit', 'eggs'],
['bread', 'noodle', 'beef', 'vegetable'],
['bread', 'milk', 'beef', 'vegetable', 'eggs'],
['milk', 'fruit', 'eggs'],
['noodle', 'milk', 'vegetable', 'beef'],
['noodle', 'milk', 'vegetable', 'fruit'],
['bread', 'milk', 'vegetable', 'eggs'],
['noodle', 'bread', 'milk', 'vegetable', 'fruit', 'eggs'],
['bread', 'milk', 'fruit', 'eggs']]
return data
def create_C1(data):
# 创建初始候选项集 C1
C1 = []
for transaction in data:
for item in transaction:
if not [item] in C1:
C1.append([item])
C1.sort()
return list(map(frozenset, C1))
def scan_D(D, Ck, min_support):
# 计算候选项集 Ck 在数据集 D 中的支持度
ss_cnt = {}
for tid in D:
for can in Ck:
if can.issubset(tid):
if not can in ss_cnt:
ss_cnt[can] = 1
else:
ss_cnt[can] += 1
num_items = float(len(D))
ret_list = []
support_data = {}
for key in ss_cnt:
support = ss_cnt[key] / num_items
if support >= min_support:
ret_list.insert(0, key)
support_data[key] = support
return ret_list, support_data
def apriori_gen(Lk, k):
# 根据频繁项集 Lk 创建候选项集 Ck+1
ret_list = []
len_Lk = len(Lk)
for i in range(len_Lk):
for j in range(i+1, len_Lk):
L1 = list(Lk[i])[:k-2]
L2 = list(Lk[j])[:k-2]
L1.sort()
L2.sort()
if L1 == L2:
ret_list.append(Lk[i] | Lk[j])
return ret_list
def apriori(data, min_support=0.5):
# 执行 Apriori 算法
C1 = create_C1(data)
D = list(map(set, data))
L1, support_data = scan_D(D, C1, min_support)
L = [L1]
k = 2
while len(L[k-2]) > 0:
Ck = apriori_gen(L[k-2], k)
Lk, sup_k = scan_D(D, Ck, min_support)
support_data.update(sup_k)
L.append(Lk)
k += 1
return L, support_data
def generate_rules(L, support_data, min_confidence=0.7):
# 生成关联规则
big_rule_list = []
for i in range(1, len(L)):
for freq_set in L[i]:
H1 = [frozenset([item]) for item in freq_set]
if i > 1:
rules_from_conseq(freq_set, H1, support_data, big_rule_list, min_confidence)
else:
calc_confidence(freq_set, H1, support_data, big_rule_list, min_confidence)
return big_rule_list
def calc_confidence(freq_set, H, support_data, brl, min_confidence=0.7):
# 计算规则的置信度
pruned_H = []
for conseq in H:
conf = support_data[freq_set] / support_data[freq_set-conseq]
if conf >= min_confidence:
print(freq_set-conseq, '-->', conseq, 'conf:', conf)
brl.append((freq_set-conseq, conseq, conf))
pruned_H.append(conseq)
return pruned_H
def rules_from_conseq(freq_set, H, support_data, brl, min_confidence=0.7):
# 从 H 中生成更多的关联规则
m = len(H[0])
if len(freq_set) > (m + 1):
Hmp1 = apriori_gen(H, m+1)
Hmp1 = calc_confidence(freq_set, Hmp1, support_data, brl, min_confidence)
if len(Hmp1) > 1:
rules_from_conseq(freq_set, Hmp1, support_data, brl, min_confidence)
data = load_data()
L, support_data = apriori(data, min_support=0.2)
rules = generate_rules(L, support_data, min_confidence=0.7)
```
这段代码实现了 Apriori 算法的核心部分,其中 `load_data` 函数用于加载数据集,`create_C1` 函数用于创建初始候选项集,`scan_D` 函数用于计算候选项集在数据集中的支持度,`apriori_gen` 函数用于根据频繁项集创建候选项集,`apriori` 函数用于执行 Apriori 算法,`generate_rules` 函数用于生成关联规则,`calc_confidence` 函数用于计算规则的置信度,`rules_from_conseq` 函数用于从 H 中生成更多的关联规则。你可以根据自己的需要调整参数和数据集来运行这段代码。
阅读全文