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

remove ws

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/KNN.h

    r2384 r3552  
    1 #ifndef _theplu_yat_classifier_knn_ 
    2 #define _theplu_yat_classifier_knn_ 
     1#ifndef _theplu_yat_classifier_knn_
     2#define _theplu_yat_classifier_knn_
    33
    44// $Id$
     
    5353  /**
    5454     \brief Nearest Neighbor Classifier
    55      
     55
    5656     A sample is predicted based on the classes of its k nearest
    5757     neighbors among the training data samples. KNN supports using
     
    6161     uniform vote a test sample gets a vote for each class which is the
    6262     number of nearest neighbors belonging to the class.
    63      
     63
    6464     The template argument Distance should be a class modelling the
    6565     concept \ref concept_distance. The template argument
     
    7070  class KNN : public SupervisedClassifier
    7171  {
    72    
     72
    7373  public:
    7474    /**
    7575       \brief Default constructor.
    76        
     76
    7777       The number of nearest neighbors (k) is set to 3. Distance and
    7878       NeighborWeighting are initialized using their default
     
    8484    /**
    8585       \brief Constructor using an intialized distance measure.
    86        
     86
    8787       The number of nearest neighbors (k) is set to
    8888       3. NeighborWeighting is initialized using its default
     
    9090       parameters and the user wants to specify the parameters by
    9191       initializing Distance prior to constructing the KNN.
    92     */ 
     92    */
    9393    KNN(const Distance&);
    9494
     
    9898    */
    9999    virtual ~KNN();
    100    
    101    
     100
     101
    102102    /**
    103103       \brief Get the number of nearest neighbors.
     
    108108    /**
    109109       \brief Set the number of nearest neighbors.
    110        
    111        Sets the number of neighbors to \a k_in. 
     110
     111       Sets the number of neighbors to \a k_in.
    112112    */
    113113    void k(unsigned int k_in);
     
    115115
    116116    KNN<Distance,NeighborWeighting>* make_classifier(void) const;
    117    
     117
    118118    /**
    119119       \brief Make predictions for unweighted test data.
    120        
     120
    121121       Predictions are calculated and returned in \a results.  For
    122122       each sample in \a data, \a results contains the weighted number
     
    128128    void predict(const MatrixLookup& data , utility::Matrix& results) const;
    129129
    130     /**   
     130    /**
    131131        \brief Make predictions for weighted test data.
    132        
     132
    133133        Predictions are calculated and returned in \a results. For
    134134        each sample in \a data, \a results contains the weighted
     
    140140        this case the distance between the two is set to infinity.
    141141    */
    142     void predict(const MatrixLookupWeighted& data, 
     142    void predict(const MatrixLookupWeighted& data,
    143143                 utility::Matrix& results) const;
    144144
     
    146146    /**
    147147       \brief Train the KNN using unweighted training data with known
    148        targets. 
    149        
     148       targets.
     149
    150150       For KNN there is no actual training; the entire training data
    151151       set is stored with targets. KNN only stores references to \a data
     
    153153       slow. If the number of training samples set is smaller than k,
    154154       k is set to the number of training samples.
    155        
     155
    156156       \note If \a data or \a targets go out of scope ore are
    157157       deleted, the KNN becomes invalid and further use is undefined
     
    159159    */
    160160    void train(const MatrixLookup& data, const Target& targets);
    161    
    162     /**   
    163        \brief Train the KNN using weighted training data with known targets. 
    164    
     161
     162    /**
     163       \brief Train the KNN using weighted training data with known targets.
     164
    165165       See train(const MatrixLookup& data, const Target& targets) for
    166166       additional information.
    167167    */
    168168    void train(const MatrixLookupWeighted& data, const Target& targets);
    169    
     169
    170170  private:
    171    
     171
    172172    const MatrixLookup* data_ml_;
    173173    const MatrixLookupWeighted* data_mlw_;
     
    187187                            utility::Matrix*) const;
    188188
    189     void predict_common(const utility::Matrix& distances, 
     189    void predict_common(const utility::Matrix& distances,
    190190                        utility::Matrix& prediction) const;
    191191
    192192  };
    193  
    194  
     193
     194
    195195  /**
    196196     \brief Concept check for a \ref concept_neighbor_weighting
    197197
    198      This class is intended to be used in a <a 
     198     This class is intended to be used in a <a
    199199     href="\boost_url/concept_check/using_concept_check.htm">
    200200     BOOST_CONCEPT_ASSERT </a>
     
    212212  */
    213213  template <class T>
    214   class NeighborWeightingConcept 
     214  class NeighborWeightingConcept
    215215    : public boost::DefaultConstructible<T>, public boost::Assignable<T>
    216216  {
     
    233233
    234234  // template implementation
    235  
    236   template <typename Distance, typename NeighborWeighting>
    237   KNN<Distance, NeighborWeighting>::KNN() 
     235
     236  template <typename Distance, typename NeighborWeighting>
     237  KNN<Distance, NeighborWeighting>::KNN()
    238238    : SupervisedClassifier(),data_ml_(0),data_mlw_(0),target_(0),k_(3)
    239239  {
     
    243243
    244244  template <typename Distance, typename NeighborWeighting>
    245   KNN<Distance, NeighborWeighting>::KNN(const Distance& dist) 
    246     : SupervisedClassifier(), data_ml_(0), data_mlw_(0), target_(0), k_(3), 
     245  KNN<Distance, NeighborWeighting>::KNN(const Distance& dist)
     246    : SupervisedClassifier(), data_ml_(0), data_mlw_(0), target_(0), k_(3),
    247247      distance_(dist)
    248248  {
     
    251251  }
    252252
    253  
    254   template <typename Distance, typename NeighborWeighting>
    255   KNN<Distance, NeighborWeighting>::~KNN()   
    256   {
    257   }
    258  
     253
     254  template <typename Distance, typename NeighborWeighting>
     255  KNN<Distance, NeighborWeighting>::~KNN()
     256  {
     257  }
     258
    259259
    260260  template <typename Distance, typename NeighborWeighting>
     
    265265    for(size_t i=0; i<training.columns(); i++) {
    266266      for(size_t j=0; j<test.columns(); j++) {
    267         (*distances)(i,j) = distance_(training.begin_column(i), 
    268                                       training.end_column(i), 
     267        (*distances)(i,j) = distance_(training.begin_column(i),
     268                                      training.end_column(i),
    269269                                      test.begin_column(j));
    270270        YAT_ASSERT(!std::isnan((*distances)(i,j)));
     
    273273  }
    274274
    275  
    276   template <typename Distance, typename NeighborWeighting>
    277   void 
     275
     276  template <typename Distance, typename NeighborWeighting>
     277  void
    278278  KNN<Distance, NeighborWeighting>::calculate_weighted
    279279  (const MatrixLookupWeighted& training, const MatrixLookupWeighted& test,
    280280   utility::Matrix* distances) const
    281281  {
    282     for(size_t i=0; i<training.columns(); i++) { 
     282    for(size_t i=0; i<training.columns(); i++) {
    283283      for(size_t j=0; j<test.columns(); j++) {
    284         (*distances)(i,j) = distance_(training.begin_column(i), 
    285                                       training.end_column(i), 
     284        (*distances)(i,j) = distance_(training.begin_column(i),
     285                                      training.end_column(i),
    286286                                      test.begin_column(j));
    287287        // If the distance is NaN (no common variables with non-zero weights),
    288288        // the distance is set to infinity to be sorted as a neighbor at the end
    289         if(std::isnan((*distances)(i,j))) 
     289        if(std::isnan((*distances)(i,j)))
    290290          (*distances)(i,j)=std::numeric_limits<double>::infinity();
    291291      }
    292292    }
    293293  }
    294  
    295  
     294
     295
    296296  template <typename Distance, typename NeighborWeighting>
    297297  unsigned int KNN<Distance, NeighborWeighting>::k() const
     
    308308
    309309  template <typename Distance, typename NeighborWeighting>
    310   KNN<Distance, NeighborWeighting>* 
    311   KNN<Distance, NeighborWeighting>::make_classifier() const 
    312   {     
     310  KNN<Distance, NeighborWeighting>*
     311  KNN<Distance, NeighborWeighting>::make_classifier() const
     312  {
    313313    // All private members should be copied here to generate an
    314314    // identical but untrained classifier
     
    318318    return knn;
    319319  }
    320  
    321  
    322   template <typename Distance, typename NeighborWeighting>
    323   void KNN<Distance, NeighborWeighting>::train(const MatrixLookup& data, 
     320
     321
     322  template <typename Distance, typename NeighborWeighting>
     323  void KNN<Distance, NeighborWeighting>::train(const MatrixLookup& data,
    324324                                               const Target& target)
    325   {   
     325  {
    326326    utility::yat_assert<utility::runtime_error>
    327327      (data.columns()==target.size(),
    328328       "KNN::train called with different sizes of target and data");
    329329    // k has to be at most the number of training samples.
    330     if(data.columns()<k_) 
     330    if(data.columns()<k_)
    331331      k_=data.columns();
    332332    data_ml_=&data;
     
    336336
    337337  template <typename Distance, typename NeighborWeighting>
    338   void KNN<Distance, NeighborWeighting>::train(const MatrixLookupWeighted& data, 
     338  void KNN<Distance, NeighborWeighting>::train(const MatrixLookupWeighted& data,
    339339                                               const Target& target)
    340   {   
     340  {
    341341    utility::yat_assert<utility::runtime_error>
    342342      (data.columns()==target.size(),
    343343       "KNN::train called with different sizes of target and data");
    344344    // k has to be at most the number of training samples.
    345     if(data.columns()<k_) 
     345    if(data.columns()<k_)
    346346      k_=data.columns();
    347347    data_ml_=0;
     
    352352
    353353  template <typename Distance, typename NeighborWeighting>
    354   void 
     354  void
    355355  KNN<Distance, NeighborWeighting>::predict(const MatrixLookup& test,
    356356                                            utility::Matrix& prediction) const
    357   {   
     357  {
    358358    // matrix with training samples as rows and test samples as columns
    359359    utility::Matrix* distances = 0;
     
    373373      distances=new utility::Matrix(data_mlw_->columns(),test.columns());
    374374      calculate_weighted(*data_mlw_,MatrixLookupWeighted(test),
    375                          distances);             
     375                         distances);
    376376    }
    377377    else {
     
    386386
    387387  template <typename Distance, typename NeighborWeighting>
    388   void 
     388  void
    389389  KNN<Distance, NeighborWeighting>::predict(const MatrixLookupWeighted& test,
    390390                                            utility::Matrix& prediction) const
    391   {   
     391  {
    392392    // matrix with training samples as rows and test samples as columns
    393     utility::Matrix* distances=0; 
     393    utility::Matrix* distances=0;
    394394    // unweighted training data
    395     if(data_ml_ && !data_mlw_) { 
     395    if(data_ml_ && !data_mlw_) {
    396396      utility::yat_assert<utility::runtime_error>
    397397        (data_ml_->rows()==test.rows(),
    398          "KNN::predict different number of rows in training and test data");   
     398         "KNN::predict different number of rows in training and test data");
    399399      distances=new utility::Matrix(data_ml_->columns(),test.columns());
    400       calculate_weighted(MatrixLookupWeighted(*data_ml_),test,distances);   
     400      calculate_weighted(MatrixLookupWeighted(*data_ml_),test,distances);
    401401    }
    402402    // weighted training data
     
    404404      utility::yat_assert<utility::runtime_error>
    405405        (data_mlw_->rows()==test.rows(),
    406          "KNN::predict different number of rows in training and test data");   
     406         "KNN::predict different number of rows in training and test data");
    407407      distances=new utility::Matrix(data_mlw_->columns(),test.columns());
    408       calculate_weighted(*data_mlw_,test,distances);             
     408      calculate_weighted(*data_mlw_,test,distances);
    409409    }
    410410    else {
     
    414414    prediction.resize(target_->nof_classes(),test.columns(),0.0);
    415415    predict_common(*distances,prediction);
    416    
     416
    417417    if(distances)
    418418      delete distances;
    419419  }
    420  
     420
    421421  template <typename Distance, typename NeighborWeighting>
    422422  void KNN<Distance, NeighborWeighting>::predict_common
    423423  (const utility::Matrix& distances, utility::Matrix& prediction) const
    424   {   
     424  {
    425425    for(size_t sample=0;sample<distances.columns();sample++) {
    426426      std::vector<size_t> k_index;
     
    430430      weighting_(dist,k_index,*target_,pred);
    431431    }
    432    
     432
    433433    // classes for which there are no training samples should be set
    434434    // to nan in the predictions
    435     for(size_t c=0;c<target_->nof_classes(); c++) 
    436       if(!target_->size(c)) 
     435    for(size_t c=0;c<target_->nof_classes(); c++)
     436      if(!target_->size(c))
    437437        for(size_t j=0;j<prediction.columns();j++)
    438438          prediction(c,j)=std::numeric_limits<double>::quiet_NaN();
Note: See TracChangeset for help on using the changeset viewer.