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

Skip to content

Commit 9ba1f2d

Browse files
dzzhenaeseth
authored andcommitted
Fix a bug in support calculation
Fixes enaeseth#2.
1 parent 5803128 commit 9ba1f2d

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

fp_growth.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,9 @@ def conditional_tree_from_paths(paths, minimum_support):
232232

233233
# Calculate the counts of the non-leaf nodes.
234234
for path in tree.prefix_paths(condition_item):
235-
count = None
236-
for node in reversed(path):
237-
if count is not None:
238-
node._count += count
239-
count = node.count
235+
count = path[-1].count
236+
for node in reversed(path[:-1]):
237+
node._count += count
240238

241239
# Eliminate the nodes for any items that are no longer frequent.
242240
for item in items:
@@ -403,7 +401,7 @@ def __repr__(self):
403401

404402
f = open(args[0])
405403
try:
406-
for itemset in find_frequent_itemsets(csv.reader(f), options.minsup):
407-
print '{' + ', '.join(itemset) + '}'
404+
for itemset, support in find_frequent_itemsets(csv.reader(f), options.minsup, True):
405+
print '{' + ', '.join(itemset) + '} ' + str(support)
408406
finally:
409407
f.close()

test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,19 @@ def testPruning(self):
205205
root.child('a', 2).leaf()
206206
self.assertEqual(1, len(root.node.children))
207207

208+
def testSupport(self):
209+
"""
210+
Issue #2: incorrect support calculation for shared paths
211+
"""
212+
self.tree.add('abcd')
213+
self.tree.add('abd')
214+
paths = list(self.tree.prefix_paths('d'))
215+
ct = fp_growth.conditional_tree_from_paths(paths, 1)
216+
root = NodeTester(self,ct.root)
217+
a = root.child('a',2)
218+
b = a.child('b',2)
219+
c = b.child('c',1)
220+
208221
class FrequentSetTests(unittest.TestCase):
209222
def testDuplicate(self):
210223
raw = '25,52,274;71;71,274;52;25,52;274,71'

0 commit comments

Comments
 (0)