| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _CONFUSIONMATRIX_H_ |
| 22 |
#define _CONFUSIONMATRIX_H_ |
| 23 |
|
| 24 |
#include <sstream> |
| 25 |
#include <vector> |
| 26 |
#include "base/Vector2d.h" |
| 27 |
#include "base/Optionjl.h" |
| 28 |
|
| 29 |
namespace confusionmatrix |
| 30 |
{ |
| 31 |
enum CM_FORMAT { ASCII = 0, LATEX = 1, HTML = 2 }; |
| 32 |
|
| 33 |
class ConfusionMatrix{ |
| 34 |
public: |
| 35 |
ConfusionMatrix(); |
| 36 |
ConfusionMatrix(short nclass); |
| 37 |
ConfusionMatrix(const std::vector<std::string>& classNames); |
| 38 |
ConfusionMatrix(const ConfusionMatrix& cm); |
| 39 |
ConfusionMatrix& operator=(const ConfusionMatrix& cm); |
| 40 |
short size() const {return m_results.size();}; |
| 41 |
void resize(short nclass); |
| 42 |
void setClassNames(const std::vector<std::string>& classNames, bool doSort=false); |
| 43 |
void pushBackClassName(const std::string& className, bool doSort=false); |
| 44 |
void setResults(const Vector2d<double>& theResults); |
| 45 |
void setResult(const std::string& theRef, const std::string& theClass, double theResult); |
| 46 |
void incrementResult(const std::string& theRef, const std::string& theClass, double theIncrement); |
| 47 |
void clearResults(); |
| 48 |
double nReference(const std::string& theRef) const; |
| 49 |
double nReference() const; |
| 50 |
double nClassified(const std::string& theRef) const; |
| 51 |
int nClasses() const {return m_classes.size();}; |
| 52 |
std::string getClass(int iclass) const {assert(iclass>=0);assert(iclass<m_classes.size());return m_classes[iclass];}; |
| 53 |
int getClassIndex(std::string className) const { |
| 54 |
unsigned int index=0; |
| 55 |
for(index=0;index<m_classes.size();++index){ |
| 56 |
if(m_classes[index]==className) |
| 57 |
break; |
| 58 |
} |
| 59 |
if(index>=m_classes.size()) |
| 60 |
index=-1; |
| 61 |
return index; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
} |
| 69 |
std::vector<std::string> getClassNames() const {return m_classes;}; |
| 70 |
~ConfusionMatrix(); |
| 71 |
double pa(const std::string& theClass, double* se95=NULL) const; |
| 72 |
double ua(const std::string& theClass, double* se95=NULL) const; |
| 73 |
double oa(double* se95=NULL) const; |
| 74 |
int pa_pct(const std::string& theClass, double* se95=NULL) const; |
| 75 |
int ua_pct(const std::string& theClass, double* se95=NULL) const; |
| 76 |
int oa_pct(double* se95=NULL) const; |
| 77 |
double kappa() const; |
| 78 |
ConfusionMatrix& operator*=(double weight); |
| 79 |
ConfusionMatrix operator*(double weight); |
| 80 |
ConfusionMatrix& operator+=(const ConfusionMatrix &cm); |
| 81 |
ConfusionMatrix operator+(const ConfusionMatrix &cm){ |
| 82 |
return ConfusionMatrix(*this)+=cm; |
| 83 |
} |
| 84 |
void sortClassNames(); |
| 85 |
|
| 86 |
void reportSE95(bool doReport) {m_se95=doReport;}; |
| 87 |
void setFormat(const CM_FORMAT& theFormat) {m_format=theFormat;}; |
| 88 |
void setFormat(const std::string theFormat) {m_format=getFormat(theFormat);}; |
| 89 |
CM_FORMAT getFormat() const {return m_format;}; |
| 90 |
|
| 91 |
static const CM_FORMAT getFormat(const std::string theFormat){ |
| 92 |
if(theFormat=="ascii") return(ASCII); |
| 93 |
else if(theFormat=="latex") return(LATEX); |
| 94 |
else{ |
| 95 |
std::string errorString="Format not supported: "; |
| 96 |
errorString+=theFormat; |
| 97 |
errorString+=" use ascii or latex"; |
| 98 |
throw(errorString); |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
friend std::ostream& operator<<(std::ostream& os, const ConfusionMatrix &cm){ |
| 103 |
std::ostringstream streamLine; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
std::string fieldSeparator=" "; |
| 108 |
std::string lineSeparator=""; |
| 109 |
std::string mathMode=""; |
| 110 |
switch(cm.getFormat()){ |
| 111 |
case(LATEX): |
| 112 |
fieldSeparator=" & "; |
| 113 |
lineSeparator="\\\\"; |
| 114 |
mathMode="$"; |
| 115 |
break; |
| 116 |
case(ASCII): |
| 117 |
default: |
| 118 |
fieldSeparator="\t"; |
| 119 |
lineSeparator=""; |
| 120 |
mathMode=""; |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
double se95_ua=0; |
| 125 |
double se95_pa=0; |
| 126 |
double se95_oa=0; |
| 127 |
double dua=0; |
| 128 |
double dpa=0; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
if(cm.getFormat()==LATEX){ |
| 133 |
os << "\\documentclass{article}" << std::endl; |
| 134 |
os << "\\begin{document}" << std::endl; |
| 135 |
} |
| 136 |
os << "Kappa = " << mathMode << cm.kappa() << mathMode ; |
| 137 |
os << ", Overall Acc. = " << mathMode << 100.0*cm.oa() << mathMode ; |
| 138 |
if(cm.m_se95) |
| 139 |
os << " (" << mathMode << se95_oa << mathMode << ")"; |
| 140 |
os << std::endl; |
| 141 |
os << std::endl; |
| 142 |
if(cm.getFormat()==LATEX){ |
| 143 |
os << "\\begin{tabular}{@{}l"; |
| 144 |
for(int iclass=0;iclass<cm.nClasses();++iclass) |
| 145 |
os << "l"; |
| 146 |
os << "}" << std::endl; |
| 147 |
os << "\\hline" << std::endl; |
| 148 |
} |
| 149 |
|
| 150 |
os << "Class"; |
| 151 |
for(int iclass=0;iclass<cm.nClasses();++iclass) |
| 152 |
os << fieldSeparator << cm.m_classes[iclass]; |
| 153 |
os << lineSeparator << std::endl; |
| 154 |
if(cm.getFormat()==LATEX) |
| 155 |
os << "\\hline" << std::endl; |
| 156 |
assert(cm.m_classes.size()==cm.m_results.size()); |
| 157 |
for(unsigned int irow=0;irow<cm.m_results.size();++irow){ |
| 158 |
os << cm.m_classes[irow]; |
| 159 |
for(unsigned int icol=0;icol<cm.m_results[irow].size();++icol) |
| 160 |
os << fieldSeparator << cm.m_results[irow][icol]; |
| 161 |
os << lineSeparator<< std::endl; |
| 162 |
} |
| 163 |
if(cm.getFormat()==LATEX){ |
| 164 |
os << "\\hline" << std::endl; |
| 165 |
} |
| 166 |
else |
| 167 |
os << std::endl; |
| 168 |
|
| 169 |
os << "User' Acc."; |
| 170 |
for(int iclass=0;iclass<cm.nClasses();++iclass){ |
| 171 |
dua=cm.ua_pct(cm.m_classes[iclass],&se95_ua); |
| 172 |
os << fieldSeparator << dua; |
| 173 |
if(cm.m_se95) |
| 174 |
os << " (" << se95_ua << ")"; |
| 175 |
} |
| 176 |
os << lineSeparator<< std::endl; |
| 177 |
os << "Prod. Acc."; |
| 178 |
for(int iclass=0;iclass<cm.nClasses();++iclass){ |
| 179 |
dpa=cm.pa_pct(cm.m_classes[iclass],&se95_ua); |
| 180 |
os << fieldSeparator << dpa; |
| 181 |
if(cm.m_se95) |
| 182 |
os << " (" << se95_pa << ")"; |
| 183 |
} |
| 184 |
os << lineSeparator<< std::endl; |
| 185 |
if(cm.getFormat()==LATEX){ |
| 186 |
os << "\\end{tabular}" << std::endl; |
| 187 |
os << "\\end{document}" << std::endl; |
| 188 |
} |
| 189 |
return os; |
| 190 |
}; |
| 191 |
private: |
| 192 |
std::vector<std::string> m_classes; |
| 193 |
Vector2d<double> m_results; |
| 194 |
CM_FORMAT m_format; |
| 195 |
bool m_se95; |
| 196 |
}; |
| 197 |
} |
| 198 |
#endif |