Changeset 948


Ignore:
Timestamp:
Oct 8, 2007, 4:06:53 PM (16 years ago)
Author:
Markus Ringnér
Message:

Adding support and checks for intended lookups in classifiers

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/knn_test.cc

    r916 r948  
    4646  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
    4747  classifier::KNN<statistics::pearson_vector_distance_tag> knn(dataviewweighted,targets);
    48   *error << "Training KNN" << std::endl;
     48  *error << "training KNN" << std::endl;
    4949  knn.train();
    5050 
    5151  utility::matrix prediction;
    5252  knn.predict(dataviewweighted,prediction);
     53  *error << prediction << std::endl;
    5354 
    5455  if(!ok) {
    55     *error << "vector_distance_test failed" << std::endl;
     56    *error << "knn_test failed" << std::endl;
     57  }
     58  else {
     59    *error << "OK" << std::endl;
    5660  }
    5761  if (error!=&std::cerr)
  • trunk/test/ncc_test.cc

    r931 r948  
    148148    ok=1;
    149149  }
     150  try {
     151    ok=0; // should catch error here
     152    ncc.predict(*dl_kernel,prediction);
     153  }
     154  catch (std::runtime_error) {   
     155    *error << "caught expected bad cast runtime_error" << std::endl;
     156    ok=1;
     157  }
    150158  delete dl_kernel;
    151159 
  • trunk/yat/classifier/KNN.h

    r936 r948  
    55
    66#include "DataLookupWeighted1D.h"
     7#include "MatrixLookup.h"
    78#include "MatrixLookupWeighted.h"
    89#include "SupervisedClassifier.h"
     
    3132  public:
    3233    ///
    33     /// Constructor taking the training data with weights, the target
    34     /// vector, the distance measure, and a weight matrix for the
    35     /// training data as input.
     34    /// Constructor taking the training data and the target   
     35    /// as input.
     36    ///
     37    KNN(const MatrixLookup&, const Target&);
     38
     39
     40    ///
     41    /// Constructor taking the training data with weights and the
     42    /// target as input.
    3643    ///
    3744    KNN(const MatrixLookupWeighted&, const Target&);
     
    93100    utility::matrix* calculate_distances(const DataLookup2D&) const;
    94101  };
    95 
    96   ///
    97   /// The output operator for the KNN class.
    98   ///
    99   //  std::ostream& operator<< (std::ostream&, const KNN&);
    100102 
    101103 
    102104  // templates
    103105 
     106  template <typename Distance>
     107  KNN<Distance>::KNN(const MatrixLookup& data, const Target& target)
     108    : SupervisedClassifier(target), data_(data),k_(3)
     109  {
     110  }
     111
     112
    104113  template <typename Distance>
    105114  KNN<Distance>::KNN(const MatrixLookupWeighted& data, const Target& target)
     
    116125  utility::matrix* KNN<Distance>::calculate_distances(const DataLookup2D& input) const
    117126  {
    118     const MatrixLookupWeighted* weighted_data =
    119       dynamic_cast<const MatrixLookupWeighted*>(&data_);
    120     const MatrixLookupWeighted* weighted_input =
    121       dynamic_cast<const MatrixLookupWeighted*>(&input); 
    122 
    123127    // matrix with training samples as rows and test samples as columns
    124128    utility::matrix* distances =
    125129      new utility::matrix(data_.columns(),input.columns());
    126      
    127     if(weighted_data && weighted_input) {
     130   
     131    // if both training and test are unweighted: unweighted
     132    // calculations are used.
     133    const MatrixLookup* test_unweighted =
     134      dynamic_cast<const MatrixLookup*>(&input);     
     135    if(test_unweighted && !data_.weighted()) {
     136      const MatrixLookup* data_unweighted =
     137        dynamic_cast<const MatrixLookup*>(&data_);     
    128138      for(size_t i=0; i<data_.columns(); i++) {
    129         classifier::DataLookupWeighted1D training(*weighted_data,i,false);
     139        classifier::DataLookup1D training(*data_unweighted,i,false);
    130140        for(size_t j=0; j<input.columns(); j++) {
    131           classifier::DataLookupWeighted1D test(*weighted_input,j,false);
     141          classifier::DataLookup1D test(*test_unweighted,j,false);
    132142          utility::yat_assert<std::runtime_error>(training.size()==test.size());
    133143          (*distances)(i,j) =
     
    138148      }
    139149    }
     150    // if either training or test is weighted: weighted calculations
     151    // are used.
    140152    else {
    141       std::string str;
    142       str = "Error in KNN::calculate_distances: Only MatrixLookupWeighted supported still.";
    143       throw std::runtime_error(str);
     153      const MatrixLookupWeighted* data_weighted =
     154        dynamic_cast<const MatrixLookupWeighted*>(&data_);
     155      const MatrixLookupWeighted* test_weighted =
     156        dynamic_cast<const MatrixLookupWeighted*>(&input);               
     157      if(data_weighted && test_weighted) {
     158        for(size_t i=0; i<data_.columns(); i++) {
     159          classifier::DataLookupWeighted1D training(*data_weighted,i,false);
     160          for(size_t j=0; j<input.columns(); j++) {
     161            classifier::DataLookupWeighted1D test(*test_weighted,j,false);
     162            utility::yat_assert<std::runtime_error>(training.size()==test.size());
     163            (*distances)(i,j) =
     164              statistics::vector_distance(training.begin(),training.end(),
     165                                          test.begin(), typename statistics::vector_distance_traits<Distance>::distance());
     166            utility::yat_assert<std::runtime_error>(!std::isnan((*distances)(i,j)));
     167          }
     168        }
     169      }
     170      else {
     171        std::string str;
     172        str = "Error in KNN::calculate_distances: Only support when training and test data both are either MatrixLookup or MatrixLookupWeighted";
     173        throw std::runtime_error(str);
     174      }
    144175    }
    145176    return distances;
     
    176207                              target);
    177208      } 
     209      else {
     210        knn=new KNN<Distance>(dynamic_cast<const MatrixLookup&>(data),
     211                              target);
     212      }
    178213      knn->k(this->k());
    179214    }
     
    210245      }
    211246    }
     247    prediction*=(1.0/k_);
    212248    delete distances;
    213249  }
  • trunk/yat/classifier/NBC.cc

    r865 r948  
    6363  NBC::make_classifier(const DataLookup2D& data, const Target& target) const
    6464  {     
    65     NBC* ncc=0;
    66     if(data.weighted()) {
    67       ncc=new NBC(dynamic_cast<const MatrixLookupWeighted&>(data),target);
     65    NBC* nbc=0;
     66    try {
     67      if(data.weighted()) {
     68        nbc=new NBC(dynamic_cast<const MatrixLookupWeighted&>(data),target);
     69      }
     70      else {
     71        nbc=new NBC(dynamic_cast<const MatrixLookup&>(data),target);
     72      }     
    6873    }
    69     else {
    70       ncc=new NBC(dynamic_cast<const MatrixLookup&>(data),target);
     74    catch (std::bad_cast) {
     75      std::string str = "Error in NBC::make_classifier: DataLookup2D of unexpected class.";
     76      throw std::runtime_error(str);
    7177    }
    72     return ncc;
     78    return nbc;
    7379  }
    7480
  • trunk/yat/classifier/NCC.h

    r936 r948  
    246246      const MatrixLookupWeighted* test_weighted =
    247247        dynamic_cast<const MatrixLookupWeighted*>(&input);     
    248       MatrixLookupWeighted weighted_centroids(*centroids_);
    249248      if(test_weighted) {
     249        MatrixLookupWeighted weighted_centroids(*centroids_);
    250250        for(size_t j=0; j<input.columns();j++) {       
    251251          DataLookupWeighted1D in(*test_weighted,j,false);
     
    260260      }
    261261      else if(data_.weighted() && test_unweighted) {
    262         //        MatrixLookupWeighted test2weighted(*test_unweighted);
    263         // Need to convert MatrixLookup to MatrixLookupWeighted here
    264         // and use it in the code below
    265         for(size_t j=0; j<input.columns();j++) {       
    266           DataLookupWeighted1D in(*test_weighted,j,false);
    267           for(size_t k=0; k<centroids_->columns();k++) {
    268             DataLookupWeighted1D centroid(weighted_centroids,k,false);
    269             utility::yat_assert<std::runtime_error>(in.size()==centroid.size());
    270             prediction(k,j)=statistics::
    271               vector_distance(in.begin(),in.end(),centroid.begin(),
    272                               typename statistics::vector_distance_traits<Distance>::distance());
    273           }
    274         }
     262        std::string str =  "Error in NCC<Distance>::predict:";
     263        str += " predicting unweighted data when NCC";
     264        str += " is trained on weighted data is not yet supported";
     265        throw std::runtime_error(str);       
    275266      }
    276267      else {
    277         std::string str;
    278         str = "Error in NCC<Distance>::predict: DataLookup2D of unexpected class.";
     268        std::string str =
     269          "Error in NCC<Distance>::predict: DataLookup2D of unexpected class.";
    279270        throw std::runtime_error(str);
    280271      }
Note: See TracChangeset for help on using the changeset viewer.