Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 742adf1

Browse files
committed
Add an option to return itemsets' support
1 parent 710608f commit 742adf1

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

fp_growth.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
>>> find_frequent_itemsets(transactions, minimum_support)
1010
"""
1111

12-
from collections import defaultdict
12+
from collections import defaultdict, namedtuple
1313
from itertools import imap
1414

1515
__author__ = 'Eric Naeseth <[email protected]>'
1616
__copyright__ = 'Copyright © 2009 Eric Naeseth'
1717
__license__ = 'MIT License'
1818

19-
def find_frequent_itemsets(transactions, minimum_support):
19+
def find_frequent_itemsets(transactions, minimum_support, include_support=False):
2020
"""
21-
Finds frequent itemsets in the given transactions using FP-growth. This
21+
Find frequent itemsets in the given transactions using FP-growth. This
2222
function returns a generator instead of an eagerly-populated list of items.
2323
2424
The `transactions` parameter can be any iterable of iterables of items.
@@ -27,6 +27,9 @@ def find_frequent_itemsets(transactions, minimum_support):
2727
2828
Each item must be hashable (i.e., it must be valid as a member of a
2929
dictionary or a set).
30+
31+
If `include_support` is true, yield (itemset, support) pairs instead of
32+
just the itemsets.
3033
"""
3134
items = defaultdict(lambda: 0) # mapping from items to their supports
3235
processed_transactions = []
@@ -41,10 +44,8 @@ def find_frequent_itemsets(transactions, minimum_support):
4144
processed_transactions.append(processed)
4245

4346
# Remove infrequent items from the item support dictionary.
44-
items = dict(items)
45-
for item, support in items.items():
46-
if support < minimum_support:
47-
del items[item]
47+
items = dict((item, support) for item, support in items.iteritems()
48+
if support >= minimum_support)
4849

4950
# Build our FP-tree. Before any transactions can be added to the tree, they
5051
# must be stripped of infrequent items and their surviving items must be
@@ -64,7 +65,7 @@ def find_with_suffix(tree, suffix):
6465
if support >= minimum_support and item not in suffix:
6566
# New winner!
6667
found_set = [item] + suffix
67-
yield found_set
68+
yield (found_set, support) if include_support else found_set
6869

6970
# Build a conditional tree and recursively search for frequent
7071
# itemsets within it.
@@ -74,8 +75,8 @@ def find_with_suffix(tree, suffix):
7475
yield s # pass along the good news to our caller
7576

7677
# Search for frequent itemsets, and yield the results we find.
77-
for s in find_with_suffix(master, []):
78-
yield s
78+
for itemset in find_with_suffix(master, []):
79+
yield itemset
7980

8081
class FPTree(object):
8182
"""

0 commit comments

Comments
 (0)