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 <mithridatus@mail.ru>
This commit is contained in:
@@ -11,6 +11,7 @@ WIKI: https://en.wikipedia.org/wiki/Apriori_algorithm
|
|||||||
Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining
|
Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
|
|
||||||
|
|
||||||
@@ -44,11 +45,16 @@ def prune(itemset: list, candidates: list, length: int) -> list:
|
|||||||
>>> prune(itemset, candidates, 3)
|
>>> prune(itemset, candidates, 3)
|
||||||
[]
|
[]
|
||||||
"""
|
"""
|
||||||
|
itemset_counter = Counter(tuple(item) for item in itemset)
|
||||||
pruned = []
|
pruned = []
|
||||||
for candidate in candidates:
|
for candidate in candidates:
|
||||||
is_subsequence = True
|
is_subsequence = True
|
||||||
for item in candidate:
|
for item in candidate:
|
||||||
if item not in itemset or itemset.count(item) < length - 1:
|
item_tuple = tuple(item)
|
||||||
|
if (
|
||||||
|
item_tuple not in itemset_counter
|
||||||
|
or itemset_counter[item_tuple] < length - 1
|
||||||
|
):
|
||||||
is_subsequence = False
|
is_subsequence = False
|
||||||
break
|
break
|
||||||
if is_subsequence:
|
if is_subsequence:
|
||||||
|
|||||||
Reference in New Issue
Block a user