Ignore:
Timestamp:
Sep 7, 2007, 1:23:27 AM (16 years ago)
Author:
Peter
Message:

refs #148 in NCC

File:
1 edited

Legend:

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

    r831 r857  
    9191    centroids_.clone(utility::matrix(data_.rows(), target_.nof_classes()));
    9292    utility::matrix nof_in_class(data_.rows(), target_.nof_classes());
     93    const MatrixLookupWeighted* weighted_data =
     94      dynamic_cast<const MatrixLookupWeighted*>(&data_);
     95    bool weighted = weighted_data;
     96
    9397    for(size_t i=0; i<data_.rows(); i++) {
    9498      for(size_t j=0; j<data_.columns(); j++) {
    9599        centroids_(i,target_(j)) += data_(i,j);
    96         try {
    97           nof_in_class(i,target_(j))+=
    98             dynamic_cast<const MatrixLookupWeighted&>(data_).weight(i,j);
    99         }
    100         catch (std::bad_cast) {
     100        if (weighted)
     101          nof_in_class(i,target_(j))+= weighted_data->weight(i,j);
     102        else
    101103          nof_in_class(i,target_(j))+=1.0;
    102         }
    103104      }
    104105    }   
     
    135136  {   
    136137    prediction.clone(utility::matrix(centroids_.columns(), input.columns()));
    137     try {   
    138       const MatrixLookupWeighted& data=
    139         dynamic_cast<const MatrixLookupWeighted&>(input);     
     138    // weighted case
     139    const MatrixLookupWeighted* data =
     140      dynamic_cast<const MatrixLookupWeighted*>(&input); 
     141    if (data) {
    140142      for(size_t j=0; j<input.columns();j++) {     
    141143        DataLookup1D in(input,j,false);
    142144        utility::vector weights(in.size(),0);
    143145        for(size_t i=0; i<in.size();i++)
    144           weights(i)=data.weight(i,j);
     146          weights(i)=data->weight(i,j);
    145147        utility::vector out;
    146148        predict(in,weights,out);
    147149        prediction.column(j,out);
    148150      }
     151      return;
    149152    }
    150     catch (std::bad_cast) {
    151       try {
    152         dynamic_cast<const MatrixLookup&>(input);
    153         for(size_t j=0; j<input.columns();j++) {     
    154           DataLookup1D in(input,j,false);
    155           utility::vector weights(in.size(),1.0);
    156           utility::vector out;
    157           predict(in,weights,out);
    158           prediction.column(j,out);
    159         }
    160       }
    161       catch (std::bad_cast e) {       
    162         std::cerr << "Error in NCC::predict: DataLookup2D of unexpected class. "
    163                   <<  "bad_cast: " << e.what() << std::endl;
    164       }
     153    // non-weighted case
     154    const MatrixLookup* x = dynamic_cast<const MatrixLookup*>(&input);
     155    if (!x){
     156      std::string str;
     157      str = "Error in NCC::predict: DataLookup2D of unexpected class.";
     158      throw std::runtime_error(str);
     159    }
     160    for(size_t j=0; j<input.columns();j++) {     
     161      DataLookup1D in(input,j,false);
     162      utility::vector weights(in.size(),1.0);
     163      utility::vector out;
     164      predict(in,weights,out);
     165      prediction.column(j,out);
    165166    }
    166167  }
Note: See TracChangeset for help on using the changeset viewer.