-
Notifications
You must be signed in to change notification settings - Fork 1
/
assocrules.py
43 lines (34 loc) · 1.3 KB
/
assocrules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class assocrules(object):
"""description of class"""
def mine_assoc_rules(isets, min_support=2, min_confidence=0.5):
rules = []
visited = set()
for key in sorted(isets, key=lambda k: len(k), reverse=True):
support = isets[key]
if support < min_support or len(key) < 2:
continue
for winnie in key:
left = key.difference([winnie])
right = frozenset([winnie])
_mine_assoc_rules(
left, right, support, visited, isets,
min_support, min_confidence, rules)
return rules
def _mine_assoc_rules(
left, right, rule_support, visited, isets, min_support,
min_confidence, rules):
if (left, right) in visited or len(left) < 1:
return
else:
visited.add((left, right))
support_a = isets[left]
confidence = float(rule_support) / float(support_a)
if confidence >= min_confidence:
rules.append((left, right, rule_support, confidence))
# We can try to increase right!
for winnie in left:
new_left = left.difference([winnie])
new_right = right.union([winnie])
_mine_assoc_rules(
new_left, new_right, rule_support, visited, isets,
min_support, min_confidence, rules)