Changeset 3554


Ignore:
Timestamp:
Jan 3, 2017, 8:56:50 AM (7 years ago)
Author:
Peter
Message:

remove ws

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_
    33
    44// $Id$
     
    4848namespace theplu {
    4949namespace yat {
    50 namespace classifier { 
     50namespace classifier {
    5151
    5252
    5353  /**
    5454     \brief Nearest Centroid Classifier
    55      
     55
    5656     A sample is predicted based on its distance to centroids for each
    5757     class. The centroids are generated using training data. NCC
    5858     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.
    6060
    6161     The template argument Distance should be a class modelling
     
    6565  class NCC : public SupervisedClassifier
    6666  {
    67  
     67
    6868  public:
    6969    /**
    7070       \brief Constructor
    71        
    72        Distance is initialized using its default constructor.   
     71
     72       Distance is initialized using its default constructor.
    7373    */
    7474    NCC(void);
    75    
     75
    7676    /**
    7777       \brief Constructor using an initialized distance measure
     
    9797
    9898    NCC<Distance>* make_classifier(void) const;
    99        
     99
    100100    /**
    101101       \brief Make predictions for unweighted test data.
     
    107107       results. Weighted distance calculations, in which NaN's have
    108108       zero weights, are used if the centroids contain NaN's.
    109        
     109
    110110       \note NCC returns distances to centroids as the
    111111       prediction. This means that the best class for a sample has the
     
    116116    */
    117117    void predict(const MatrixLookup& data, utility::Matrix& results) const;
    118    
     118
    119119    /**
    120120       \brief Make predictions for weighted test data.
     
    130130       common. In this case the prediction for the sample is set to
    131131       NaN for the class in \a results.
    132        
     132
    133133       \note NCC returns distances to centroids as the
    134134       prediction. This means that the best class for a sample has the
     
    142142    /**
    143143       \brief Train the NCC using unweighted training data with known
    144        targets. 
     144       targets.
    145145
    146146       A centroid is calculated for each class. For each variable in
     
    153153    /**
    154154       \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&).
    159159       The weights of the data are used when calculating the centroids
    160160       and the centroids should be interpreted as unweighted
     
    165165    void train(const MatrixLookupWeighted& data, const Target& targets);
    166166
    167    
     167
    168168  private:
    169169
    170170    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;
    172172
    173173    utility::Matrix centroids_;
    174174    bool centroids_nan_;
    175175    Distance distance_;
    176   }; 
     176  };
    177177
    178178  // templates
    179179
    180180  template <typename Distance>
    181   NCC<Distance>::NCC() 
     181  NCC<Distance>::NCC()
    182182    : SupervisedClassifier(), centroids_nan_(false)
    183183  {
     
    186186
    187187  template <typename Distance>
    188   NCC<Distance>::NCC(const Distance& dist) 
     188  NCC<Distance>::NCC(const Distance& dist)
    189189    : SupervisedClassifier(), centroids_nan_(false), distance_(dist)
    190190  {
     
    194194
    195195  template <typename Distance>
    196   NCC<Distance>::~NCC()   
     196  NCC<Distance>::~NCC()
    197197  {
    198198  }
     
    204204    return centroids_;
    205205  }
    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  {
    212212    // All private members should be copied here to generate an
    213213    // identical but untrained classifier
     
    217217  template <typename Distance>
    218218  void NCC<Distance>::train(const MatrixLookup& data, const Target& target)
    219   {   
     219  {
    220220    centroids_.resize(data.rows(), target.nof_classes());
    221221    for(size_t i=0; i<data.rows(); i++) {
     
    234234  template <typename Distance>
    235235  void NCC<Distance>::train(const MatrixLookupWeighted& data, const Target& target)
    236   {   
     236  {
    237237    centroids_.resize(data.rows(), target.nof_classes());
    238238    for(size_t i=0; i<data.rows(); i++) {
    239239      std::vector<statistics::AveragerWeighted> class_averager;
    240240      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++)
    242242        class_averager[target(j)].add(data.data(i,j),data.weight(i,j));
    243243      for(size_t c=0;c<target.nof_classes();c++) {
     
    252252
    253253  template <typename Distance>
    254   void NCC<Distance>::predict(const MatrixLookup& test,                     
     254  void NCC<Distance>::predict(const MatrixLookup& test,
    255255                              utility::Matrix& prediction) const
    256   {   
     256  {
    257257    utility::yat_assert<utility::runtime_error>
    258258      (centroids_.rows()==test.rows(),
    259259       "NCC::predict test data with incorrect number of rows");
    260    
     260
    261261    prediction.resize(centroids_.columns(), test.columns());
    262262
    263263    // If weighted training data has resulted in NaN in centroids: weighted calculations
    264     if(centroids_nan_) { 
     264    if(centroids_nan_) {
    265265      predict_weighted(MatrixLookupWeighted(test),prediction);
    266266    }
     
    272272
    273273  template <typename Distance>
    274   void NCC<Distance>::predict(const MatrixLookupWeighted& test,                     
     274  void NCC<Distance>::predict(const MatrixLookupWeighted& test,
    275275                              utility::Matrix& prediction) const
    276   {   
     276  {
    277277    utility::yat_assert<utility::runtime_error>
    278278      (centroids_.rows()==test.rows(),
    279279       "NCC::predict test data with incorrect number of rows");
    280    
     280
    281281    prediction.resize(centroids_.columns(), test.columns());
    282282    predict_weighted(test,prediction);
    283283  }
    284284
    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,
    288288                                         utility::Matrix& prediction) const
    289289  {
    290290    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),
    293293                                    centroids_.begin_column(k));
    294294  }
    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,
    298298                                          utility::Matrix& prediction) const
    299299  {
    300300    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++)
    302302      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),
    304304                                    weighted_centroids.begin_column(k));
    305305  }
    306306
    307      
     307
    308308}}} // of namespace classifier, yat, and theplu
    309309
Note: See TracChangeset for help on using the changeset viewer.