FP tree算法详解附代码及挖掘案例

完整代码在文末


概述

Apriori算法需要多次扫描数据,庞大的I/O次数是很大的瓶颈,FP Tree算法(也称FP Growth算法)采用了一些技巧,无论多少数据,只需要扫描两次数据集,因此提高了算法运行的效率

我先概述一下这个算法的数据结构,你只需要知道是啥就行,如果你现在一眼能看懂来龙去脉,可以直接点个赞然后溜了;第一眼看不懂也正常,慢慢来往下看…

数据结构

为了减少I/O次数,FP Tree算法引入了一些数据结构来临时存储数据。这个数据结构包括三部分,如下图所示:

在这里插入图片描述

  1. 项头表。里面记录了所有的单项频繁集出现的次数,按照次数降序排列
  2. FP Tree,它将我们的原始数据集映射到了内存中的一颗FP树,这个FP树比较难理解,后面再讲
  3. 节点链表。所有项头表里的单项频繁集都是一个节点链表的头,它依次指向FP树中该单项频繁集出现的位置。这样做主要是方便项头表和FP Tree之间的联系查找和更新,也好理解

项头表的建立

FP树的建立需要首先依赖项头表的建立。那看看怎么建立项头表

官方描述:第一次扫描数据,得到所有频繁单项集的的计数。然后删除支持度低于阈值的项,将单项频繁集放入项头表,并按照支持度降序排列。接着第二次也是最后一次扫描数据,将读到的原始数据剔除非频繁单项集,并按照支持度降序排列

  • 这是我们的数据

在这里插入图片描述

有10条数据,首先第一次扫描数据并对单项集计数,我们发现O,I,L,J,P,M, N都只出现一次,支持度低于20%的阈值,因此他们不会出现在下面的项头表中。剩下的A,C,E,G,B,D,F按照支持度的大小降序排列,组成了我们的项头表

  • 项头表如下:
    在这里插入图片描述
    接着我们第二次扫描数据,对于每条数据剔除非频繁1项集,并按照支持度降序排列。比如数据项ABCEFO,里面O是非频繁1项集,因此被剔除,只剩下了ABCEF。按照支持度的顺序排序,它变成了ACEBF。其他的数据项以此类推

  • 删除过程如下:

在这里插入图片描述

  • 然后对删除后的每一项集进行排序(按单项集的频率进行排序,参照项头表)

请添加图片描述

OKay 长征第一步已经成功了!

FP Tree的建立

有了项头表和排序后的数据集,我们就可以开始FP树的建立了。开始时FP树没有数据,建立FP树时我们一条条的读入排序后的数据集,插入FP树

插入规则:

  1. 插入时按照排序后的顺序,插入FP树中
  2. 排序靠前的节点是祖先节点,而靠后的是子孙节点
  3. 如果有共用的祖先,则对应的公用祖先节点计数加1
  4. 插入后,如果有新节点出现,则项头表对应的节点会通过节点链表链接上新节点
  5. 直到所有的数据都插入到FP树后,FP树的建立完成

妈耶!敢不敢再抽象一点!!!

唉,什么疾苦,那就一条数据一条数据的来吧

  1. 首先,我们插入第一条数据ACEBF,如下图所示。此时FP树没有节点,因此ACEBF是一个独立的路径,所有节点计数为1, 项头表通过节点链表链接上对应的新增节点。
    在这里插入图片描述
  2. 接着我们插入数据ACG,如下图所示。由于ACG和现有的FP树可以有共有的祖先节点序列AC,因此只需要增加一个新节点G,将新节点G的计数记为1。同时A和C的计数加1成为2。
    在这里插入图片描述

后面的8条数据都这样插入,注意有公共的祖先节点一定要走那条路,然后祖先节点次数+1

最后的FP tree结构:

在这里插入图片描述
ojbk,长征第二步差不多了


挖掘FP tree

挖掘规则

  • 得到了FP树和项头表以及节点链表,我们首先要从项头表的底部项依次向上挖掘

对于项头表对应于FP树的每一项,我们要找到它的条件模式基。

条件模式基是以我们要挖掘的节点作为叶子节点所对应的FP子树。

得到这个FP子树,我们将子树中每个节点的的计数设置为叶子节点的计数,并删除计数低于支持度的节点。从这个条件模式基,我们就可以递归挖掘得到频繁项集了。

码字说了也是白说,那就纸上谈一下兵吧

  • 挖掘F节点
  1. 从最底下的F节点开始,我们先来寻找F节点的条件模式基,由于F在FP树中只有一个节点,因此候选就只有下图左所示的一条路径,对应{A:8,C:8,E:6,B:2, F:2}
  2. 叶子节点是F,F的计数是2,那么这一整条路径计数都是2,即{A:2,C:2,E:2,B:2, F:2}
  3. F的频繁2项集为{A:2,F:2}, {C:2,F:2}, {E:2,F:2}, {B:2,F:2};频繁3项集为{A:2,C:2,F:2},{A:2,E:2,F:2},…就不写了。最大的频繁项集为频繁5项集,为{A:2,C:2,E:2,B:2,F:2}
  • 挖掘D节点
    D节点比F节点复杂一些,因为它有两个叶子节点,因此首先得到的FP子树如下图
    在这里插入图片描述
  1. 叶子节点是D,计数为1,变成{A:2, C:2,E:1 G:1,D:1, D:1} ; 解释一下,有两条路ACEGD和ACD ,所以AC同时在两条路上,所以是2
  2. E节点和G节点由于在条件模式基里面的支持度低于阈值,被我们删除
  3. 最终在去除低支持度节点并不包括叶子节点后D的条件模式基为{A:2, C:2}

注意特例:至于A,由于它的条件模式基为空,因此可以不用去挖掘了

  • 其他的都依次类推…

得到的条件模式基就是频繁项集

算法归纳

  1. 扫描数据,得到所有频繁一项集的的计数。然后删除支持度低于阈值的项,将1项频繁集放入项头表,并按照支持度降序排列。

  2. 扫描数据,将读到的原始数据剔除非频繁1项集,并按照支持度降序排列。

  3. 读入排序后的数据集,插入FP树,插入时按照排序后的顺序,插入FP树中,排序靠前的节点是祖先节点,而靠后的是子孙节点。如果有共用的祖先,则对应的公用祖先节点计数加1。插入后,如果有新节点出现,则项头表对应的节点会通过节点链表链接上新节点。直到所有的数据都插入到FP树后,FP树的建立完成。

  4. 从项头表的底部项依次向上找到项头表项对应的条件模式基。从条件模式基递归挖掘得到项头表项项的频繁项集(可以参见第4节对F的条件模式基的频繁二项集到频繁5五项集的挖掘)。

  5. 如果不限制频繁项集的项数,则返回步骤4所有的频繁项集,否则只返回满足项数要求的频繁项集。

超市数据集挖掘

在这里插入图片描述
在这里插入图片描述

  • Fp-growth算法返回的频繁项集,即条件模式基已经可以发现,Lassi不论在单频繁项集、二频繁项集、三频繁项集中,都占有很高的支持度

可以对比参考另一篇文章:Apriori算法挖掘

数据挖掘的学习笔记:数据挖掘学习

代码

python版本为Python2

  • 使用命令行启动 python fp_growth.py 数据集 -s 最小支持度


from collections import defaultdict, namedtuple
from itertools import imap


def find_frequent_itemsets(transactions, minimum_support, include_support=False):

    items = defaultdict(lambda: 0) # mapping from items to their supports


    for transaction in transactions:
        for item in transaction:
            items[item] += 1

    # Remove infrequent items from the item support dictionary.
    items = dict((item, support) for item, support in items.iteritems()
        if support >= minimum_support)


    def clean_transaction(transaction):
        transaction = filter(lambda v: v in items, transaction)
        transaction.sort(key=lambda v: items[v], reverse=True)
        return transaction

    master = FPTree()
    for transaction in imap(clean_transaction, transactions):
        master.add(transaction)

    def find_with_suffix(tree, suffix):
        for item, nodes in tree.items():
            support = sum(n.count for n in nodes)
            if support >= minimum_support and item not in suffix:
                # New winner!
                found_set = [item] + suffix
                yield (found_set, support) if include_support else found_set

                # Build a conditional tree and recursively search for frequent
                # itemsets within it.
                cond_tree = conditional_tree_from_paths(tree.prefix_paths(item))
                for s in find_with_suffix(cond_tree, found_set):
                    yield s # pass along the good news to our caller

    # Search for frequent itemsets, and yield the results we find.
    for itemset in find_with_suffix(master, []):
        yield itemset

class FPTree(object):


    Route = namedtuple('Route', 'head tail')

    def __init__(self):
        # The root node of the tree.
        self._root = FPNode(self, None, None)

        # A dictionary mapping items to the head and tail of a path of
        # "neighbors" that will hit every node containing that item.
        self._routes = {}

    @property
    def root(self):
        """The root node of the tree."""
        return self._root

    def add(self, transaction):
        """Add a transaction to the tree."""
        point = self._root

        for item in transaction:
            next_point = point.search(item)
            if next_point:
                # There is already a node in this tree for the current
                # transaction item; reuse it.
                next_point.increment()
            else:
                # Create a new point and add it as a child of the point we're
                # currently looking at.
                next_point = FPNode(self, item)
                point.add(next_point)

                # Update the route of nodes that contain this item to include
                # our new node.
                self._update_route(next_point)

            point = next_point

    def _update_route(self, point):
        """Add the given node to the route through all nodes for its item."""
        assert self is point.tree

        try:
            route = self._routes[point.item]
            route[1].neighbor = point # route[1] is the tail
            self._routes[point.item] = self.Route(route[0], point)
        except KeyError:
            # First node for this item; start a new route.
            self._routes[point.item] = self.Route(point, point)

    def items(self):

        for item in self._routes.iterkeys():
            yield (item, self.nodes(item))

    def nodes(self, item):
        """
        Generate the sequence of nodes that contain the given item.
        """

        try:
            node = self._routes[item][0]
        except KeyError:
            return

        while node:
            yield node
            node = node.neighbor

    def prefix_paths(self, item):
        """Generate the prefix paths that end with the given item."""

        def collect_path(node):
            path = []
            while node and not node.root:
                path.append(node)
                node = node.parent
            path.reverse()
            return path

        return (collect_path(node) for node in self.nodes(item))

    def inspect(self):
        print 'Tree:'
        self.root.inspect(1)

        print
        print 'Routes:'
        for item, nodes in self.items():
            print '  %r' % item
            for node in nodes:
                print '    %r' % node

def conditional_tree_from_paths(paths):
    """Build a conditional FP-tree from the given prefix paths."""
    tree = FPTree()
    condition_item = None
    items = set()


    for path in paths:
        if condition_item is None:
            condition_item = path[-1].item

        point = tree.root
        for node in path:
            next_point = point.search(node.item)
            if not next_point:
                # Add a new node to the tree.
                items.add(node.item)
                count = node.count if node.item == condition_item else 0
                next_point = FPNode(tree, node.item, count)
                point.add(next_point)
                tree._update_route(next_point)
            point = next_point

    assert condition_item is not None

    # Calculate the counts of the non-leaf nodes.
    for path in tree.prefix_paths(condition_item):
        count = path[-1].count
        for node in reversed(path[:-1]):
            node._count += count

    return tree

class FPNode(object):
    """A node in an FP tree."""

    def __init__(self, tree, item, count=1):
        self._tree = tree
        self._item = item
        self._count = count
        self._parent = None
        self._children = {}
        self._neighbor = None

    def add(self, child):
        """Add the given FPNode `child` as a child of this node."""

        if not isinstance(child, FPNode):
            raise TypeError("Can only add other FPNodes as children")

        if not child.item in self._children:
            self._children[child.item] = child
            child.parent = self

    def search(self, item):

        try:
            return self._children[item]
        except KeyError:
            return None

    def __contains__(self, item):
        return item in self._children

    @property
    def tree(self):
        """The tree in which this node appears."""
        return self._tree

    @property
    def item(self):
        """The item contained in this node."""
        return self._item

    @property
    def count(self):
        """The count associated with this node's item."""
        return self._count

    def increment(self):
        """Increment the count associated with this node's item."""
        if self._count is None:
            raise ValueError("Root nodes have no associated count.")
        self._count += 1

    @property
    def root(self):
        """True if this node is the root of a tree; false if otherwise."""
        return self._item is None and self._count is None

    @property
    def leaf(self):
        """True if this node is a leaf in the tree; false if otherwise."""
        return len(self._children) == 0

    @property
    def parent(self):
        """The node's parent"""
        return self._parent

    @parent.setter
    def parent(self, value):
        if value is not None and not isinstance(value, FPNode):
            raise TypeError("A node must have an FPNode as a parent.")
        if value and value.tree is not self.tree:
            raise ValueError("Cannot have a parent from another tree.")
        self._parent = value

    @property
    def neighbor(self):
        """
        The node's neighbor; the one with the same value that is "to the right"
        of it in the tree.
        """
        return self._neighbor

    @neighbor.setter
    def neighbor(self, value):
        if value is not None and not isinstance(value, FPNode):
            raise TypeError("A node must have an FPNode as a neighbor.")
        if value and value.tree is not self.tree:
            raise ValueError("Cannot have a neighbor from another tree.")
        self._neighbor = value

    @property
    def children(self):
        """The nodes that are children of this node."""
        return tuple(self._children.itervalues())

    def inspect(self, depth=0):
        print ('  ' * depth) + repr(self)
        for child in self.children:
            child.inspect(depth + 1)

    def __repr__(self):
        if self.root:
            return "<%s (root)>" % type(self).__name__
        return "<%s %r (%r)>" % (type(self).__name__, self.item, self.count)


if __name__ == '__main__':
    from optparse import OptionParser
    import csv

    p = OptionParser(usage='%prog data_file')
    p.add_option('-s', '--minimum-support', dest='minsup', type='int',
        help='Minimum itemset support (default: 2)')
    p.add_option('-n', '--numeric', dest='numeric', action='store_true',
        help='Convert the values in datasets to numerals (default: false)')
    p.set_defaults(minsup=2)
    p.set_defaults(numeric=False)

    options, args = p.parse_args()
    if len(args) < 1:
        p.error('must provide the path to a CSV file to read')

    transactions = []
    with open(args[0]) as database:
        for row in csv.reader(database):
            if options.numeric:
                transaction = []
                for item in row:
                    transaction.append(long(item))
                transactions.append(transaction)
            else:
                transactions.append(row)

    result = []
    for itemset, support in find_frequent_itemsets(transactions, options.minsup, True):
        result.append((itemset,support))

    result = sorted(result, key=lambda i: i[0])
    for itemset, support in result:
        print str(itemset) + ' ' + str(support)

  • 16
    点赞
  • 84
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
### 回答1: FP-Tree算法是一种高效的频繁项集挖掘算法,适用于大数据集。它通过构建FP-Tree来进行频繁项集挖掘FP-Tree是一种特殊的树结构,由项和链接组成。 在Python中,可以使用第三方库pyfpgrowth来实现FP-Tree算法。使用方法如下: 1. 安装pyfpgrowth库:pip install pyfpgrowth 2. 导入库:from pyfpgrowth import find_frequent_patterns 3. 使用find_frequent_patterns()函数进行频繁项集挖掘,如:frequent_itemsets = find_frequent_patterns(transactions, min_support) 其中transactions是事务数据集,min_support是最小支持度。 ### 回答2: FP-tree算法是一种用于频繁模式挖掘算法,它是一种新型的挖掘频繁项集的算法,它挖掘频繁项集时利用了一种基于树结构的压缩存储的方式,能够大大减少挖掘频繁项集的时间和空间开销。它的主要思路是在构建树形结构的同时,对于每个频繁项都记录其支持度计数,这样就可以通过低空间高效的方式挖掘到频繁项集。 在FP-tree算法中,首先需要进行数据预处理,将原始数据转换为FP-tree,在转换过程中需要统计每个元素的支持度计数和元素之间的关联关系,并按照支持度计数从大到小排序,然后剪枝去除支持度计数小于最小支持度的元素。接着,使用FP-tree进行频繁项集挖掘,利用FP-tree的树形结构和每个元素的支持度计数,可以使用分治法来递归地挖掘频繁项集。具体地,FP-tree算法使用递归方式遍历FP-tree,并迭代地将条件模式基转换为新的子问题,在这个过程中可以利用条件的模式基来更新FP-tree,最后根据这些更新后的频繁项集可以得出所有的频繁项集。 在Python中,FP-tree可以通过Python的库plyvel来进行实现,通过代码实现节点的添加,查询,删除,更新等操作,从而实现FP-tree的构建和使用。具体地,对于每个节点,可以用Python中的数据结构字典来进行表示,字典中包括节点的名称,支持度计数,以及指向父节点和子节点的指针。在建树的过程中,可以使用递归方式遍历每个元素,并根据元素的支持度计数来进行排序和剪枝。在挖掘频繁项集的过程中,通过对树进行递归遍历,可以找到每个元素的条件模式基,并使用条件模式基来更新FP-tree,最终可以得到所有的频繁项集。 总而言之,FP-tree算法是一种基于树形结构和压缩存储的频繁项集挖掘算法,在实现上可以通过Python中的字典来进行节点的表示和操作,在具体实现中需要注意剪枝和更新的操作,以及频繁项集的递归遍历。 ### 回答3: FP算法是一种非常常见的频繁项集挖掘算法,它是由Han等人在2000年提出的。相比传统的Apriori算法FP算法的优点在于它可以高效地挖掘频繁项集,大幅提升了算法的效率和准确性。 FP算法的核心是FP数据结构,它是一种压缩存储的树形结构,能够高效地存储频繁项集信息。FP算法的具体步骤如下: 1. 构建项头表:遍历所有的事务,统计每个项出现的频次,并按照频次从大到小排序,构建项头表。 2. 构建FP树:遍历所有的事务,按照事务中项的顺序构建FP树。每个事务中的项要按照项头表中的顺序排序。如果树中已存在该项的节点,则将该节点的计数加1;否则,添加一个新的节点。 3. 抽取频繁项集:根据构建好的FP树和项头表,采用递归的方式,从FP树底部往上挖掘频繁项集。 FP算法的实现可以使用Python语言,需要用到的库包括numpy和pandas。具体实现步骤如下: 1. 构建项头表:遍历数据集,统计每个项出现的频次,将频繁项保存到一个字典中。 ```python def create_item_dict(data_set): item_dict = {} for trans in data_set: for item in trans: if item in item_dict: item_dict[item] += 1 else: item_dict[item] = 1 return item_dict ``` 2. 构建FP树:遍历数据集,按照项头表中的顺序构建FP树。 ```python class TreeNode: def __init__(self, name_value, num_occurrences, parent_node): self.name = name_value self.count = num_occurrences self.parent = parent_node self.children = {} self.next = None def inc(self, num_occurrences): self.count += num_occurrences def create_tree(data_set, min_sup=1): item_dict = create_item_dict(data_set) freq_items = set([item for item in item_dict.keys() if item_dict[item] >= min_sup]) if len(freq_items) == 0: return None, None for item in item_dict.keys(): if item not in freq_items: del(item_dict[item]) header_table = {item: [item_dict[item], None] for item in freq_items} root_node = TreeNode('Null Set', 1, None) for trans in data_set: local_dict = {} for item in trans: if item in freq_items: local_dict[item] = header_table[item][0] if len(local_dict) > 0: ordered_items = [item[0] for item in sorted(local_dict.items(), key=lambda x: (-x[1], x[0]))] update_tree(ordered_items, root_node, header_table) return root_node, header_table def update_tree(items, cur_node, header_table): if items[0] in cur_node.children: cur_node.children[items[0]].inc(1) else: cur_node.children[items[0]] = TreeNode(items[0], 1, cur_node) if header_table[items[0]][1] is None: header_table[items[0]][1] = cur_node.children[items[0]] else: update_header(header_table[items[0]][1], cur_node.children[items[0]]) if len(items) > 1: update_tree(items[1:], cur_node.children[items[0]], header_table) def update_header(node_to_test, target_node): while node_to_test.next is not None: node_to_test = node_to_test.next node_to_test.next = target_node ``` 3. 抽取频繁项集:采用递归的方式,从FP树底部往上挖掘频繁项集。 ```python def ascend_tree(leaf_node, prefix_path): if leaf_node.parent is not None: prefix_path.append(leaf_node.name) ascend_tree(leaf_node.parent, prefix_path) def find_prefix_path(base_pat, header_table): node = header_table[base_pat][1] pats = [] while node is not None: prefix_path = [] ascend_tree(node, prefix_path) if len(prefix_path) > 1: pats.append(prefix_path[1:]) node = node.next return pats def mine_tree(in_tree, header_table, min_sup, pre_fix, freq_item_list): bigL = [v[0] for v in sorted(header_table.items(), key=lambda x: (-x[1][0], x[0]))] for base_pat in bigL: new_freq_set = pre_fix.copy() new_freq_set.add(base_pat) freq_item_list.append(new_freq_set) cond_patt_bases = find_prefix_path(base_pat, header_table) my_cond_tree, my_head = create_tree(cond_patt_bases, min_sup) if my_head is not None: mine_tree(my_cond_tree, my_head, min_sup, new_freq_set, freq_item_list) ``` 通过以上实现,我们就可以使用FP算法高效地挖掘频繁项集了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值