From def174d88696aa1d1d713848829fc50bec1f566f Mon Sep 17 00:00:00 2001 From: joaoneto9 Date: Wed, 24 Sep 2025 15:19:11 -0300 Subject: [PATCH 1/5] feat: optimizing the prune function at the apriori_algorithm.py archive --- machine_learning/apriori_algorithm.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/machine_learning/apriori_algorithm.py b/machine_learning/apriori_algorithm.py index 09a89ac236bd..54c6f7f4a9fb 100644 --- a/machine_learning/apriori_algorithm.py +++ b/machine_learning/apriori_algorithm.py @@ -12,7 +12,7 @@ """ from itertools import combinations - +from collections import Counter def load_data() -> list[list[str]]: """ @@ -32,7 +32,7 @@ def prune(itemset: list, candidates: list, length: int) -> list: the frequent itemsets of the previous iteration (valid subsequences of the frequent itemsets from the previous iteration). - Prunes candidate itemsets that are not frequent. + Prunes candidate itemsets that are not frequent using Counter for optimization. >>> itemset = ['X', 'Y', 'Z'] >>> candidates = [['X', 'Y'], ['X', 'Z'], ['Y', 'Z']] @@ -44,11 +44,13 @@ def prune(itemset: list, candidates: list, length: int) -> list: >>> prune(itemset, candidates, 3) [] """ + itemset_counter = Counter(itemset) pruned = [] + for candidate in candidates: is_subsequence = True for item in candidate: - if item not in itemset or itemset.count(item) < length - 1: + if item not in itemset_counter or itemset_counter[item] < length - 1: is_subsequence = False break if is_subsequence: From c2d061309c863779d755a8edf91c36df6c9d5500 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 24 Sep 2025 18:48:29 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/apriori_algorithm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/machine_learning/apriori_algorithm.py b/machine_learning/apriori_algorithm.py index 54c6f7f4a9fb..dca3758e183e 100644 --- a/machine_learning/apriori_algorithm.py +++ b/machine_learning/apriori_algorithm.py @@ -14,6 +14,7 @@ from itertools import combinations from collections import Counter + def load_data() -> list[list[str]]: """ Returns a sample transaction dataset. @@ -46,7 +47,7 @@ def prune(itemset: list, candidates: list, length: int) -> list: """ itemset_counter = Counter(itemset) pruned = [] - + for candidate in candidates: is_subsequence = True for item in candidate: From 839c43ad540f83bfa543f820c9a805af7dae9e0f Mon Sep 17 00:00:00 2001 From: joaoneto9 Date: Wed, 24 Sep 2025 15:51:11 -0300 Subject: [PATCH 3/5] fix: fixing the unsorted importing statment --- machine_learning/apriori_algorithm.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/machine_learning/apriori_algorithm.py b/machine_learning/apriori_algorithm.py index 54c6f7f4a9fb..cbd44b401a5c 100644 --- a/machine_learning/apriori_algorithm.py +++ b/machine_learning/apriori_algorithm.py @@ -10,9 +10,8 @@ WIKI: https://en.wikipedia.org/wiki/Apriori_algorithm Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining """ - -from itertools import combinations from collections import Counter +from itertools import combinations def load_data() -> list[list[str]]: """ From 38e849bcb7d3050e40e4be2079fd15ba67659cac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 24 Sep 2025 18:54:30 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/apriori_algorithm.py | 1 + 1 file changed, 1 insertion(+) diff --git a/machine_learning/apriori_algorithm.py b/machine_learning/apriori_algorithm.py index 90e8484b076a..e7f79772861b 100644 --- a/machine_learning/apriori_algorithm.py +++ b/machine_learning/apriori_algorithm.py @@ -10,6 +10,7 @@ WIKI: https://en.wikipedia.org/wiki/Apriori_algorithm Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining """ + from collections import Counter from itertools import combinations From 789f76da9dd8da5f41b2da64101ece2cefd35c3f Mon Sep 17 00:00:00 2001 From: joaoneto9 Date: Wed, 24 Sep 2025 16:54:10 -0300 Subject: [PATCH 5/5] fix: fixing the key structure to a tuple that can be an hashable structure --- machine_learning/apriori_algorithm.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/machine_learning/apriori_algorithm.py b/machine_learning/apriori_algorithm.py index 90e8484b076a..ba9f779585cc 100644 --- a/machine_learning/apriori_algorithm.py +++ b/machine_learning/apriori_algorithm.py @@ -44,13 +44,14 @@ def prune(itemset: list, candidates: list, length: int) -> list: >>> prune(itemset, candidates, 3) [] """ - itemset_counter = Counter(itemset) + itemset_counter = Counter(tuple(x) for x in itemset) pruned = [] for candidate in candidates: is_subsequence = True for item in candidate: - if item not in itemset_counter or itemset_counter[item] < length - 1: + tupla = tuple(item) + if tupla not in itemset_counter or itemset_counter[tupla] < length - 1: is_subsequence = False break if is_subsequence: