Changeset 628


Ignore:
Timestamp:
Sep 5, 2006, 9:22:30 AM (15 years ago)
Author:
Peter
Message:

fixes #116 and #90 removed classes KernelWeighted_SEV and KernelWeighted_MEV and they're now absorbed into Kernel_SEV and Kernel_MEV, respectively.

Location:
trunk
Files:
4 deleted
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/c++_tools/classifier/Kernel.cc

    r608 r628  
    33#include <c++_tools/classifier/Kernel.h>
    44
     5#include <c++_tools/classifier/DataLookup1D.h>
     6#include <c++_tools/classifier/DataLookupWeighted1D.h>
    57#include <c++_tools/classifier/KernelFunction.h>
    68#include <c++_tools/classifier/MatrixLookup.h>
     
    1214
    1315  Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf)
    14     : data_(&data), kf_(&kf), data_owner_(false), weight_owner_(true)
     16    : data_(&data), data_w_(0), kf_(&kf), data_owner_(false),
     17      weight_owner_(false)
    1518  {
    16     weights_ = new MatrixLookup(data_->rows(),data_->columns(),1.0);
    1719  }
    1820
    1921
    20   Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf,
    21                  const MatrixLookup& weights)
    22     : data_(&data), weights_(&weights), kf_(&kf), data_owner_(false),
     22  Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf)
     23    : data_(&data), data_w_(&data), kf_(&kf), data_owner_(false),
    2324      weight_owner_(false)
    2425  {
     
    2728
    2829  Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index)
    29     : kf_(other.kf_), data_owner_(true), weight_owner_(true)
     30    : kf_(other.kf_), data_owner_(true)
    3031  {
    31     data_ = new MatrixLookup(*other.data_, index, true);
    32     weights_ = new MatrixLookup(*other.weights_, index, true);
     32    // Peter go through this code; look fishy!
     33    data_ = other.data_->selected(index);
     34    if (data_w_){
     35      data_w_ = other.data_w_->selected(index);
     36      weight_owner_=true;
     37    }
     38    else{
     39      data_w_=NULL;
     40      weight_owner_=false;
     41    }
     42
    3343  }
    3444
     
    3747    if (data_owner_)
    3848      delete data_;
     49     
    3950    if (weight_owner_)
    40       delete weights_;
     51      if (data_w_)
     52        delete data_w_;
     53      else
     54        std::cerr << "Error in Kernel implementation: probably a constructor"
     55                  << std::endl;
     56   
     57  }
     58
     59 
     60  double Kernel::element(const DataLookup1D& vec, const size_t i) const
     61  {
     62    if (data_w_)
     63      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false));
     64    else
     65      return kf_->operator()(vec, DataLookup1D(*data_,i, false));
     66  }
     67
     68
     69  double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const
     70  {
     71    if (data_w_)
     72      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false));
     73    else
     74      return kf_->operator()(vec, DataLookup1D(*data_,i, false));
    4175  }
    4276
  • trunk/c++_tools/classifier/Kernel.h

    r616 r628  
    44// $Id$
    55
     6#include <c++_tools/classifier/DataLookup2D.h>
    67#include <c++_tools/classifier/KernelFunction.h>
    7 #include <c++_tools/classifier/MatrixLookup.h>
     8#include <c++_tools/classifier/MatrixLookupWeighted.h>
    89
    910#include <cctype>
     
    1213namespace theplu {
    1314namespace classifier {
     15
     16  class MatrixLookup;
    1417
    1518  ///
     
    4750
    4851    ///
    49     /// Here each data value is accompanied by a weight, and the
    50     /// Kernel matrix is built applying the weighted version of the
    51     /// KernelFunction on each pair of samples (i.e. columns in @a
    52     /// data matrix and @a weight matrix).
     52    /// Constructor taking the @a data matrix (with weights) and
     53    /// KernelFunction as
     54    /// input. Each column in the data matrix corresponds to one
     55    /// sample and the Kernel matrix is built applying the
     56    /// KernelFunction on each pair of columns in the data matrix.
    5357    ///
    54     Kernel(const MatrixLookup& data, const KernelFunction& kf,
    55            const MatrixLookup& weight);
    56    
     58    /// @note Can not handle NaNs.
    5759    ///
    58     /// @todo remove this function
     60    Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf);
     61
     62    ///
     63    /// The new kernel is created using selected features @a
     64    /// index. Kernel will own its underlying data and delete it in
     65    /// destructor.
    5966    ///
    6067    Kernel(const Kernel& kernel, const std::vector<size_t>& index);
     
    7279
    7380    ///
    74     /// @return number columns in Kernel
    75     /// @todo remove this function
    76     inline size_t columns(void) const { return size(); }
    77 
     81    /// @return const reference to the underlying data.
    7882    ///
    79     /// @return const reference to the underlying data
    80     ///
    81     inline const MatrixLookup& data(void) const { return *data_; }
    82 
    83     ///
    84     /// @return number of rows in Kernel
    85     /// @todo remove this function
    86     inline size_t rows(void) const { return size(); }
     83    inline const DataLookup2D& data(void) const { return *data_; }
    8784
    8885    ///
     
    9693    /// matrix.
    9794    ///   
    98     virtual double element(const DataLookup1D& vec, const size_t i) const=0;
     95    double element(const DataLookup1D& vec, const size_t i) const;
    9996
    10097    ///
     
    105102    /// above.
    106103    ///
    107     virtual double element(const DataLookup1D& vec, const DataLookup1D& w,
    108                            const size_t i) const=0;
     104    double element(const DataLookupWeighted1D& vec, const size_t i) const;
    109105
    110     ///
    111     /// @todo remove this function
    112106    ///
    113107    /// Created Kernel is built from selected features in data. The
     
    125119    /// @return true if kernel is calculated using weights
    126120    ///
     121    // Peter make non-virtual?
    127122    virtual bool weighted(void) const=0;
    128123
    129     ///
    130     /// If no weight matrix was used in constructing the Kernel a
    131     /// MatrixLookup with same dimensions as data MatrixLookup and all
    132     /// elements equal to unity will be returned.  @note If Kernel is
    133     /// non-weighted the Kernel is owner of the weight and the weight
    134     /// not well defined after the Kernel has gone out of scope or has
    135     /// been deleted.
    136     ///
    137     /// @return the weight matrix
    138     ///
    139     inline const MatrixLookup& weights(void) const { return *weights_; }
    140 
    141124  protected:
    142     const MatrixLookup* data_;
    143     const MatrixLookup* weights_;
     125    const DataLookup2D* data_;
     126    // Peter can we move data_w_ to weighted daughted classes?
     127    const MatrixLookupWeighted* data_w_;
    144128    const KernelFunction* kf_;
    145129    const bool data_owner_;
    146     const bool weight_owner_;
     130    bool weight_owner_;
    147131
    148132  private:
  • trunk/c++_tools/classifier/KernelLookup.cc

    r608 r628  
    3030    // Checking that each row index is less than kernel.rows()
    3131    assert(row.empty() ||
    32            *(std::max_element(row.begin(),row.end()))<kernel_->rows());
     32           *(std::max_element(row.begin(),row.end()))<kernel_->size());
    3333    // Checking that each column index is less than kernel.column()
    3434    assert(column.empty() ||
    35            *(std::max_element(column.begin(),column.end()))<kernel_->columns());
     35           *(std::max_element(column.begin(),column.end()))<kernel_->size());
    3636
    3737  }
     
    5151    // Checking that no index is out of range
    5252    assert(row_index_.empty() ||
    53            *(max_element(row_index_.begin(), row_index_.end()))<kernel_->rows());
     53           *(max_element(row_index_.begin(), row_index_.end()))<kernel_->size());
    5454    assert(column_index_.empty() ||
    5555           *(max_element(column_index_.begin(), column_index_.end()))<
    56            kernel_->columns());
     56           kernel_->size());
    5757
    5858  }
     
    6666    // Checking that no index is out of range
    6767    assert(row_index_.empty() ||
    68            *(max_element(row_index_.begin(), row_index_.end()))<kernel_->rows());
     68           *(max_element(row_index_.begin(), row_index_.end()))<kernel_->size());
    6969    assert(column_index_.empty() ||
    7070           *(max_element(column_index_.begin(), column_index_.end()))<
    71            kernel_->columns());
     71           kernel_->size());
    7272
    7373  }
  • trunk/c++_tools/classifier/KernelLookup.h

    r608 r628  
    189189    /// in KernelLookup.
    190190    ///
    191     /// @Note Returns a dynamically allocated MatrixLookup2D, which has
    192     /// to be deleted by the caller to avoid memory leaks.
    193     ///
    194     inline const MatrixLookup* data(void) const
    195     { return new MatrixLookup(kernel_->data(),column_index_,false); }
     191    /// @Note Returns a dynamically allocated MatrixLookup, which has
     192    /// to be deleted by the caller to avoid memory leaks.
     193    ///
     194    inline const DataLookup2D* data(void) const
     195    { return kernel_->data().training_data(column_index_); }
    196196
    197197
     
    213213    /// version above.
    214214    ///
    215     inline double element(const DataLookup1D& vec, const DataLookup1D& w,
    216                           const size_t i) const
    217     { return kernel_->element(vec, w, row_index_[i]); }
     215    inline double element(const DataLookupWeighted1D& vec, const size_t i) const
     216    { return kernel_->element(vec, row_index_[i]); }
    218217
    219218    ///
     
    239238    /// to be deleted by the caller to avoid memory leaks.
    240239    ///
    241     inline const MatrixLookup* weights(void) const
    242     { return new MatrixLookup(kernel_->weights(),column_index_, false); }
     240    // Peter remove this
     241    //inline const MatrixLookup* weights(void) const
     242    //{ return new MatrixLookup(kernel_->weights(),column_index_, false); }
    243243
    244244
  • trunk/c++_tools/classifier/Kernel_MEV.cc

    r608 r628  
    44
    55#include <c++_tools/classifier/DataLookup1D.h>
     6#include <c++_tools/classifier/DataLookupWeighted1D.h>
    67
    78namespace theplu {
    89namespace classifier { 
     10
     11  Kernel_MEV::Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf)
     12    : Kernel(data,kf)
     13  {
     14  }
     15
     16
     17  Kernel_MEV::Kernel_MEV(const MatrixLookupWeighted& data,
     18                         const KernelFunction& kf)
     19    : Kernel(data,kf)
     20  {
     21  }
     22
    923
    1024  Kernel_MEV::Kernel_MEV(const Kernel_MEV& kernel,
     
    1731  double Kernel_MEV::operator()(const size_t row, const size_t column) const
    1832  {
    19     return (*kf_)(DataLookup1D(*data_,row,false),
    20                   DataLookup1D(*data_,column,false));
     33    if (data_w_)
     34      return (*kf_)(DataLookupWeighted1D(*data_w_,row,false),
     35                    DataLookupWeighted1D(*data_w_,column,false));
     36    else
     37      return (*kf_)(DataLookup1D(*data_,row,false),
     38                    DataLookup1D(*data_,column,false));
    2139  }
    2240
  • trunk/c++_tools/classifier/Kernel_MEV.h

    r616 r628  
    1010namespace theplu {
    1111namespace classifier {
     12  class MatrixLookup;
     13  class MatrixLookupWeighted;
    1214
    1315  ///
     
    3335    /// sample. @note Can not handle NaNs.
    3436    ///
    35     inline Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf)
    36       : Kernel(data,kf) {}
     37    Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf);
     38
    3739
    3840    ///
    39     /// @todo remove
     41    /// Constructor taking the data matrix and KernelFunction as
     42    /// input.Each column in the data matrix corresponds to one
     43    /// sample. @note Can not handle NaNs.
     44    ///
     45    Kernel_MEV(const MatrixLookupWeighted& data, const KernelFunction& kf);
     46
     47
     48    ///
     49    ///
    4050    ///
    4151    Kernel_MEV(const Kernel_MEV& kernel, const std::vector<size_t>& index);
  • trunk/c++_tools/classifier/Kernel_SEV.cc

    r616 r628  
    44
    55#include <c++_tools/classifier/DataLookup1D.h>
     6#include <c++_tools/classifier/DataLookupWeighted1D.h>
    67#include <c++_tools/classifier/Kernel.h>
    78#include <c++_tools/classifier/KernelFunction.h>
     
    1819    build_kernel();
    1920  }
     21
     22
     23  Kernel_SEV::Kernel_SEV(const MatrixLookupWeighted& data,
     24                         const KernelFunction& kf)
     25    : Kernel(data,kf)
     26  {
     27    kernel_matrix_ = utility::matrix(data_->columns(),data_->columns());
     28    for (size_t i=0; i<kernel_matrix_.rows(); i++)
     29      for (size_t j=i; j<kernel_matrix_.columns(); j++)
     30        kernel_matrix_(i,j) = kernel_matrix_(j,i) =
     31          (*kf_)(DataLookupWeighted1D(data,i,false),
     32                 DataLookupWeighted1D(data,j,false));
     33  }
     34
    2035
    2136  Kernel_SEV::Kernel_SEV(const Kernel_SEV& other,
     
    3752
    3853
    39   double Kernel_SEV::element(const DataLookup1D& vec, const size_t i) const
    40   {
    41     return kf_->operator()(vec, DataLookup1D(*data_,i, false));
    42   }
    43 
    44   double Kernel_SEV::element(const DataLookup1D& vec, const DataLookup1D& w,
    45                              const size_t i) const
    46   {
    47     return (*kf_)(vec, DataLookup1D(*data_,i, false),
    48                   w, DataLookup1D(w.size(),1.0));
    49   }
    50 
    5154  const Kernel* Kernel_SEV::selected(const std::vector<size_t>& index) const
    5255  {
  • trunk/c++_tools/classifier/Kernel_SEV.h

    r616 r628  
    3838   
    3939    ///
    40     /// @todo remove
     40    /// Constructor taking the data matrix and KernelFunction as
     41    /// input. @note Can not handle NaNs. When dealing with missing values,
     42    /// use KernelWeighted_SEV instead.
     43    ///
     44    Kernel_SEV(const MatrixLookupWeighted&, const KernelFunction&);
     45   
     46    ///
     47    ///
    4148    ///
    4249    Kernel_SEV(const Kernel_SEV& kernel, const std::vector<size_t>& index);
  • trunk/c++_tools/classifier/Makefile.am

    r622 r628  
    4040  Kernel_SEV.cc \
    4141  KernelLookup.cc \
    42   KernelWeighted_MEV.cc \
    43   KernelWeighted_SEV.cc \
    4442  MatrixLookup.cc \
    4543  MatrixLookupWeighted.cc \
     
    7371  Kernel_SEV.h \
    7472  KernelLookup.h \
    75   KernelWeighted_MEV.h \
    76   KernelWeighted_SEV.h \
    7773  MatrixLookup.h \
    7874  MatrixLookupWeighted.h \
  • trunk/c++_tools/classifier/SVM.cc

    r626 r628  
    121121  }
    122122
    123   double SVM::predict(const DataLookup1D& x, const DataLookup1D& w) const
     123  double SVM::predict(const DataLookupWeighted1D& x) const
    124124  {
    125125    double y=0;
    126126    for (size_t i=0; i<alpha_.size(); i++)
    127       y += alpha_(i)*target_(i)*kernel_->element(x,w,i);
     127      y += alpha_(i)*target_(i)*kernel_->element(x,i);
    128128
    129129    return margin_*(y+bias_);
  • trunk/c++_tools/classifier/SVM.h

    r625 r628  
    193193    /// corresponding @a weight
    194194    ///
    195     double predict(const DataLookup1D& input, const DataLookup1D& weight) const;
     195    double predict(const DataLookupWeighted1D& input) const;
    196196
    197197    ///
  • trunk/c++_tools/classifier/SubsetGenerator.cc

    r621 r628  
    203203            }
    204204            else {
    205               const MatrixLookup* matrix = kernel->data();
    206               const MatrixLookup* training_matrix =
     205              const DataLookup2D* matrix = kernel->data();
     206              const DataLookup2D* training_matrix =
    207207                matrix->training_data(training_index());
    208               f_selector_->update(*training_matrix, training_target());
     208              if (kernel->weighted()){
     209                const MatrixLookupWeighted& ml =
     210                  dynamic_cast<const MatrixLookupWeighted&>(*training_matrix);
     211                f_selector_->update(ml, training_target());
     212              }
     213              else {
     214                const MatrixLookup& ml =
     215                  dynamic_cast<const MatrixLookup&>(*training_matrix);
     216                f_selector_->update(ml, training_target());
     217              }
     218
    209219              features_.push_back(f_selector_->features());
    210220              kl = kernel->selected(features_.back());
  • trunk/test/kernel_lookup_test.cc

    r616 r628  
    3939  *error << "KernelLookup::KernelLookup(const Kernel&)...";
    4040  classifier::KernelLookup k1(kernel);
    41   if (k1.rows()!=kernel.rows() || k1.columns()!=kernel.columns()) {
     41  if (k1.rows()!=kernel.size() || k1.columns()!=kernel.size()) {
    4242    ok =false;
    4343    *error <<   "ERROR:" << std::endl;
  • trunk/test/kernel_test.cc

    r616 r628  
    1010#include <c++_tools/classifier/Kernel_MEV.h>
    1111#include <c++_tools/classifier/Kernel_SEV.h>
    12 #include <c++_tools/classifier/KernelWeighted_MEV.h>
    13 #include <c++_tools/classifier/KernelWeighted_SEV.h>
    1412
    1513#include <cmath>
     
    121119              std::ostream* error)
    122120{
    123   // at least testing constructors
    124   *error << "testing KernelWeighted_MEV" << std::endl;
    125   classifier::KernelWeighted_MEV kernel_weighted(data,*kf,data);
    126 
    127121  *error << "testing Kernel_MEV" << std::endl;
    128122  classifier::Kernel_MEV kernel(data,*kf);
     
    163157              std::ostream* error)
    164158{
    165   // at least testing constructors
    166   *error << "testing KernelWeighted_SEV" << std::endl;
    167   classifier::KernelWeighted_SEV kernel_weighted(data,*kf,data);
    168 
    169159  *error << "testing Kernel_SEV" << std::endl;
    170160  classifier::Kernel_SEV kernel(data,*kf);
Note: See TracChangeset for help on using the changeset viewer.