mageec  0.1.0
MAchine Guided Energy Efficient Compilation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
confmat.c
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Copyright 2010 Rulequest Research Pty Ltd. */
4 /* */
5 /* This file is part of C5.0 GPL Edition, a single-threaded version */
6 /* of C5.0 release 2.07. */
7 /* */
8 /* C5.0 GPL Edition is free software: you can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License as */
10 /* published by the Free Software Foundation, either version 3 of the */
11 /* License, or (at your option) any later version. */
12 /* */
13 /* C5.0 GPL Edition is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16 /* General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU General Public License */
19 /* (gpl.txt) along with C5.0 GPL Edition. If not, see */
20 /* */
21 /* <http://www.gnu.org/licenses/>. */
22 /* */
23 /*************************************************************************/
24 
25 
26 
27 /*************************************************************************/
28 /* */
29 /* Routine for printing confusion matrices and attribute usage */
30 /* ----------------------------------------------------------- */
31 /* */
32 /*************************************************************************/
33 
34 #include "defns.i"
35 #include "extern.i"
36 
37 
38 void PrintConfusionMatrix(CaseNo *ConfusionMat)
39 /* -------------------- */
40 {
41  int Row, Col, Entry, EntryWidth=10000;
42 
43  /* For more than 20 classes, use summary instead */
44 
45  if ( MaxClass > 20 )
46  {
47  PrintErrorBreakdown(ConfusionMat);
48  return;
49  }
50 
51  /* Find maximum entry width in chars */
52 
53  ForEach(Row, 1, MaxClass)
54  {
55  ForEach(Col, 1, MaxClass)
56  {
57  EntryWidth = Max(EntryWidth, ConfusionMat[Row*(MaxClass+1) + Col]);
58  }
59  }
60 
61  EntryWidth = floor(Log(EntryWidth + 100.0) / Log(10.0)) + 2;
62 
63  /* Print the heading, then each row */
64 
65  fprintf(Of, "\n\n\t");
66  ForEach(Col, 1, MaxClass)
67  {
68  fprintf(Of, "%*s(%c)", EntryWidth-3, " ", 'a' + Col-1);
69  }
70 
71  fprintf(Of, " <-" T_classified_as "\n\t");
72  ForEach(Col, 1, MaxClass)
73  {
74  fprintf(Of, "%*.*s", EntryWidth, EntryWidth-2, "----------");
75  }
76  fprintf(Of, "\n");
77 
78  ForEach(Row, 1, MaxClass)
79  {
80  fprintf(Of, "\t");
81  ForEach(Col, 1, MaxClass)
82  {
83  if ( (Entry = ConfusionMat[Row*(MaxClass+1) + Col]) )
84  {
85  fprintf(Of, " %*d", EntryWidth-1, Entry);
86  }
87  else
88  {
89  fprintf(Of, "%*s", EntryWidth, " ");
90  }
91  }
92  fprintf(Of, " (%c): " T_class " %s\n", 'a' + Row-1, ClassName[Row]);
93  }
94 }
95 
96 
97 
98 void PrintErrorBreakdown(CaseNo *ConfusionMat)
99 /* ------------------- */
100 {
101  CaseNo *TruePos, *FalsePos, *FalseNeg, Entry;
102  int Row, Col, EntryWidth=100000, ClassWidth=5;
103 
104  TruePos = AllocZero(MaxClass+1, CaseNo);
105  FalsePos = AllocZero(MaxClass+1, CaseNo);
106  FalseNeg = AllocZero(MaxClass+1, CaseNo);
107 
108  ForEach(Row, 1, MaxClass)
109  {
110  ForEach(Col, 1, MaxClass)
111  {
112  Entry = ConfusionMat[Row*(MaxClass+1) + Col];
113 
114  if ( Col == Row )
115  {
116  TruePos[Row] += Entry;
117  }
118  else
119  {
120  FalseNeg[Row] += Entry;
121  FalsePos[Col] += Entry;
122  }
123  }
124 
125  EntryWidth = Max(EntryWidth, TruePos[Row] + FalseNeg[Row]);
126  ClassWidth = Max(ClassWidth, strlen(ClassName[Row]));
127  }
128 
129  EntryWidth = floor(Log(EntryWidth + 100.0) / Log(10.0)) + 2;
130 
131  /* Print heading (tricky spacing) */
132 
133  fprintf(Of, "\n\n\t %-*s %*s %*s %*s\n\t %*s %*s %*s %*s\n",
134  ClassWidth, "Class",
135  EntryWidth, "Cases",
136  EntryWidth, "False",
137  EntryWidth, "False",
138  ClassWidth, "",
139  EntryWidth, "",
140  EntryWidth, "Pos",
141  EntryWidth, "Neg");
142  fprintf(Of, "\t %-*s %*s %*s %*s\n",
143  ClassWidth, "-----",
144  EntryWidth, "-----",
145  EntryWidth, "-----",
146  EntryWidth, "-----");
147 
148  ForEach(Row, 1, MaxClass)
149  {
150  fprintf(Of, "\t %-*s %*d %*d %*d\n",
151  ClassWidth, ClassName[Row],
152  EntryWidth, TruePos[Row] + FalseNeg[Row],
153  EntryWidth, FalsePos[Row],
154  EntryWidth, FalseNeg[Row]);
155  }
156 
157  Free(TruePos);
158  Free(FalsePos);
159  Free(FalseNeg);
160 }
161 
162 
163 
164 void PrintUsageInfo(CaseNo *Usage)
165 /* -------------- */
166 {
167  Attribute Att, Best;
168  float Tests;
169  Boolean First=true;
170 
171  Tests = Max(1, MaxCase+1);
172 
173  while ( true )
174  {
175  Best = 0;
176 
177  ForEach(Att, 1, MaxAtt)
178  {
179  if ( Usage[Att] > Usage[Best] ) Best = Att;
180  }
181 
182  if ( ! Best || Usage[Best] < 0.01 * Tests ) break;
183 
184  if ( First )
185  {
186  fprintf(Of, T_Usage);
187  First = false;
188  }
189 
190  fprintf(Of, "\t%7d%% %s\n",
191  (int) ((100 * Usage[Best]) / Tests + 0.5), AttName[Best]);
192 
193  Usage[Best] = 0;
194  }
195 }