11
11
from collections import defaultdict
12
12
from itertools import combinations
13
13
14
+
14
15
def load_data () -> list [list [str ]]:
15
16
"""
16
17
Returns a sample transaction dataset.
@@ -24,7 +25,9 @@ def load_data() -> list[list[str]]:
24
25
class Apriori :
25
26
"""Apriori algorithm class with support, confidence, and lift filtering."""
26
27
27
- def __init__ (self , transactions , min_support = 0.25 , min_confidence = 0.5 , min_lift = 1.0 ):
28
+ def __init__ (
29
+ self , transactions , min_support = 0.25 , min_confidence = 0.5 , min_lift = 1.0
30
+ ):
28
31
self .transactions = [set (t ) for t in transactions ]
29
32
self .min_support = min_support
30
33
self .min_confidence = min_confidence
@@ -37,7 +40,9 @@ def __init__(self, transactions, min_support=0.25, min_confidence=0.5, min_lift=
37
40
38
41
def _get_support (self , itemset : frozenset ) -> float :
39
42
"""Return support of an itemset."""
40
- return sum (1 for t in self .transactions if itemset .issubset (t )) / len (self .transactions )
43
+ return sum (1 for t in self .transactions if itemset .issubset (t )) / len (
44
+ self .transactions
45
+ )
41
46
42
47
def confidence (self , antecedent : frozenset , consequent : frozenset ) -> float :
43
48
"""Calculate confidence of a rule A -> B."""
@@ -59,7 +64,11 @@ def find_frequent_itemsets(self):
59
64
item_counts [frozenset ([item ])] += 1
60
65
61
66
total = len (self .transactions )
62
- current_itemsets = {k : v / total for k , v in item_counts .items () if v / total >= self .min_support }
67
+ current_itemsets = {
68
+ k : v / total
69
+ for k , v in item_counts .items ()
70
+ if v / total >= self .min_support
71
+ }
63
72
self .itemsets .append (current_itemsets )
64
73
65
74
k = 2
@@ -69,10 +78,17 @@ def find_frequent_itemsets(self):
69
78
for i in range (len (keys )):
70
79
for j in range (i + 1 , len (keys )):
71
80
union = keys [i ] | keys [j ]
72
- if len (union ) == k and all (frozenset (sub ) in current_itemsets for sub in combinations (union , k - 1 )):
73
- candidates .add (union )
74
-
75
- freq_candidates = {c : self ._get_support (c ) for c in candidates if self ._get_support (c ) >= self .min_support }
81
+ if len (union ) == k and all (
82
+ frozenset (sub ) in current_itemsets
83
+ for sub in combinations (union , k - 1 )
84
+ ):
85
+ candidates .add (union )
86
+
87
+ freq_candidates = {
88
+ c : self ._get_support (c )
89
+ for c in candidates
90
+ if self ._get_support (c ) >= self .min_support
91
+ }
76
92
if not freq_candidates :
77
93
break
78
94
@@ -115,6 +131,6 @@ def generate_association_rules(self):
115
131
print ("\n Association Rules:" )
116
132
for rule in model .rules :
117
133
antecedent , consequent , conf , lift = rule
118
- print (f" { set ( antecedent ) } -> { set ( consequent ) } , conf= { conf :.2f } , lift= { lift :.2f } " )
119
-
120
-
134
+ print (
135
+ f" { set ( antecedent ) } -> { set ( consequent ) } , conf= { conf :.2f } , lift= { lift :.2f } "
136
+ )
0 commit comments