Skip to content

Commit 154cd3e

Browse files
joaoneto9pre-commit-ci[bot]MaximSmolskiy
authored
feat: optimizing the prune function at the apriori_algorithm.py archive (#12992)
* feat: optimizing the prune function at the apriori_algorithm.py archive * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: fixing the unsorted importing statment * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: fixing the key structure to a tuple that can be an hashable structure * Update apriori_algorithm.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update apriori_algorithm.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <[email protected]>
1 parent 3b08413 commit 154cd3e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

machine_learning/apriori_algorithm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining
1212
"""
1313

14+
from collections import Counter
1415
from itertools import combinations
1516

1617

@@ -44,11 +45,16 @@ def prune(itemset: list, candidates: list, length: int) -> list:
4445
>>> prune(itemset, candidates, 3)
4546
[]
4647
"""
48+
itemset_counter = Counter(tuple(item) for item in itemset)
4749
pruned = []
4850
for candidate in candidates:
4951
is_subsequence = True
5052
for item in candidate:
51-
if item not in itemset or itemset.count(item) < length - 1:
53+
item_tuple = tuple(item)
54+
if (
55+
item_tuple not in itemset_counter
56+
or itemset_counter[item_tuple] < length - 1
57+
):
5258
is_subsequence = False
5359
break
5460
if is_subsequence:

0 commit comments

Comments
 (0)