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/Vector.h"
00030 #include "yat/utility/yat_assert.h"
00031
00032 #include <cmath>
00033 #include <limits>
00034 #include <stdexcept>
00035
00036 namespace theplu {
00037 namespace yat {
00038 namespace classifier {
00039
00040 class Target;
00041 class MatrixLookup;
00042
00048 template <typename Distance>
00049 class IGP
00050 {
00051
00052 public:
00057 IGP(const MatrixLookup&, const Target&);
00058
00059
00064 IGP(const MatrixLookup&, const Target&, const Distance&);
00065
00069 virtual ~IGP();
00070
00074 const utility::Vector& score(void) const;
00075
00076
00077 private:
00078 void calculate();
00079
00080 utility::Vector igp_;
00081 Distance distance_;
00082
00083 const MatrixLookup& matrix_;
00084 const Target& target_;
00085 };
00086
00087
00088
00089
00090 template <typename Distance>
00091 IGP<Distance>::IGP(const MatrixLookup& data, const Target& target)
00092 : matrix_(data), target_(target)
00093 {
00094 calculate();
00095 }
00096
00097 template <typename Distance>
00098 IGP<Distance>::IGP(const MatrixLookup& data, const Target& target, const Distance& dist)
00099 : matrix_(data), target_(target), distance_(dist)
00100 {
00101 calculate();
00102 }
00103
00104
00105 template <typename Distance>
00106 IGP<Distance>::~IGP()
00107 {
00108 }
00109
00110 template <typename Distance>
00111 void IGP<Distance>::calculate()
00112 {
00113 YAT_ASSERT(target_.size()==matrix_.columns());
00114
00115
00116 igp_ = utility::Vector(target_.nof_classes());
00117
00118 for(size_t i=0; i<target_.size(); i++) {
00119 size_t neighbor=i;
00120 double mindist=std::numeric_limits<double>::max();
00121 for(size_t j=0; j<target_.size(); j++) {
00122 if (i==j)
00123 continue;
00124 double dist = distance_(matrix_.begin_column(i), matrix_.end_column(i),
00125 matrix_.begin_column(j));
00126 if(dist<mindist) {
00127 mindist=dist;
00128 neighbor=j;
00129 }
00130 }
00131 if(target_(i)==target_(neighbor))
00132 igp_(target_(i))++;
00133
00134 }
00135 for(size_t i=0; i<target_.nof_classes(); i++) {
00136 igp_(i)/=static_cast<double>(target_.size(i));
00137 }
00138 }
00139
00140
00141 template <typename Distance>
00142 const utility::Vector& IGP<Distance>::score(void) const
00143 {
00144 return igp_;
00145 }
00146
00147 }}}
00148
00149 #endif