Skip to content

Commit ac19e67

Browse files
committed
refactor: improve candidate generation and output formatting in Apriori algorithm
1 parent 63b316e commit ac19e67

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

machine_learning/apriori_algorithm.py

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

14-
from itertools import combinations
1514
from collections import defaultdict
15+
from itertools import combinations
1616

1717

1818
def load_data() -> list[list[str]]:
@@ -74,7 +74,11 @@ def apriori(data: list[list[str]], min_support: int):
7474
if count >= min_support
7575
}
7676

77-
all_frequents = [(list(i)[0], c) for i, c in item_counts.items() if c >= min_support]
77+
all_frequents = [
78+
(next(iter(i)), c)
79+
for i, c in item_counts.items()
80+
if c >= min_support
81+
]
7882

7983
k = 2
8084

@@ -102,7 +106,7 @@ def apriori(data: list[list[str]], min_support: int):
102106
}
103107

104108
all_frequents.extend(
105-
(sorted(list(c)), count)
109+
(sorted(c), count)
106110
for c, count in candidate_counts.items()
107111
if count >= min_support
108112
)

0 commit comments

Comments
 (0)