-
-
Notifications
You must be signed in to change notification settings - Fork 48.5k
feat: optimizing the prune function at the apriori_algorithm.py archive #12992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 8 commits
def174d
c2d0613
839c43a
81a9d8d
38e849b
789f76d
42fe4b6
c88b71f
30aa721
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining | ||
""" | ||
|
||
from collections import Counter | ||
from itertools import combinations | ||
|
||
|
||
|
@@ -32,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. | ||
Prunes candidate itemsets that are not frequent using Counter for optimization. | ||
|
||
>>> itemset = ['X', 'Y', 'Z'] | ||
>>> candidates = [['X', 'Y'], ['X', 'Z'], ['Y', 'Z']] | ||
|
@@ -44,11 +45,14 @@ def prune(itemset: list, candidates: list, length: int) -> list: | |
>>> prune(itemset, candidates, 3) | ||
[] | ||
""" | ||
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 or itemset.count(item) < length - 1: | ||
tupla = tuple(item) | ||
if tupla not in itemset_counter or itemset_counter[tupla] < length - 1: | ||
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tuple conversion is performed twice for the same data - once when creating the Counter and again when checking each item. Consider converting items to tuples consistently or using a different approach to avoid this duplication. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
is_subsequence = False | ||
break | ||
if is_subsequence: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tuple conversion is performed twice for the same data - once when creating the Counter and again when checking each item. Consider converting items to tuples consistently or using a different approach to avoid this duplication.
Copilot uses AI. Check for mistakes.