程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

【星海隨筆】FP樹的數據結構 python實現

編輯:Python
class treeNode:
def __init__(self, name_value, num_occur, parent_node):
self.name = name_value
self.count = num_occur
self.node_link = None
self.parent = parent_node
self.children = {
}
def inc(self, num_occur):
self.count += num_occur
def disp(self, ind=1):
print(' ' * ind, self.name, ' ', self.count)
for child in self.children.values():
child.disp(ind + 1)
def create_tree(dataset, min_sup=1):
header_table = {
}
for trans in dataset:
for item in trans:
header_table[item] = header_table.get(item, 0) + dataset[trans]
temp_table = {
}
# 移除不滿足最小支持度的項集
for k in header_table.keys():
if header_table[k] >= min_sup:
temp_table[k] = header_table[k]
del(header_table)
header_table = temp_table
freq_item_set = set(header_table.keys())
# 如果沒有項集滿足要求,則退出
if len(freq_item_set) == 0:
return None, None
for k in header_table:
header_table[k] = [header_table[k], None]
ret_tree = treeNode('Null Set', 1, None)
for tran_set, count in dataset.items():
# 根據全局頻率對每個事務中的元素進行排序
local_D = {
}
for item in tran_set:
if item in freq_item_set:
local_D[item] = header_table[item][0]
if len(local_D) > 0:
ordered_items = [v[0] for v in sorted(local_D.items(), key=lambda p:p[1], reverse=True)]
update_tree(ordered_items, ret_tree, header_table, count)
return ret_tree, header_table
def update_tree(items, in_tree, header_table, count):
if items[0] in in_tree.children:
in_tree.children[items[0]].inc(count)
else:
in_tree.children[items[0]] = treeNode(items[0], count, in_tree)
if header_table[items[0]][1] == None:
header_table[items[0]][1] = in_tree.children[items[0]]
else:
update_header(header_table[items[0]][1], in_tree.children[items[0]])
if len(items) > 1:
# 對剩下的項集迭代調用 update_tree 函數
update_tree(items[1::], in_tree.children[items[0]], header_table, count)
def update_header(node_to_test, target_node):
while node_to_test.node_link != None:
node_to_test = node_to_test.node_link
node_to_test.node_link = target_node
def ascend_tree(leaf_node, prefix_path):
if leaf_node.parent != None:
prefix_path.append(leaf_node.name)
ascend_tree(leaf_node.parent, prefix_path)
def find_prefix_path(base_pat, tree_node):
cond_pats = {
}
while tree_node != None:
prefix_path = []
ascend_tree(tree_node, prefix_path)
if len(prefix_path) > 1:
cond_pats[frozenset(prefix_path[1:])] = tree_node.count
tree_node = tree_node.node_link
return cond_pats
def mine_tree(in_tree, header_table, min_sup, prefix, freq_item_list):
#big_l = [v[0] for v in sorted(header_table.items(), key=lambda p:p[1])]
big_l=[v[0] for v in sorted(header_table.items(),key=lambda p:str(p[1]))]
for base_pat in big_l:
new_freq_set = prefix.copy()
new_freq_set.add(base_pat)
freq_item_list.append(new_freq_set)
cond_patt_bases = find_prefix_path(base_pat, header_table[base_pat][1])
my_cond_tree, my_head = create_tree(cond_patt_bases, min_sup)
if my_head != None:
mine_tree(my_cond_tree, my_head, min_sup, new_freq_set, freq_item_list)
def loadSimpDat():
simDat = [
['r', 'z', 'h', 'j', 'p'],
['z', 'y', 'x', 'w', 'v', 'u', 't', 's'],
['z'],
['r', 'x', 'n', 'o', 's'],
['y', 'r', 'x', 'z', 'q', 't', 'p'],
['y', 'z', 'x', 'e', 'q', 's', 't', 'm']
]
return simDat
def createInitSet(dataSet):
retDict={
}
for trans in dataSet:
retDict[frozenset(trans)]=1
return retDict
if __name__=='__main__':
minSup=3#最小支持度
simDat=loadSimpDat()
initSet=createInitSet(simDat)
myFPtree,myHeaderTab=create_tree(initSet,minSup)
myFPtree.disp()#文本形式展示FP樹
print('~~~~~~~~')
myFreqList=[]
mine_tree(myFPtree,myHeaderTab,minSup,set([]),myFreqList)
#mineTree(myFPtree,myHeaderTab,minSup,set([]),myFreqList)
print(myFreqList)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved