Skip to content

Commit

Permalink
Replace _FixedLengthTrie by set
Browse files Browse the repository at this point in the history
This is now possible because tuples are hashable.
  • Loading branch information
dbarbier committed Jan 3, 2020
1 parent 85ca67d commit 2917065
Showing 1 changed file with 2 additions and 40 deletions.
42 changes: 2 additions & 40 deletions mlxtend/frequent_patterns/apriori.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,6 @@
from ..frequent_patterns import fpcommon as fpc


class _FixedLengthTrie:

"""Fixed-length trie (prefix tree).
Parameters
----------
combinations: list of itemsets
All combinations with enough support in the last step
Attributes
----------
root : dict
Root node
"""
__slots__ = ("root")

def __init__(self, combinations):
self.root = dict()
for combination in combinations:
current = self.root
for item in combination:
try:
current = current[item]
except KeyError:
next_node = dict()
current[item] = next_node
current = next_node

def __contains__(self, combination):
current = self.root
try:
for item in combination:
current = current[item]
return True
except KeyError:
return False


def generate_new_combinations(old_combinations):
"""
Generator of all combinations based on the last state of Apriori algorithm
Expand Down Expand Up @@ -79,7 +41,7 @@ def generate_new_combinations(old_combinations):
"""

length = len(old_combinations)
trie = _FixedLengthTrie(old_combinations)
set_old_combinations = set(old_combinations)
for i, old_combination in enumerate(old_combinations):
head_i = list(old_combination[:-1])
j = i + 1
Expand All @@ -94,7 +56,7 @@ def generate_new_combinations(old_combinations):
for idx in range(len(candidate) - 2):
test_candidate = list(candidate)
del test_candidate[idx]
if test_candidate not in trie:
if tuple(test_candidate) not in set_old_combinations:
# early exit from for-loop skips else clause just below
break
else:
Expand Down

0 comments on commit 2917065

Please sign in to comment.