Skip to content

Refactor apriori algorithm - #14579

Open
btatongk wants to merge 6 commits into
TheAlgorithms:masterfrom
btatongk:refactor-apriori-algorithm
Open

btatongk wants to merge 6 commits into
TheAlgorithms:masterfrom
btatongk:refactor-apriori-algorithm

Conversation

@btatongk

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes Refactor the Apriori Algorithm #14577".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Apr 24, 2026
Comment thread machine_learning/apriori_algorithm.py Outdated
Comment thread machine_learning/apriori_algorithm.py
@cclauss

cclauss commented Sep 8, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev are the proposed changes useful?

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cclauss I dug into this one. The algorithmic restructuring is genuinely useful — it replaces the old ad-hoc prune() (which counted item tuples and never did true subset pruning) with the textbook Apriori shape: generate_candidates doing the F(k-1) ⋈ F(k-1) join, has_infrequent_subset doing proper (k-1)-subset pruning, and support counted with frozenset.issubset. That's a real correctness/readability win over main.

But it can't merge as-is — two blockers:

  1. All doctests were removed. apriori() and the deleted prune() lost their >>> examples; doctest.testmod() now tests nothing. This repo requires doctests (or a dedicated test) for every function, so we'd be reducing coverage. The refactor needs fresh doctests that pin the new output.

  2. The return type is inconsistent (and the annotation is wrong). The signature says -> list[tuple[frozenset, int]], but 1-itemsets are appended as (next(iter(i)), c) → a str first element, while k≥2 itemsets are (sorted(c), count) → a list first element. Running it on load_data() with min_support=2 gives:

    ('milk', 4)          # str
    ('bread', 2)         # str
    (['bread', 'milk'], 2)  # list
    

    Callers can't consume that uniformly, and it doesn't match the annotation. Also note the output contract changed vs main (1-itemsets are now included), which should be called out in the PR description.

Suggested path to merge: make the 1-itemset rows (sorted(i), c) so every row is (list[str], int), correct the annotation to list[tuple[list[str], int]], and add doctests for both helpers and apriori(). If the author does that, I'd be happy to re-review — the underlying refactor is worth landing.

@cclauss cclauss added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes A maintainer has requested changes to this PR enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor the Apriori Algorithm

5 participants