From def174d88696aa1d1d713848829fc50bec1f566f Mon Sep 17 00:00:00 2001 From: joaoneto9 Date: Wed, 24 Sep 2025 15:19:11 -0300 Subject: [PATCH 1/8] 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/8] [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/8] 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/8] [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/8] 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: From f005cc0a2f07e3351774be12dbe5d7b906a6e71d Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 20 Oct 2025 00:11:18 +0300 Subject: [PATCH 6/8] Update apriori_algorithm.py --- machine_learning/apriori_algorithm.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/machine_learning/apriori_algorithm.py b/machine_learning/apriori_algorithm.py index 71b7e0c0a5f1..dd2f9cac2e51 100644 --- a/machine_learning/apriori_algorithm.py +++ b/machine_learning/apriori_algorithm.py @@ -33,7 +33,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 using Counter for optimization. + Prunes candidate itemsets that are not frequent. >>> itemset = ['X', 'Y', 'Z'] >>> candidates = [['X', 'Y'], ['X', 'Z'], ['Y', 'Z']] @@ -47,12 +47,11 @@ def prune(itemset: list, candidates: list, length: int) -> list: """ itemset_counter = Counter(tuple(x) for x in itemset) pruned = [] - for candidate in candidates: is_subsequence = True for item in candidate: - tupla = tuple(item) - if tupla not in itemset_counter or itemset_counter[tupla] < length - 1: + item_tuple = tuple(item) + if item_tuple not in itemset_counter or itemset_counter[item_tuple] < length - 1: is_subsequence = False break if is_subsequence: From 27261653d9daced5f3af3786dfd35f5971546b6a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 19 Oct 2025 21:11:37 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/apriori_algorithm.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/machine_learning/apriori_algorithm.py b/machine_learning/apriori_algorithm.py index dd2f9cac2e51..2bddfee13b25 100644 --- a/machine_learning/apriori_algorithm.py +++ b/machine_learning/apriori_algorithm.py @@ -51,7 +51,10 @@ def prune(itemset: list, candidates: list, length: int) -> list: is_subsequence = True for item in candidate: item_tuple = tuple(item) - if item_tuple not in itemset_counter or itemset_counter[item_tuple] < length - 1: + if ( + item_tuple not in itemset_counter + or itemset_counter[item_tuple] < length - 1 + ): is_subsequence = False break if is_subsequence: From 3a561b3b08c653755a3ba122514db9d414db05f8 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Mon, 20 Oct 2025 00:12:47 +0300 Subject: [PATCH 8/8] Update apriori_algorithm.py --- machine_learning/apriori_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine_learning/apriori_algorithm.py b/machine_learning/apriori_algorithm.py index 2bddfee13b25..5c3e2baba2c2 100644 --- a/machine_learning/apriori_algorithm.py +++ b/machine_learning/apriori_algorithm.py @@ -45,7 +45,7 @@ def prune(itemset: list, candidates: list, length: int) -> list: >>> prune(itemset, candidates, 3) [] """ - itemset_counter = Counter(tuple(x) for x in itemset) + itemset_counter = Counter(tuple(item) for item in itemset) pruned = [] for candidate in candidates: is_subsequence = True