From 4c43f5c4cfe4da4791c33ecfc5e1b51666cb2f92 Mon Sep 17 00:00:00 2001 From: Denis Barbier Date: Fri, 20 Dec 2019 11:51:47 +0100 Subject: [PATCH] Replace pygtrie by a simplistic implementation --- mlxtend/frequent_patterns/apriori.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/mlxtend/frequent_patterns/apriori.py b/mlxtend/frequent_patterns/apriori.py index eb918a3b9..79a97113a 100644 --- a/mlxtend/frequent_patterns/apriori.py +++ b/mlxtend/frequent_patterns/apriori.py @@ -6,10 +6,34 @@ import numpy as np import pandas as pd -import pygtrie from ..frequent_patterns import fpcommon as fpc +class _FixedLengthTrie: + __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 @@ -44,7 +68,7 @@ def generate_new_combinations(old_combinations): """ length = len(old_combinations) - trie = pygtrie.Trie(list(zip(old_combinations, [1]*length))) + trie = _FixedLengthTrie(old_combinations) for i, old_combination in enumerate(old_combinations): *head_i, _ = old_combination j = i + 1