def fpgrowth(fptree, min_support=2, pruning=False): '''Finds frequent item sets of items appearing in a list of transactions based on FP-Growth by Han et al. :param fptree: The input of the algorithm. Must come from `get_fptree`. :param min_support: The minimal support of a set. :param pruning: Perform a pruning operation. Default to False. :rtype: A set containing the frequent item sets and their support. ''' fis = set() report = {} _fpgrowth(fptree, fis, report, min_support, pruning) return report
def test_fpgrowth(should_print=False, ts=None, support=2, pruning=False): if ts is None: ts = get_default_transactions() fptree = get_fptree(ts, lambda e: e, support) fis = set() report = {} n = _fpgrowth(fptree, fis, report, support, pruning) if should_print: print(n) print(report) return (n, report)
def _fpgrowth(fptree, fis, report, min_support=2, pruning=True): (_, heads) = fptree n = 0 for (head_node, head_support) in heads.values(): if head_support < min_support: continue fis.add(head_node.key) #print('Report {0} with support {1}'.format(fis, head_support)) report[frozenset(fis)] = head_support new_heads = _init_heads(heads) _create_cond_tree(head_node, new_heads, pruning) if pruning: _prune_cond_tree(new_heads, min_support) n = n + 1 + _fpgrowth((None, new_heads), fis, report, min_support, pruning) fis.remove(head_node.key) return n
def _fpgrowth(fptree, fis, report, min_support=2, pruning=True): (_, heads) = fptree n = 0 for (head_node, head_support) in heads.values(): if head_support < min_support: continue fis.add(head_node.key) #print('Report {0} with support {1}'.format(fis, head_support)) report[frozenset(fis)] = head_support new_heads = _init_heads(heads) _create_cond_tree(head_node, new_heads, pruning) if pruning: _prune_cond_tree(new_heads, min_support) n = n + 1 + _fpgrowth( (None, new_heads), fis, report, min_support, pruning) fis.remove(head_node.key) return n