-
-
Save staticor/a8c52aae159a5279cb6f377c21e73d93 to your computer and use it in GitHub Desktop.
apriori_rules.py
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
def generateRules(L, support_data, min_confidence=0.7): | |
"""Create the association rules | |
L: list of frequent item sets | |
support_data: support data for those itemsets | |
min_confidence: minimum confidence threshold | |
""" | |
rules = [] | |
for i in range(1, len(L)): | |
for freqSet in L[i]: | |
H1 = [frozenset([item]) for item in freqSet] | |
print "freqSet", freqSet, 'H1', H1 | |
if (i > 1): | |
rules_from_conseq(freqSet, H1, support_data, rules, min_confidence) | |
else: | |
calc_confidence(freqSet, H1, support_data, rules, min_confidence) | |
return rules | |
def calc_confidence(freqSet, H, support_data, rules, min_confidence=0.7): | |
"Evaluate the rule generated" | |
pruned_H = [] | |
for conseq in H: | |
conf = support_data[freqSet] / support_data[freqSet - conseq] | |
if conf >= min_confidence: | |
print freqSet - conseq, '--->', conseq, 'conf:', conf | |
rules.append((freqSet - conseq, conseq, conf)) | |
pruned_H.append(conseq) | |
return pruned_H | |
def rules_from_conseq(freqSet, H, support_data, rules, min_confidence=0.7): | |
"Generate a set of candidate rules" | |
m = len(H[0]) | |
if (len(freqSet) > (m + 1)): | |
Hmp1 = aprioriGen(H, m + 1) | |
Hmp1 = calc_confidence(freqSet, Hmp1, support_data, rules, min_confidence) | |
if len(Hmp1) > 1: | |
rules_from_conseq(freqSet, Hmp1, support_data, rules, min_confidence) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment