00001 #ifndef _theplu_yat_classifier_igp_
00002 #define _theplu_yat_classifier_igp_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
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)
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 }}}
00153
00154 #endif