[pktools] 112/375: sorting classes numerically in confusion matrix

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:05 UTC 2014


This is an automated email from the git hooks/post-receive script.

sebastic-guest pushed a commit to branch upstream-master
in repository pktools.

commit 54ee080f196344eb447d627123052144608838d1
Author: user <user at osgeolive.(none)>
Date:   Mon May 20 09:45:48 2013 +0200

    sorting classes numerically in confusion matrix
---
 src/algorithms/ConfusionMatrix.cc | 17 +++++++++++++++--
 src/algorithms/ConfusionMatrix.h  |  6 ++++--
 src/apps/pkclassify_nn.cc         | 10 ++++++++--
 src/apps/pkclassify_svm.cc        | 12 +++++++++---
 4 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/src/algorithms/ConfusionMatrix.cc b/src/algorithms/ConfusionMatrix.cc
index a2e0c36..a9a04f4 100644
--- a/src/algorithms/ConfusionMatrix.cc
+++ b/src/algorithms/ConfusionMatrix.cc
@@ -21,6 +21,11 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include <iostream>
 #include <numeric>
 
+bool compareClass(const string& string1, const string& string2){
+  int int1=string2type<int>(string1);
+  int int2=string2type<int>(string2);
+  return(int1<int2);
+};
 
 ConfusionMatrix::ConfusionMatrix()
   : m_classes(),m_results()
@@ -88,6 +93,10 @@ ConfusionMatrix& ConfusionMatrix::operator*=(double weight)
   return *this;
 }
 
+void ConfusionMatrix::sortClassNames(){
+  sort(m_classes.begin(),m_classes.end(),compareClass);
+}
+
 ConfusionMatrix ConfusionMatrix::operator*(double weight)
 {
   ConfusionMatrix result = *this;//make a copy of myself
@@ -105,14 +114,18 @@ void ConfusionMatrix::resize(short nclass){
   m_results.resize(nclass,nclass);
 }
 
-void ConfusionMatrix::setClassNames(const vector<string>& classNames){
+void ConfusionMatrix::setClassNames(const vector<string>& classNames, bool doSort){
   m_classes=classNames;
+  if(doSort)
+    sortClassNames();
   if(m_results.size()!=m_classes.size())
     m_results.resize(m_classes.size(),m_classes.size());
 }
 
-void ConfusionMatrix::pushBackClassName(const string& className){
+void ConfusionMatrix::pushBackClassName(const string& className, bool doSort){
   m_classes.push_back(className);
+  if(doSort)
+    sortClassNames();
   if(m_results.size()!=m_classes.size())
     m_results.resize(m_classes.size(),m_classes.size());
 }  
diff --git a/src/algorithms/ConfusionMatrix.h b/src/algorithms/ConfusionMatrix.h
index 7d0b5a8..e079eb9 100644
--- a/src/algorithms/ConfusionMatrix.h
+++ b/src/algorithms/ConfusionMatrix.h
@@ -23,6 +23,7 @@ along with pktools.  If not, see <http://www.gnu.org/licenses/>.
 #include <sstream>
 #include <vector>
 #include "base/Vector2d.h"
+#include "base/Optionpk.h"
 
 using namespace std;
 
@@ -35,8 +36,8 @@ public:
   ConfusionMatrix& operator=(const ConfusionMatrix& cm);
   short size() const {return m_results.size();};
   void resize(short nclass);
-  void setClassNames(const vector<string>& classNames);
-  void pushBackClassName(const string& className);
+  void setClassNames(const vector<string>& classNames, bool doSort=false);
+  void pushBackClassName(const string& className, bool doSort=false);
   void setResults(const Vector2d<double>& theResults);
   void setResult(const string& theRef, const string& theClass, double theResult);
   void incrementResult(const string& theRef, const string& theClass, double theIncrement);
@@ -69,6 +70,7 @@ public:
   ConfusionMatrix operator+(const ConfusionMatrix &cm){
     return ConfusionMatrix(*this)+=cm;
   }
+  void sortClassNames();
   friend ostream& operator<<(ostream& os, const ConfusionMatrix &cm){
     for(int iclass=0;iclass<cm.nClasses();++iclass)
       os << "\t" << cm.m_classes[iclass];
diff --git a/src/apps/pkclassify_nn.cc b/src/apps/pkclassify_nn.cc
index d67d1d4..04a72d2 100644
--- a/src/apps/pkclassify_nn.cc
+++ b/src/apps/pkclassify_nn.cc
@@ -343,13 +343,14 @@ int main(int argc, char *argv[])
         }
       }
       map<string,Vector2d<float> >::iterator mapit=trainingMap.begin();
+      bool doSort=true;
       while(mapit!=trainingMap.end()){
 	nameVector.push_back(mapit->first);
 	if(classValueMap.size()){
 	  //check if name in training is covered by classname_opt (values can not be 0)
 	  if(classValueMap[mapit->first]>0){
 	    if(cm.getClassIndex(type2string<short>(classValueMap[mapit->first]))<0)
-	      cm.pushBackClassName(type2string<short>(classValueMap[mapit->first]));
+	      cm.pushBackClassName(type2string<short>(classValueMap[mapit->first]),doSort);
 	  }
 	  else{
 	    std::cerr << "Error: names in classname option are not complete, please check names in training vector and make sure classvalue is > 0" << std::endl;
@@ -357,9 +358,14 @@ int main(int argc, char *argv[])
 	  }
 	}
 	else
-	  cm.pushBackClassName(mapit->first);
+	  cm.pushBackClassName(mapit->first,doSort);
 	++mapit;
       }
+      if(priors_opt.size()==nameVector.size()){
+	std::cerr << "Warning: please check if priors are provided in correct order!!!" << std::endl;
+	for(int iclass=0;iclass<nameVector.size();++iclass)
+	  std::cerr << nameVector[iclass] << " " << priors_opt[iclass] << std::endl;
+      }
     }//if(!ibag)
 
     //Calculate features of training set
diff --git a/src/apps/pkclassify_svm.cc b/src/apps/pkclassify_svm.cc
index 05fd0df..88fe43e 100644
--- a/src/apps/pkclassify_svm.cc
+++ b/src/apps/pkclassify_svm.cc
@@ -369,13 +369,14 @@ int main(int argc, char *argv[])
 	}
       }
       map<string,Vector2d<float> >::iterator mapit=trainingMap.begin();
+      bool doSort=true;
       while(mapit!=trainingMap.end()){
 	nameVector.push_back(mapit->first);
 	if(classValueMap.size()){
 	  //check if name in training is covered by classname_opt (values can not be 0)
 	  if(classValueMap[mapit->first]>0){
 	    if(cm.getClassIndex(type2string<short>(classValueMap[mapit->first]))<0)
-	      cm.pushBackClassName(type2string<short>(classValueMap[mapit->first]));
+	      cm.pushBackClassName(type2string<short>(classValueMap[mapit->first]),doSort);
 	  }
 	  else{
 	    std::cerr << "Error: names in classname option are not complete, please check names in training vector and make sure classvalue is > 0" << std::endl;
@@ -383,10 +384,15 @@ int main(int argc, char *argv[])
 	  }
 	}
 	else
-	  cm.pushBackClassName(mapit->first);
+	  cm.pushBackClassName(mapit->first,doSort);
 	++mapit;
       }
-    }
+      if(priors_opt.size()==nameVector.size()){
+	std::cerr << "Warning: please check if priors are provided in correct order!!!" << std::endl;
+	for(int iclass=0;iclass<nameVector.size();++iclass)
+	  std::cerr << nameVector[iclass] << " " << priors_opt[iclass] << std::endl;
+      }
+    }//if(!ibag)
 
     //Calculate features of training set
     vector< Vector2d<float> > trainingFeatures(nclass);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/pktools.git



More information about the Pkg-grass-devel mailing list