Skip to content

Commit a00ee50

Browse files
committed
Replace pygtrie by a simplistic implementation
1 parent 71356a7 commit a00ee50

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

mlxtend/frequent_patterns/apriori.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,34 @@
66

77
import numpy as np
88
import pandas as pd
9-
import pygtrie
109
from ..frequent_patterns import fpcommon as fpc
1110

1211

12+
class _FixedLengthTrie:
13+
__slots__ = ("root")
14+
15+
def __init__(self, combinations):
16+
self.root = dict()
17+
for combination in combinations:
18+
current = self.root
19+
for item in combination:
20+
try:
21+
current = current[item]
22+
except KeyError:
23+
next_node = dict()
24+
current[item] = next_node
25+
current = next_node
26+
27+
def __contains__(self, combination):
28+
current = self.root
29+
try:
30+
for item in combination:
31+
current = current[item]
32+
return True
33+
except KeyError:
34+
return False
35+
36+
1337
def generate_new_combinations(old_combinations):
1438
"""
1539
Generator of all combinations based on the last state of Apriori algorithm
@@ -44,7 +68,7 @@ def generate_new_combinations(old_combinations):
4468
"""
4569

4670
length = len(old_combinations)
47-
trie = pygtrie.Trie(list(zip(old_combinations, [1]*length)))
71+
trie = _FixedLengthTrie(old_combinations)
4872
for i, old_combination in enumerate(old_combinations):
4973
*head_i, _ = old_combination
5074
j = i + 1

0 commit comments

Comments
 (0)