source: trunk/yat/classifier/NCC.h @ 1124

Last change on this file since 1124 was 1124, checked in by Peter, 14 years ago

train returns nothing, removed docs saying else

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 8.3 KB
Line 
1#ifndef _theplu_yat_classifier_ncc_
2#define _theplu_yat_classifier_ncc_
3
4// $Id$
5
6/*
7  Copyright (C) 2005 Markus Ringnér, Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
9  Copyright (C) 2007 Peter Johansson
10
11  This file is part of the yat library, http://trac.thep.lu.se/yat
12
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 2 of the
16  License, or (at your option) any later version.
17
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26  02111-1307, USA.
27*/
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/Averager.h"
38#include "yat/statistics/AveragerWeighted.h"
39#include "yat/utility/Matrix.h"
40#include "yat/utility/Vector.h"
41#include "yat/utility/stl_utility.h"
42#include "yat/utility/yat_assert.h"
43
44#include<iostream>
45#include<iterator>
46#include <map>
47#include <cmath>
48#include <stdexcept>
49
50namespace theplu {
51namespace yat {
52namespace classifier { 
53
54
55  ///
56  /// @brief Class for Nearest Centroid Classification.
57  ///
58  /// The template argument Distance should be a class modelling
59  /// the concept \ref concept_distance.
60  ///
61  template <typename Distance>
62  class NCC : public SupervisedClassifier
63  {
64 
65  public:
66    ///
67    /// Constructor taking the training data and the target vector as
68    /// input
69    ///
70    NCC(const MatrixLookup&, const Target&);
71   
72    ///
73    /// Constructor taking the training data with weights and the
74    /// target vector as input.
75    ///
76    NCC(const MatrixLookupWeighted&, const Target&);
77
78    virtual ~NCC();
79
80    ///
81    /// @return the centroids for each class as columns in a matrix.
82    ///
83    const utility::Matrix& centroids(void) const;
84
85    const DataLookup2D& data(void) const;
86
87    SupervisedClassifier* make_classifier(const DataLookup2D&, 
88                                          const Target&) const;
89   
90    ///
91    /// Train the classifier using the training data. Centroids are
92    /// calculated for each class.
93    ///
94    void train();
95
96   
97    ///
98    /// Calculate the distance to each centroid for test samples
99    ///
100    void predict(const DataLookup2D&, utility::Matrix&) const;
101   
102   
103  private:
104
105    void predict_unweighted(const MatrixLookup&, utility::Matrix&) const;
106    void predict_weighted(const MatrixLookupWeighted&, utility::Matrix&) const;   
107
108    utility::Matrix* centroids_;
109    bool centroids_nan_;
110    Distance distance_;
111
112    // data_ has to be of type DataLookup2D to accomodate both
113    // MatrixLookup and MatrixLookupWeighted
114    const DataLookup2D& data_;
115  };
116
117  ///
118  /// The output operator for the NCC class.
119  ///
120  //  std::ostream& operator<< (std::ostream&, const NCC&);
121 
122
123  // templates
124
125  template <typename Distance>
126  NCC<Distance>::NCC(const MatrixLookup& data, const Target& target) 
127    : SupervisedClassifier(target), centroids_(0), centroids_nan_(false), data_(data) 
128  {
129  }
130
131  template <typename Distance>
132  NCC<Distance>::NCC(const MatrixLookupWeighted& data, const Target& target)
133    : SupervisedClassifier(target), centroids_(0), centroids_nan_(false), data_(data)
134  {
135  }
136
137  template <typename Distance>
138  NCC<Distance>::~NCC()   
139  {
140    if(centroids_)
141      delete centroids_;
142  }
143
144  template <typename Distance>
145  const utility::Matrix& NCC<Distance>::centroids(void) const
146  {
147    return *centroids_;
148  }
149 
150
151  template <typename Distance>
152  const DataLookup2D& NCC<Distance>::data(void) const
153  {
154    return data_;
155  }
156 
157  template <typename Distance>
158  SupervisedClassifier* 
159  NCC<Distance>::make_classifier(const DataLookup2D& data, const Target& target) const 
160  {     
161    NCC* ncc=0;
162    try {
163      if(data.weighted()) {
164        ncc=new NCC<Distance>(dynamic_cast<const MatrixLookupWeighted&>(data),
165                              target);
166      }
167      else {
168        ncc=new NCC<Distance>(dynamic_cast<const MatrixLookup&>(data),
169                              target);
170      }
171    }
172    catch (std::bad_cast) {
173      std::string str = "Error in NCC<Distance>::make_classifier: DataLookup2D of unexpected class.";
174      throw std::runtime_error(str);
175    }
176    return ncc;
177  }
178
179
180  template <typename Distance>
181  void NCC<Distance>::train()
182  {   
183    if(centroids_) 
184      delete centroids_;
185    centroids_= new utility::Matrix(data_.rows(), target_.nof_classes());
186    // data_ is a MatrixLookup or a MatrixLookupWeighted
187    if(data_.weighted()) {
188      const MatrixLookupWeighted* weighted_data = 
189        dynamic_cast<const MatrixLookupWeighted*>(&data_);     
190      for(size_t i=0; i<data_.rows(); i++) {
191        std::vector<statistics::AveragerWeighted> class_averager;
192        class_averager.resize(target_.nof_classes());
193        for(size_t j=0; j<data_.columns(); j++) {
194          class_averager[target_(j)].add(weighted_data->data(i,j),
195                                         weighted_data->weight(i,j));
196        }
197        for(size_t c=0;c<target_.nof_classes();c++) {
198          if(class_averager[c].sum_w()==0) {
199            centroids_nan_=true;
200            (*centroids_)(i,c) = std::numeric_limits<double>::quiet_NaN();
201          }
202          else {
203            (*centroids_)(i,c) = class_averager[c].mean();
204          }
205        }
206      }
207    }
208    else {
209      const MatrixLookup* unweighted_data = 
210        dynamic_cast<const MatrixLookup*>(&data_);     
211      for(size_t i=0; i<data_.rows(); i++) {
212        std::vector<statistics::Averager> class_averager;
213        class_averager.resize(target_.nof_classes());
214        for(size_t j=0; j<data_.columns(); j++) {
215          class_averager[target_(j)].add((*unweighted_data)(i,j));
216        }
217        for(size_t c=0;c<target_.nof_classes();c++) {
218          (*centroids_)(i,c) = class_averager[c].mean();
219        }
220      }
221    }
222  }
223
224  template <typename Distance>
225  void NCC<Distance>::predict(const DataLookup2D& test,                     
226                              utility::Matrix& prediction) const
227  {   
228    utility::yat_assert<std::runtime_error>
229      (centroids_,"NCC::predict called for untrained classifier");
230    utility::yat_assert<std::runtime_error>
231      (data_.rows()==test.rows(),
232       "NCC::predict test data with incorrect number of rows");
233   
234    prediction.resize(centroids_->columns(), test.columns());
235
236    // unweighted test data
237    if (const MatrixLookup* test_unweighted =
238        dynamic_cast<const MatrixLookup*>(&test)) {
239      // If weighted training data has resulted in NaN in centroids: weighted calculations
240      if(centroids_nan_) { 
241        predict_weighted(MatrixLookupWeighted(*test_unweighted),prediction);
242      }
243      // If unweighted training data: unweighted calculations
244      else {
245        predict_unweighted(*test_unweighted,prediction);
246      }
247    }
248    // weighted test data: weighted calculations
249    else if (const MatrixLookupWeighted* test_weighted =
250             dynamic_cast<const MatrixLookupWeighted*>(&test)) { 
251      predict_weighted(*test_weighted,prediction);
252    }
253    else {
254      std::string str = 
255        "Error in NCC<Distance>::predict: DataLookup2D of unexpected class.";
256      throw std::runtime_error(str);
257    }
258  }
259 
260  template <typename Distance>
261  void NCC<Distance>::predict_unweighted(const MatrixLookup& test, 
262                                         utility::Matrix& prediction) const
263  {
264    MatrixLookup unweighted_centroids(*centroids_);
265    for(size_t j=0; j<test.columns();j++) {       
266      DataLookup1D in(test,j,false);
267      for(size_t k=0; k<centroids_->columns();k++) {
268        DataLookup1D centroid(unweighted_centroids,k,false);           
269        utility::yat_assert<std::runtime_error>(in.size()==centroid.size());
270        prediction(k,j) = distance_(in.begin(), in.end(), centroid.begin());
271      }
272    }
273  }
274
275  template <typename Distance>
276  void NCC<Distance>::predict_weighted(const MatrixLookupWeighted& test, 
277                                          utility::Matrix& prediction) const
278  {
279    MatrixLookupWeighted weighted_centroids(*centroids_);
280    for(size_t j=0; j<test.columns();j++) {       
281      DataLookupWeighted1D in(test,j,false);
282      for(size_t k=0; k<centroids_->columns();k++) {
283        DataLookupWeighted1D centroid(weighted_centroids,k,false);
284        utility::yat_assert<std::runtime_error>(in.size()==centroid.size());
285        prediction(k,j) = distance_(in.begin(), in.end(), centroid.begin());
286      }
287    }
288  }
289
290     
291}}} // of namespace classifier, yat, and theplu
292
293#endif
Note: See TracBrowser for help on using the repository browser.