Go to the documentation of this file.00001 import os, sys
00002 from numpy import *
00003 import matplotlib.pyplot as plt
00004 from pylab import *
00005
00006 conf_array_array = []
00007
00008 def buildConfusionMatrix(path,nr):
00009 for i in range(1, int(nr)):
00010 input = open(path)
00011
00012 k = l = 0
00013 nr_lines = 0
00014 conf_array = []
00015 for el in range(1, int((nr))):
00016 conf_array.append(0)
00017 for line in input.xreadlines():
00018
00019 j = [int(line.split()[0])/10, int(line.split()[1])/10]
00020
00021 if i == int(j[1]):
00022 conf_array[int(j[0]) - 1] = conf_array[int(j[0]) - 1] + 1
00023
00024 nr_lines = nr_lines + 1
00025 print "nr lines", nr_lines
00026
00027 conf_array_array.append(conf_array)
00028 input.close()
00029 print 'rows are ground truth, columns the classification: ', conf_array_array
00030 compute_false_pos_statistics(conf_array_array)
00031 plot_confusion_matrix(conf_array_array, int(nr)-1)
00032
00033 def plot_confusion_matrix(conf_arr, nr):
00034 norm_conf = []
00035 for i in conf_arr:
00036 a = 0
00037 tmp_arr = []
00038 a = sum(i,0)
00039 for j in i:
00040 if j == 0 and a == 0:
00041 tmp_arr.append(0)
00042 else:
00043 tmp_arr.append(float(j)/float(a))
00044
00045 norm_conf.append(tmp_arr)
00046
00047 plt.clf()
00048 fig = plt.figure()
00049 plt.xticks( arange(nr), ('Cylinder', 'Flat', 'Tetrapak', 'Box', 'Tube', 'Other'), rotation=90)
00050 plt.yticks( arange(nr), ('Cylinder', 'Flat', 'Tetrapak', 'Box', 'Tube', 'Other') )
00051
00052
00053 plt.title('GRSD Confusion Matrix for Test Views')
00054
00055
00056 ax = fig.add_subplot(111)
00057 res = ax.imshow(array(norm_conf), cmap=cm.gray, interpolation='nearest', aspect='auto')
00058 cb = fig.colorbar(res)
00059 savefig("confmat.png", format="png", bbox_inches='tight')
00060
00061 def compute_false_pos_statistics(conf_arr):
00062 tp = 0
00063 all = 0
00064 array_len = conf_arr.__len__()
00065 for i in range(array_len):
00066 tp = tp + conf_arr[i][i]
00067 all = all + sum(conf_arr[i])
00068 print 'all: ', all, 'true positives: ', tp, 'percentage: ', double(tp)/double(all)
00069
00070
00071 if __name__ == "__main__":
00072 if sys.argv.__len__() < 3:
00073 print "Usage: build_confusion_matrix.py \t test.conf \t number_of_classes+1 \n\nAlso, adopt lines plt.xticks plt.yticks with actual category names"
00074 sys.exit()
00075 path = sys.argv[1]
00076 nr = sys.argv[2]
00077 buildConfusionMatrix(path, nr)