Changeset 3554
 Timestamp:
 Jan 3, 2017, 8:56:50 AM (6 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/classifier/NCC.h
r2384 r3554 1 #ifndef _theplu_yat_classifier_ncc_ 2 #define _theplu_yat_classifier_ncc_ 1 #ifndef _theplu_yat_classifier_ncc_ 2 #define _theplu_yat_classifier_ncc_ 3 3 4 4 // $Id$ … … 48 48 namespace theplu { 49 49 namespace yat { 50 namespace classifier { 50 namespace classifier { 51 51 52 52 53 53 /** 54 54 \brief Nearest Centroid Classifier 55 55 56 56 A sample is predicted based on its distance to centroids for each 57 57 class. The centroids are generated using training data. NCC 58 58 supports using different measures, for example, Euclidean 59 distance, to define distance between samples and centroids. 59 distance, to define distance between samples and centroids. 60 60 61 61 The template argument Distance should be a class modelling … … 65 65 class NCC : public SupervisedClassifier 66 66 { 67 67 68 68 public: 69 69 /** 70 70 \brief Constructor 71 72 Distance is initialized using its default constructor. 71 72 Distance is initialized using its default constructor. 73 73 */ 74 74 NCC(void); 75 75 76 76 /** 77 77 \brief Constructor using an initialized distance measure … … 97 97 98 98 NCC<Distance>* make_classifier(void) const; 99 99 100 100 /** 101 101 \brief Make predictions for unweighted test data. … … 107 107 results. Weighted distance calculations, in which NaN's have 108 108 zero weights, are used if the centroids contain NaN's. 109 109 110 110 \note NCC returns distances to centroids as the 111 111 prediction. This means that the best class for a sample has the … … 116 116 */ 117 117 void predict(const MatrixLookup& data, utility::Matrix& results) const; 118 118 119 119 /** 120 120 \brief Make predictions for weighted test data. … … 130 130 common. In this case the prediction for the sample is set to 131 131 NaN for the class in \a results. 132 132 133 133 \note NCC returns distances to centroids as the 134 134 prediction. This means that the best class for a sample has the … … 142 142 /** 143 143 \brief Train the NCC using unweighted training data with known 144 targets. 144 targets. 145 145 146 146 A centroid is calculated for each class. For each variable in … … 153 153 /** 154 154 \brief Train the NCC using weighted training data with known 155 targets. 156 157 A centroid is calculated for each class as in 158 train(const MatrixLookup&, const Target&). 155 targets. 156 157 A centroid is calculated for each class as in 158 train(const MatrixLookup&, const Target&). 159 159 The weights of the data are used when calculating the centroids 160 160 and the centroids should be interpreted as unweighted … … 165 165 void train(const MatrixLookupWeighted& data, const Target& targets); 166 166 167 167 168 168 private: 169 169 170 170 void predict_unweighted(const MatrixLookup&, utility::Matrix&) const; 171 void predict_weighted(const MatrixLookupWeighted&, utility::Matrix&) const; 171 void predict_weighted(const MatrixLookupWeighted&, utility::Matrix&) const; 172 172 173 173 utility::Matrix centroids_; 174 174 bool centroids_nan_; 175 175 Distance distance_; 176 }; 176 }; 177 177 178 178 // templates 179 179 180 180 template <typename Distance> 181 NCC<Distance>::NCC() 181 NCC<Distance>::NCC() 182 182 : SupervisedClassifier(), centroids_nan_(false) 183 183 { … … 186 186 187 187 template <typename Distance> 188 NCC<Distance>::NCC(const Distance& dist) 188 NCC<Distance>::NCC(const Distance& dist) 189 189 : SupervisedClassifier(), centroids_nan_(false), distance_(dist) 190 190 { … … 194 194 195 195 template <typename Distance> 196 NCC<Distance>::~NCC() 196 NCC<Distance>::~NCC() 197 197 { 198 198 } … … 204 204 return centroids_; 205 205 } 206 207 208 template <typename Distance> 209 NCC<Distance>* 210 NCC<Distance>::make_classifier() const 211 { 206 207 208 template <typename Distance> 209 NCC<Distance>* 210 NCC<Distance>::make_classifier() const 211 { 212 212 // All private members should be copied here to generate an 213 213 // identical but untrained classifier … … 217 217 template <typename Distance> 218 218 void NCC<Distance>::train(const MatrixLookup& data, const Target& target) 219 { 219 { 220 220 centroids_.resize(data.rows(), target.nof_classes()); 221 221 for(size_t i=0; i<data.rows(); i++) { … … 234 234 template <typename Distance> 235 235 void NCC<Distance>::train(const MatrixLookupWeighted& data, const Target& target) 236 { 236 { 237 237 centroids_.resize(data.rows(), target.nof_classes()); 238 238 for(size_t i=0; i<data.rows(); i++) { 239 239 std::vector<statistics::AveragerWeighted> class_averager; 240 240 class_averager.resize(target.nof_classes()); 241 for(size_t j=0; j<data.columns(); j++) 241 for(size_t j=0; j<data.columns(); j++) 242 242 class_averager[target(j)].add(data.data(i,j),data.weight(i,j)); 243 243 for(size_t c=0;c<target.nof_classes();c++) { … … 252 252 253 253 template <typename Distance> 254 void NCC<Distance>::predict(const MatrixLookup& test, 254 void NCC<Distance>::predict(const MatrixLookup& test, 255 255 utility::Matrix& prediction) const 256 { 256 { 257 257 utility::yat_assert<utility::runtime_error> 258 258 (centroids_.rows()==test.rows(), 259 259 "NCC::predict test data with incorrect number of rows"); 260 260 261 261 prediction.resize(centroids_.columns(), test.columns()); 262 262 263 263 // If weighted training data has resulted in NaN in centroids: weighted calculations 264 if(centroids_nan_) { 264 if(centroids_nan_) { 265 265 predict_weighted(MatrixLookupWeighted(test),prediction); 266 266 } … … 272 272 273 273 template <typename Distance> 274 void NCC<Distance>::predict(const MatrixLookupWeighted& test, 274 void NCC<Distance>::predict(const MatrixLookupWeighted& test, 275 275 utility::Matrix& prediction) const 276 { 276 { 277 277 utility::yat_assert<utility::runtime_error> 278 278 (centroids_.rows()==test.rows(), 279 279 "NCC::predict test data with incorrect number of rows"); 280 280 281 281 prediction.resize(centroids_.columns(), test.columns()); 282 282 predict_weighted(test,prediction); 283 283 } 284 284 285 286 template <typename Distance> 287 void NCC<Distance>::predict_unweighted(const MatrixLookup& test, 285 286 template <typename Distance> 287 void NCC<Distance>::predict_unweighted(const MatrixLookup& test, 288 288 utility::Matrix& prediction) const 289 289 { 290 290 for(size_t j=0; j<test.columns();j++) 291 for(size_t k=0; k<centroids_.columns();k++) 292 prediction(k,j) = distance_(test.begin_column(j), test.end_column(j), 291 for(size_t k=0; k<centroids_.columns();k++) 292 prediction(k,j) = distance_(test.begin_column(j), test.end_column(j), 293 293 centroids_.begin_column(k)); 294 294 } 295 296 template <typename Distance> 297 void NCC<Distance>::predict_weighted(const MatrixLookupWeighted& test, 295 296 template <typename Distance> 297 void NCC<Distance>::predict_weighted(const MatrixLookupWeighted& test, 298 298 utility::Matrix& prediction) const 299 299 { 300 300 utility::MatrixWeighted weighted_centroids(centroids_); 301 for(size_t j=0; j<test.columns();j++) 301 for(size_t j=0; j<test.columns();j++) 302 302 for(size_t k=0; k<centroids_.columns();k++) 303 prediction(k,j) = distance_(test.begin_column(j), test.end_column(j), 303 prediction(k,j) = distance_(test.begin_column(j), test.end_column(j), 304 304 weighted_centroids.begin_column(k)); 305 305 } 306 306 307 307 308 308 }}} // of namespace classifier, yat, and theplu 309 309
Note: See TracChangeset
for help on using the changeset viewer.