public
Created

  • Download Gist
marcelcaraciolo created this gist . View gist @ 01e75ce
Apriori_rules.py
39 
...                                                                                 ... 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  
@@ -0,0 +1,39 @@
+
+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)
\ No newline at end of file
Something went wrong with that request. Please try again.