yat
0.8.3pre
|
00001 #ifndef _theplu_yat_classifier_igp_ 00002 #define _theplu_yat_classifier_igp_ 00003 00004 // $Id: IGP.h 2384 2010-12-22 14:03:36Z peter $ 00005 00006 /* 00007 Copyright (C) 2006 Jari Häkkinen, Markus Ringnér 00008 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér 00009 Copyright (C) 2009, 2010 Peter Johansson 00010 00011 This file is part of the yat library, http://dev.thep.lu.se/yat 00012 00013 The yat library is free software; you can redistribute it and/or 00014 modify it under the terms of the GNU General Public License as 00015 published by the Free Software Foundation; either version 3 of the 00016 License, or (at your option) any later version. 00017 00018 The yat library is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00021 General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 along with yat. If not, see <http://www.gnu.org/licenses/>. 00025 */ 00026 00027 #include "MatrixLookup.h" 00028 #include "Target.h" 00029 #include "yat/utility/concept_check.h" 00030 #include "yat/utility/Vector.h" 00031 #include "yat/utility/yat_assert.h" 00032 00033 #include <boost/concept_check.hpp> 00034 00035 #include <cmath> 00036 #include <limits> 00037 #include <stdexcept> 00038 00039 namespace theplu { 00040 namespace yat { 00041 namespace classifier { 00042 00043 class Target; 00044 class MatrixLookup; 00045 00051 template <typename Distance> 00052 class IGP 00053 { 00054 00055 public: 00060 IGP(const MatrixLookup&, const Target&); 00061 00062 00067 IGP(const MatrixLookup&, const Target&, const Distance&); 00068 00072 virtual ~IGP(); 00073 00077 const utility::Vector& score(void) const; 00078 00079 00080 private: 00081 void calculate(); 00082 00083 utility::Vector igp_; 00084 Distance distance_; 00085 00086 const MatrixLookup& matrix_; 00087 const Target& target_; 00088 }; 00089 00090 00091 // templates 00092 00093 template <typename Distance> 00094 IGP<Distance>::IGP(const MatrixLookup& data, const Target& target) 00095 : matrix_(data), target_(target) 00096 { 00097 BOOST_CONCEPT_ASSERT((utility::DistanceConcept<Distance>)); 00098 calculate(); 00099 } 00100 00101 template <typename Distance> 00102 IGP<Distance>::IGP(const MatrixLookup& data, const Target& target, const Distance& dist) 00103 : matrix_(data), target_(target), distance_(dist) 00104 { 00105 BOOST_CONCEPT_ASSERT((utility::DistanceConcept<Distance>)); 00106 calculate(); 00107 } 00108 00109 00110 template <typename Distance> 00111 IGP<Distance>::~IGP() 00112 { 00113 } 00114 00115 template <typename Distance> 00116 void IGP<Distance>::calculate() 00117 { 00118 YAT_ASSERT(target_.size()==matrix_.columns()); 00119 00120 // Calculate IGP for each class 00121 igp_ = utility::Vector(target_.nof_classes()); 00122 00123 for(size_t i=0; i<target_.size(); i++) { 00124 size_t neighbor=i; 00125 double mindist=std::numeric_limits<double>::max(); 00126 for(size_t j=0; j<target_.size(); j++) { 00127 if (i==j) // avoid self-self comparison 00128 continue; 00129 double dist = distance_(matrix_.begin_column(i), matrix_.end_column(i), 00130 matrix_.begin_column(j)); 00131 if(dist<mindist) { 00132 mindist=dist; 00133 neighbor=j; 00134 } 00135 } 00136 if(target_(i)==target_(neighbor)) 00137 igp_(target_(i))++; 00138 00139 } 00140 for(size_t i=0; i<target_.nof_classes(); i++) { 00141 igp_(i)/=static_cast<double>(target_.size(i)); 00142 } 00143 } 00144 00145 00146 template <typename Distance> 00147 const utility::Vector& IGP<Distance>::score(void) const 00148 { 00149 return igp_; 00150 } 00151 00152 }}} // of namespace classifier, yat, and theplu 00153 00154 #endif