diff --git a/ss2013/1_Web Mining/Uebungen/4_Uebung/code/confusion_matrix.py b/ss2013/1_Web Mining/Uebungen/4_Uebung/code/confusion_matrix.py index 32c18c50..52f7fb8c 100644 --- a/ss2013/1_Web Mining/Uebungen/4_Uebung/code/confusion_matrix.py +++ b/ss2013/1_Web Mining/Uebungen/4_Uebung/code/confusion_matrix.py @@ -14,8 +14,10 @@ conf_matr = [[0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0, accuracy = 0 document_count = 0 precision = [0,0,0,0,0,0,0,0,0,0,0] -precision_avg = 0 +precision_micro = 0 +precision_macro = 0 recalls = [0,0,0,0,0,0,0,0,0,0,0] +recall_micro = 0 def load_predictionfile(file): global document_count @@ -75,11 +77,11 @@ def prec(): i += 1 def macro_prec(): - global precision_avg + global precision_macro global precision for p in precision: - precision_avg += p - precision_avg = float(precision_avg) / 11 + precision_macro += p + precision_macro = float(precision_macro) / 11 def pp_confusionmatrix(): @@ -113,7 +115,7 @@ def pp_prec(): print line def pp_macroprec(): - print "Precision Macroavg: "+str(round(precision_avg*100,4))+"%" + print "Precision Macroavg: "+str(round(precision_macro*100,4))+"%" def recall(): i = 0 @@ -138,18 +140,83 @@ def pp_recall(): i += 1 print line +def prec_micro(): + i = 0 + result = [0,0,0,0] + for c in classes: + res = conf_micro_class(i) + result[0] += res[0] + result[1] += res[1] + result[2] += res[2] + result[3] += res[3] + i += 1 + + global precision_micro + precision_micro = float(result[0]) / float(result[0]+result[1]) + +def conf_micro_class(class_): + i = 0 + ok_values = 0 #class ok, is ok + wrong_values = 0 # class ok, is not ok + wrong_values_others = 0 # class not ok, is not ok + ok_values_others = 0 # class not ok, is ok + for conf in conf_matr: + j = 0 + for c in conf: + if i == class_: + if i == j: + ok_values += c + else: + wrong_values += c + else: + if i == j: + ok_values_others += c + else: + wrong_values_others += c + j += 1 + i += 1 + return [ok_values,wrong_values,wrong_values_others,ok_values_others] + +def pp_microprec(): + print "Precision Microavg: "+str(round(precision_micro*100,4))+"%" + +def recall_micro(): + i = 0 + result = [0,0,0,0] + for c in classes: + res = conf_micro_class(i) + result[0] += res[0] + result[1] += res[1] + result[2] += res[2] + result[3] += res[3] + i += 1 + + global recall_micro + recall_micro = float(result[0]) / float(result[0]+result[2]) + +def pp_microrecall(): + print "Recall Microavg: "+str(round(recall_micro*100,4))+"%" + if __name__ == '__main__': load_predictionfile(predfile) #print preds confusion_matrix() pp_confusionmatrix() + accuracy() pp_accuracy() + prec() pp_prec() macro_prec() pp_macroprec() + prec_micro() + pp_microprec() + + recall() pp_recall() + recall_micro() + pp_microrecall() \ No newline at end of file