Skip to content

Commit 2917065

Browse files
committed
Replace _FixedLengthTrie by set
This is now possible because tuples are hashable.
1 parent 85ca67d commit 2917065

File tree

1 file changed

+2
-40
lines changed

1 file changed

+2
-40
lines changed

mlxtend/frequent_patterns/apriori.py

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,6 @@
99
from ..frequent_patterns import fpcommon as fpc
1010

1111

12-
class _FixedLengthTrie:
13-
14-
"""Fixed-length trie (prefix tree).
15-
16-
Parameters
17-
----------
18-
combinations: list of itemsets
19-
All combinations with enough support in the last step
20-
21-
Attributes
22-
----------
23-
root : dict
24-
Root node
25-
"""
26-
__slots__ = ("root")
27-
28-
def __init__(self, combinations):
29-
self.root = dict()
30-
for combination in combinations:
31-
current = self.root
32-
for item in combination:
33-
try:
34-
current = current[item]
35-
except KeyError:
36-
next_node = dict()
37-
current[item] = next_node
38-
current = next_node
39-
40-
def __contains__(self, combination):
41-
current = self.root
42-
try:
43-
for item in combination:
44-
current = current[item]
45-
return True
46-
except KeyError:
47-
return False
48-
49-
5012
def generate_new_combinations(old_combinations):
5113
"""
5214
Generator of all combinations based on the last state of Apriori algorithm
@@ -79,7 +41,7 @@ def generate_new_combinations(old_combinations):
7941
"""
8042

8143
length = len(old_combinations)
82-
trie = _FixedLengthTrie(old_combinations)
44+
set_old_combinations = set(old_combinations)
8345
for i, old_combination in enumerate(old_combinations):
8446
head_i = list(old_combination[:-1])
8547
j = i + 1
@@ -94,7 +56,7 @@ def generate_new_combinations(old_combinations):
9456
for idx in range(len(candidate) - 2):
9557
test_candidate = list(candidate)
9658
del test_candidate[idx]
97-
if test_candidate not in trie:
59+
if tuple(test_candidate) not in set_old_combinations:
9860
# early exit from for-loop skips else clause just below
9961
break
10062
else:

0 commit comments

Comments
 (0)