Changeset 925 for trunk/yat/classifier
- Timestamp:
- Oct 2, 2007, 4:02:08 PM (15 years ago)
- Location:
- trunk/yat/classifier
- Files:
-
- 2 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/classifier/IGP.h
r865 r925 25 25 */ 26 26 27 #include "DataLookup1D.h" 28 #include "MatrixLookup.h" 29 #include "Target.h" 27 30 #include "yat/utility/vector.h" 31 #include "yat/utility/yat_assert.h" 32 #include "yat/statistics/vector_distance.h" 33 34 #include <cmath> 35 #include <limits> 28 36 29 37 namespace theplu { 30 38 namespace yat { 31 32 namespace statistics {33 class Distance;34 }35 36 39 namespace classifier { 37 40 … … 43 46 /// See Kapp and Tibshirani, Biostatistics (2006). 44 47 /// 48 template <typename Distance> 45 49 class IGP 46 50 { … … 51 55 /// the distance measure as input. 52 56 /// 53 IGP(const MatrixLookup&, const Target& , const statistics::Distance&);57 IGP(const MatrixLookup&, const Target&); 54 58 55 59 /// … … 61 65 /// @return the IGP score for each class as elements in a vector. 62 66 /// 63 const utility::vector& score(void) const {return igp_;}67 const utility::vector& score(void) const; 64 68 65 69 … … 67 71 utility::vector igp_; 68 72 69 const statistics::Distance& distance_;70 73 const MatrixLookup& matrix_; 71 74 const Target& target_; 72 75 }; 76 77 78 // templates 79 80 template <typename Distance> 81 IGP<Distance>::IGP(const MatrixLookup& data, const Target& target) 82 : matrix_(data), target_(target) 83 { 84 yat_assert(target_.size()==matrix_.columns()); 85 86 // Calculate IGP for each class 87 igp_.clone(utility::vector(target_.nof_classes())); 88 89 for(u_int i=0; i<target_.size(); i++) { 90 u_int neighbor=i; 91 double mindist=std::numeric_limits<double>::max(); 92 const DataLookup1D a(matrix_,i,false); 93 for(u_int j=0; j<target_.size(); j++) { 94 DataLookup1D b(matrix_,j,false); 95 double dist=statistics:: 96 vector_distance(a.begin,a.end(),b.begin(), 97 statistics::vector_distance_traits<Distance>::distace()); 98 if(j!=i && dist<mindist) { 99 mindist=dist; 100 neighbor=j; 101 } 102 } 103 if(target_(i)==target_(neighbor)) 104 igp_(target_(i))++; 105 106 } 107 for(u_int i=0; i<target_.nof_classes(); i++) { 108 igp_(i)/=static_cast<double>(target_.size(i)); 109 } 110 } 111 112 template <typename Distance> 113 IGP<Distance>::~IGP() 114 { 115 } 116 117 118 template <typename Distance> 119 const utility::vector& IGP<Distance>::score(void) const 120 { 121 return igp_; 122 } 73 123 74 124 }}} // of namespace classifier, yat, and theplu -
trunk/yat/classifier/Makefile.am
r901 r925 38 38 FeatureSelectorRandom.cc \ 39 39 GaussianKernelFunction.cc \ 40 IGP.cc \41 40 InputRanker.cc \ 42 41 Kernel.cc \ … … 47 46 MatrixLookupWeighted.cc \ 48 47 NBC.cc \ 49 NCC.cc \50 48 PolynomialKernelFunction.cc \ 51 49 Sampler.cc \ … … 84 82 MatrixLookupWeighted.h \ 85 83 NBC.h \ 86 NCC.h \87 84 PolynomialKernelFunction.h \ 88 85 SVM.h \ -
trunk/yat/classifier/NCC.h
r909 r925 27 27 */ 28 28 29 #include "DataLookup1D.h" 30 #include "DataLookup2D.h" 31 #include "DataLookupWeighted1D.h" 32 #include "MatrixLookup.h" 33 #include "MatrixLookupWeighted.h" 34 #include "SupervisedClassifier.h" 35 #include "Target.h" 36 37 #include "yat/statistics/vector_distance.h" 38 39 #include "yat/utility/Iterator.h" 40 #include "yat/utility/IteratorWeighted.h" 29 41 #include "yat/utility/matrix.h" 30 #include "yat/statistics/vector_distance_ptr.h" 31 #include "SupervisedClassifier.h" 32 42 #include "yat/utility/vector.h" 43 #include "yat/utility/stl_utility.h" 44 #include "yat/utility/yat_assert.h" 45 46 #include<iostream> 47 #include<iterator> 33 48 #include <map> 49 #include <cmath> 50 34 51 35 52 namespace theplu { 36 53 namespace yat { 37 38 namespace utlitity {39 class vector;40 }41 42 54 namespace classifier { 43 55 44 class DataLookup1D;45 class DataLookup2D;46 class MatrixLookup;47 class MatrixLookupWeighted;48 class Target;49 56 50 57 /// … … 52 59 /// 53 60 61 template <typename Distance> 54 62 class NCC : public SupervisedClassifier 55 63 { … … 57 65 public: 58 66 /// 59 /// Constructor taking the training data , the target vector, and60 /// the distance measure tag as input.67 /// Constructor taking the training data and the target vector as 68 /// input 61 69 /// 62 NCC(const MatrixLookup&, const Target&, 63 const statistics::vector_distance_lookup_weighted_ptr); 64 65 /// 66 /// Constructor taking the training data with weights, the target 67 /// vector, the distance measure tag. 70 NCC(const MatrixLookup&, const Target&); 71 72 /// 73 /// Constructor taking the training data with weights and the 74 /// target vector as input. 68 75 /// 69 NCC(const MatrixLookupWeighted&, const Target&, 70 const statistics::vector_distance_lookup_weighted_ptr); 76 NCC(const MatrixLookupWeighted&, const Target&); 71 77 72 78 virtual ~NCC(); … … 98 104 99 105 private: 106 100 107 utility::matrix centroids_; 101 const statistics::vector_distance_lookup_weighted_ptr distance_;102 108 103 109 // data_ has to be of type DataLookup2D to accomodate both … … 112 118 // std::ostream& operator<< (std::ostream&, const NCC&); 113 119 120 121 // templates 122 123 template <typename Distance> 124 NCC<Distance>::NCC(const MatrixLookup& data, const Target& target) 125 : SupervisedClassifier(target), data_(data) 126 { 127 } 128 129 template <typename Distance> 130 NCC<Distance>::NCC(const MatrixLookupWeighted& data, const Target& target) 131 : SupervisedClassifier(target), data_(data) 132 { 133 } 134 135 template <typename Distance> 136 NCC<Distance>::~NCC() 137 { 138 } 139 140 141 template <typename Distance> 142 const utility::matrix& NCC<Distance>::centroids(void) const 143 { 144 return centroids_; 145 } 114 146 147 148 template <typename Distance> 149 const DataLookup2D& NCC<Distance>::data(void) const 150 { 151 return data_; 152 } 153 154 template <typename Distance> 155 SupervisedClassifier* 156 NCC<Distance>::make_classifier(const DataLookup2D& data, const Target& target) const 157 { 158 NCC* ncc=0; 159 if(data.weighted()) { 160 ncc=new NCC<Distance>(dynamic_cast<const MatrixLookupWeighted&>(data), 161 target); 162 } 163 else { 164 ncc=new NCC<Distance>(dynamic_cast<const MatrixLookup&>(data), 165 target); 166 } 167 return ncc; 168 } 169 170 171 template <typename Distance> 172 bool NCC<Distance>::train() 173 { 174 centroids_.clone(utility::matrix(data_.rows(), target_.nof_classes())); 175 utility::matrix nof_in_class(data_.rows(), target_.nof_classes()); 176 const MatrixLookupWeighted* weighted_data = 177 dynamic_cast<const MatrixLookupWeighted*>(&data_); 178 bool weighted = weighted_data; 179 180 for(size_t i=0; i<data_.rows(); i++) { 181 for(size_t j=0; j<data_.columns(); j++) { 182 centroids_(i,target_(j)) += data_(i,j); 183 if (weighted) 184 nof_in_class(i,target_(j))+= weighted_data->weight(i,j); 185 else 186 nof_in_class(i,target_(j))+=1.0; 187 } 188 } 189 centroids_.div(nof_in_class); 190 trained_=true; 191 return trained_; 192 } 193 194 template <typename Distance> 195 void NCC<Distance>::predict(const DataLookup2D& input, 196 utility::matrix& prediction) const 197 { 198 prediction.clone(utility::matrix(centroids_.columns(), input.columns())); 199 200 // Weighted case 201 const MatrixLookupWeighted* testdata = 202 dynamic_cast<const MatrixLookupWeighted*>(&input); 203 if (testdata) { 204 MatrixLookupWeighted weighted_centroids(centroids_); 205 for(size_t j=0; j<input.columns();j++) { 206 DataLookupWeighted1D in(*testdata,j,false); 207 for(size_t k=0; k<centroids_.columns();k++) { 208 DataLookupWeighted1D centroid(weighted_centroids,k,false); 209 210 yat_assert(in.size()==centroid.size()); 211 prediction(k,j)=statistics:: 212 vector_distance(in.begin(),in.end(),centroid.begin(), 213 typename statistics::vector_distance_traits<Distance>::distance()); 214 } 215 } 216 } 217 else { 218 std::string str; 219 str = "Error in NCC<Distance>::predict: DataLookup2D of unexpected class."; 220 throw std::runtime_error(str); 221 } 222 } 223 115 224 }}} // of namespace classifier, yat, and theplu 116 225
Note: See TracChangeset
for help on using the changeset viewer.