python实现Apriori算法和fp树算法挖掘频繁集的实验结果截图
时间: 2023-07-26 15:45:26 浏览: 103
使用Apriori算法进行频繁项集的挖掘以及关联规则的挖掘
很抱歉,我无法提供截图。但是我可以提供一些Python实现Apriori算法和FP树算法的代码示例,您可以根据自己的数据进行实验,并生成相应的结果截图。
Apriori算法代码示例:
```python
# Apriori算法实现
def apriori(data, min_support=0.5):
"""生成频繁项集"""
n_trans = len(data)
itemsets = {}
# 阶段1:生成所有1项集
for trans in data:
for item in trans:
if item not in itemsets:
itemsets[frozenset([item])] = 0
itemsets[frozenset([item])] += 1
# 阶段2:生成所有k项集(k>=2)
k = 2
while True:
freq_itemsets = {}
for itemset in itemsets:
for item in itemset:
subset = frozenset([x for x in itemset if x != item])
if subset not in freq_itemsets:
freq_itemsets[subset] = 0
freq_itemsets[subset] += 1
itemsets = {itemset: freq_itemsets[itemset] / n_trans for itemset in freq_itemsets if freq_itemsets[itemset] / n_trans >= min_support}
if not itemsets:
break
k += 1
return itemsets
```
FP树算法代码示例:
```python
# FP树算法实现
class Node:
"""FP树结点"""
def __init__(self, item, freq, parent=None):
self.item = item
self.freq = freq
self.parent = parent
self.children = {}
self.next = None
def add_child(self, child):
self.children[child.item] = child
def get_child(self, item):
return self.children.get(item)
def insert_tree(root, trans, freq_table):
"""将一条事务插入FP树"""
node = root
for item in trans:
child = node.get_child(item)
if not child:
child_freq = freq_table.get(item, 0)
child = Node(item, child_freq, node)
freq_table[item] = child_freq + 1
node.add_child(child)
else:
child.freq += 1
node = child
def build_tree(data, min_support=0.5):
"""构建FP树"""
freq_table = {}
root = Node(None, 0)
for trans in data:
insert_tree(root, trans, freq_table)
# 去掉不满足最小支持度的结点
for item in list(freq_table.keys()):
if freq_table[item] / len(data) < min_support:
freq_table.pop(item)
# 排序,获得频繁项集的顺序
freq_items = sorted(freq_table, key=lambda x: freq_table[x], reverse=True)
# 构建条件FP树
cond_trees = {}
for item in freq_items:
cond_data = []
node = root.get_child(item)
while node:
trans = []
while node.parent:
trans.append(node.item)
node = node.parent
cond_data.append(trans)
node = node.next
cond_trees[item] = build_tree(cond_data, min_support)
return (root, freq_table, cond_trees)
def find_freq_sets(cond_trees, prefix):
"""寻找以prefix为前缀的所有频繁项集"""
freq_sets = {}
for item in cond_trees:
new_prefix = prefix.copy()
new_prefix.add(item)
freq_sets[frozenset(new_prefix)] = cond_trees[item][1][item]
freq_sets.update(find_freq_sets(cond_trees[item][2], new_prefix))
return freq_sets
```
这些代码示例可以帮助您开始实验。祝您好运!
阅读全文