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