pktools  2.6.3
Processing Kernel for geospatial data
ConfusionMatrix.h
1 /**********************************************************************
2 ConfusionMatrix.h: class for (classification accuracy) confusion matrix
3 Copyright (C) 2008-2012 Pieter Kempeneers
4 
5 This file is part of pktools
6 
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19 ***********************************************************************/
20 #ifndef _CONFUSIONMATRIX_H_
21 #define _CONFUSIONMATRIX_H_
22 
23 #include <sstream>
24 #include <vector>
25 #include "base/Vector2d.h"
26 #include "base/Optionpk.h"
27 
29 public:
31  ConfusionMatrix(short nclass);
32  ConfusionMatrix(const std::vector<std::string>& classNames);
34  ConfusionMatrix& operator=(const ConfusionMatrix& cm);
35  short size() const {return m_results.size();};
36  void resize(short nclass);
37  void setClassNames(const std::vector<std::string>& classNames, bool doSort=false);
38  void pushBackClassName(const std::string& className, bool doSort=false);
39  void setResults(const Vector2d<double>& theResults);
40  void setResult(const std::string& theRef, const std::string& theClass, double theResult);
41  void incrementResult(const std::string& theRef, const std::string& theClass, double theIncrement);
42  void clearResults();
43  double nReference(const std::string& theRef) const;
44  double nReference() const;
45  double nClassified(const std::string& theRef) const;
46  int nClasses() const {return m_classes.size();};
47  std::string getClass(int iclass) const {assert(iclass>=0);assert(iclass<m_classes.size());return m_classes[iclass];};
48  int getClassIndex(std::string className) const {
49  int index=0;
50  for(index=0;index<m_classes.size();++index){
51  if(m_classes[index]==className)
52  break;
53  }
54  if(index>=m_classes.size())
55  index=-1;
56  return index;
57  // int index=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),className));
58  // assert(index>=0);
59  // if(index<m_results.size())
60  // return(index);
61  // else
62  // return(-1);
63  }
64  std::vector<std::string> getClassNames() const {return m_classes;};
65  ~ConfusionMatrix();
66  double pa(const std::string& theClass, double* se95=NULL) const;
67  double ua(const std::string& theClass, double* se95=NULL) const;
68  double oa(double* se95=NULL) const;
69  int pa_pct(const std::string& theClass, double* se95=NULL) const;
70  int ua_pct(const std::string& theClass, double* se95=NULL) const;
71  int oa_pct(double* se95=NULL) const;
72  double kappa() const;
73  ConfusionMatrix& operator*=(double weight);
74  ConfusionMatrix operator*(double weight);
75  ConfusionMatrix& operator+=(const ConfusionMatrix &cm);
76  ConfusionMatrix operator+(const ConfusionMatrix &cm){
77  return ConfusionMatrix(*this)+=cm;
78  }
79  void sortClassNames();
80  friend std::ostream& operator<<(std::ostream& os, const ConfusionMatrix &cm){
81  for(int iclass=0;iclass<cm.nClasses();++iclass)
82  os << "\t" << cm.m_classes[iclass];
83  os << std::endl;
84  assert(cm.m_classes.size()==cm.m_results.size());
85  for(int irow=0;irow<cm.m_results.size();++irow){
86  os << cm.m_classes[irow];
87  for(int icol=0;icol<cm.m_results[irow].size();++icol)
88  os << "\t" << cm.m_results[irow][icol];
89  os << std::endl;
90  }
91  return os;
92  };
93 private:
94  std::vector<std::string> m_classes;
95  Vector2d<double> m_results;
96 };
97 
98 #endif /* _CONFUSIONMATRIX_H_ */