-
-
Notifications
You must be signed in to change notification settings - Fork 49.7k
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
def174d
feat: optimizing the prune function at the apriori_algorithm.py archive
joaoneto9 c2d0613
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 839c43a
fix: fixing the unsorted importing statment
joaoneto9 81a9d8d
Merge branch 'master' of https://github.com/joaoneto9/Python
joaoneto9 38e849b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 789f76d
fix: fixing the key structure to a tuple that can be an hashable stru…
joaoneto9 42fe4b6
Merge branch 'master' of https://github.com/joaoneto9/Python
joaoneto9 c88b71f
Merge branch 'master' into master
joaoneto9 30aa721
Merge branch 'master' into master
joaoneto9 f005cc0
Update apriori_algorithm.py
MaximSmolskiy 2726165
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1957f9b
Merge branch 'master' into master
MaximSmolskiy 3a561b3
Update apriori_algorithm.py
MaximSmolskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
|
||
| is_subsequence = False | ||
| break | ||
| if is_subsequence: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.