From 5a13f0924701c69fb9467d48958a762f8db3838d 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 c63d0019d..23332df05 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