Changeset 1173 for trunk/yat/classifier


Ignore:
Timestamp:
Feb 27, 2008, 4:19:05 PM (13 years ago)
Author:
Markus Ringnér
Message:

Minor fixes

File:
1 edited

Legend:

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

    r1164 r1173  
    114114    void predict_weighted(const MatrixLookupWeighted&, utility::Matrix&) const;   
    115115
    116     utility::Matrix* centroids_;
     116    utility::Matrix centroids_;
    117117    bool centroids_nan_;
    118118    Distance distance_;
     
    129129  template <typename Distance>
    130130  NCC<Distance>::NCC()
    131     : SupervisedClassifier(), centroids_(0), centroids_nan_(false)
     131    : SupervisedClassifier(), centroids_nan_(false)
    132132  {
    133133  }
     
    135135  template <typename Distance>
    136136  NCC<Distance>::NCC(const Distance& dist)
    137     : SupervisedClassifier(), centroids_(0), centroids_nan_(false), distance_(dist)
     137    : SupervisedClassifier(), centroids_nan_(false), distance_(dist)
    138138  {
    139139  }
     
    143143  NCC<Distance>::~NCC()   
    144144  {
    145     if(centroids_)
    146       delete centroids_;
    147145  }
    148146
     
    151149  const utility::Matrix& NCC<Distance>::centroids(void) const
    152150  {
    153     return *centroids_;
     151    return centroids_;
    154152  }
    155153 
     
    167165  void NCC<Distance>::train(const MatrixLookup& data, const Target& target)
    168166  {   
    169     if(centroids_)
    170       delete centroids_;
    171     centroids_= new utility::Matrix(data.rows(), target.nof_classes());
     167    centroids_.resize(data.rows(), target.nof_classes());
    172168    for(size_t i=0; i<data.rows(); i++) {
    173169      std::vector<statistics::Averager> class_averager;
     
    177173      }
    178174      for(size_t c=0;c<target.nof_classes();c++) {
    179         (*centroids_)(i,c) = class_averager[c].mean();
     175        centroids_(i,c) = class_averager[c].mean();
    180176      }
    181177    }
     
    186182  void NCC<Distance>::train(const MatrixLookupWeighted& data, const Target& target)
    187183  {   
    188     if(centroids_)
    189       delete centroids_;
    190     centroids_= new utility::Matrix(data.rows(), target.nof_classes());
     184    centroids_.resize(data.rows(), target.nof_classes());
    191185    for(size_t i=0; i<data.rows(); i++) {
    192186      std::vector<statistics::AveragerWeighted> class_averager;
     
    198192          centroids_nan_=true;
    199193        }
    200         (*centroids_)(i,c) = class_averager[c].mean();
     194        centroids_(i,c) = class_averager[c].mean();
    201195      }
    202196    }
     
    209203  {   
    210204    utility::yat_assert<std::runtime_error>
    211       (centroids_,"NCC::predict called for untrained classifier");
    212     utility::yat_assert<std::runtime_error>
    213       (centroids_->rows()==test.rows(),
     205      (centroids_.rows()==test.rows(),
    214206       "NCC::predict test data with incorrect number of rows");
    215207   
    216     prediction.resize(centroids_->columns(), test.columns());
     208    prediction.resize(centroids_.columns(), test.columns());
    217209
    218210    // If weighted training data has resulted in NaN in centroids: weighted calculations
     
    231223  {   
    232224    utility::yat_assert<std::runtime_error>
    233       (centroids_,"NCC::predict called for untrained classifier");
    234     utility::yat_assert<std::runtime_error>
    235       (centroids_->rows()==test.rows(),
     225      (centroids_.rows()==test.rows(),
    236226       "NCC::predict test data with incorrect number of rows");
    237227   
    238     prediction.resize(centroids_->columns(), test.columns());
     228    prediction.resize(centroids_.columns(), test.columns());
    239229    predict_weighted(test,prediction);
    240230  }
     
    245235                                         utility::Matrix& prediction) const
    246236  {
    247     MatrixLookup centroids(*centroids_);
     237    MatrixLookup centroids(centroids_);
    248238    for(size_t j=0; j<test.columns();j++)
    249       for(size_t k=0; k<centroids_->columns();k++)
     239      for(size_t k=0; k<centroids_.columns();k++)
    250240        prediction(k,j) = distance_(test.begin_column(j), test.end_column(j),
    251241                                    centroids.begin_column(k));
     
    256246                                          utility::Matrix& prediction) const
    257247  {
    258     MatrixLookupWeighted weighted_centroids(*centroids_);
     248    MatrixLookupWeighted weighted_centroids(centroids_);
    259249    for(size_t j=0; j<test.columns();j++)
    260       for(size_t k=0; k<centroids_->columns();k++)
     250      for(size_t k=0; k<centroids_.columns();k++)
    261251        prediction(k,j) = distance_(test.begin_column(j), test.end_column(j),
    262252                                    weighted_centroids.begin_column(k));
Note: See TracChangeset for help on using the changeset viewer.