Changeset 545


Ignore:
Timestamp:
Mar 6, 2006, 2:35:45 PM (16 years ago)
Author:
Peter
Message:

added feature selection for SVM

Location:
trunk/lib/classifier
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/classifier/DataLookup2D.h

    r537 r545  
    2222    /// Default constructor.
    2323    ///
    24     inline DataLookup2D(void) : owner_(false){};
     24    inline DataLookup2D(const bool owner=false) : owner_(owner){};
    2525
    2626
  • trunk/lib/classifier/Kernel.h

    r542 r545  
    1010
    1111#include <cctype>
     12#include <vector>
    1213
    1314namespace theplu {
     
    3738    ///   @note Can not handle NaNs.
    3839    ///
    39     Kernel(const MatrixLookup& data, const KernelFunction& kf)
    40       : data_(data), kf_(&kf) {};
     40    Kernel(const MatrixLookup& data, const KernelFunction& kf);
     41
     42    Kernel(const MatrixLookup& data, const KernelFunction& kf,
     43           const MatrixLookup& weight);
    4144   
     45    ///
     46    /// @todo doc
     47    ///
     48    Kernel(const Kernel& kernel, const std::vector<size_t>& index);
     49
    4250    ///
    4351    ///   Destructor
    4452    ///
    45     virtual ~Kernel(void) {};
     53    virtual ~Kernel(void);
    4654
    4755    ///
     
    5664    inline size_t columns(void) const { return size(); }
    5765
     66    inline const MatrixLookup& data(void) const { return *data_; }
     67
    5868    ///
    5969    /// @return number of rows in Kernel
     
    6474    /// @brief number of samples
    6575    ///
    66     inline size_t size(void) const { return data_.columns(); }
     76    inline size_t size(void) const { return data_->columns(); }
    6777
    6878   
     
    7181                           const size_t i) const=0;
    7282
     83    ///
     84    /// Created Kernel is built from selected features in data. The
     85    /// @a index corresponds to which rows in data to use for the
     86    /// calculation of the returned Kernel.
     87    ///
     88    /// @return Dynamically allocated Kernel based on selected features
     89    ///
     90    /// @Note Returns a dynamically allocated Kernel, which has
     91    /// to be deleted by the caller to avoid memory leaks.
     92    ///
     93    virtual const Kernel* selected(const std::vector<size_t>& index) const=0;
     94
     95    ///
     96    /// @return true if kernel is calculated using weights
     97    ///
     98    virtual bool weighted(void) const=0;
     99
     100    inline const MatrixLookup& weights(void) const { return *weights_; }
     101
    73102  protected:
    74     // Peter should be a copy
    75     const MatrixLookup& data_;
     103    const MatrixLookup* data_;
     104    const MatrixLookup* weights_;
    76105    const KernelFunction* kf_;
     106    const bool data_owner_;
     107    const bool weight_owner_;
    77108
    78109  private:
  • trunk/lib/classifier/KernelLookup.cc

    r537 r545  
    1313namespace classifier { 
    1414
    15   KernelLookup::KernelLookup(const Kernel& kernel)
    16     : DataLookup2D(), kernel_(&kernel)
     15  KernelLookup::KernelLookup(const Kernel& kernel, const bool own)
     16    : DataLookup2D(own), kernel_(&kernel)
    1717  {
    1818    column_index_.reserve(kernel.size());
     
    7171  }
    7272
     73  const MatrixLookup* KernelLookup::weights(void) const
     74  { return new MatrixLookup(kernel_->weights(),column_index_); }
     75
     76  const KernelLookup*
     77  KernelLookup::selected(const std::vector<size_t>& index) const
     78  {
     79    const Kernel* kernel = kernel_->selected(index);
     80    return new KernelLookup(*kernel, true);
     81  }
    7382
    7483}} // of namespace classifier and namespace theplu
  • trunk/lib/classifier/KernelLookup.h

    r542 r545  
    2222   
    2323    ///
    24     /// Constructor
     24    /// @brief Constructor from a Kernel
     25    ///
     26    /// @parameter own if true @a kernel is deleted in destructor,
     27    /// i.e., it must be dynamically allocated.
    2528    ///
    2629    /// @note If underlying Kernel goes out of scope or is deleted, the
     
    2831    /// undefined.
    2932    ///
    30     KernelLookup(const Kernel& kernel);
     33    KernelLookup(const Kernel& kernel, const bool own=false);
    3134
    3235    ///
     
    102105    { return (*kernel_)(row_index_[row],column_index_[column]); }
    103106
     107    ///
     108    /// Each column in returned MatrixLook corresponds to the column
     109    /// in KernelLookup.
     110    ///
     111    /// @Note Returns a dynamically allocated MatrixLookup2D, which has
     112    /// to be deleted by the caller to avoid memory leaks.
     113    ///
     114    inline const MatrixLookup* data(void) const
     115    { return new MatrixLookup(kernel_->data(),column_index_); }
     116
     117
     118    ///
     119    /// @todo doc
     120    ///
    104121    inline double element(const DataLookup1D& vec, const size_t i) const
    105122    { return kernel_->element(vec, row_index_[i]); }
    106123
     124    ///
     125    /// @todo doc
     126    ///
    107127    inline double element(const DataLookup1D& vec, const DataLookup1D& w,
    108128                          const size_t i) const
    109129    { return kernel_->element(vec, w, row_index_[i]); }
     130
     131    ///
     132    /// @todo doc
     133    ///
     134    /// @Note Returns a dynamically allocated KernelLookup, which has
     135    /// to be deleted by the caller to avoid memory leaks.
     136    ///
     137    const KernelLookup* selected(const std::vector<size_t>&) const;
     138   
     139    ///
     140    /// @return true if underlying Kernel is weighted
     141    ///
     142    inline bool weighted(void) const { return kernel_->weighted(); }
     143
     144    ///
     145    /// @todo doc
     146    ///
     147    /// @Note Returns a dynamically allocated KernelLookup, which has
     148    /// to be deleted by the caller to avoid memory leaks.
     149    ///
     150    const MatrixLookup* weights(void) const;
     151
    110152
    111153  private:
  • trunk/lib/classifier/KernelWeighted_MEV.cc

    r528 r545  
    1212                                        const size_t column) const
    1313      {
    14         return (*kf_)(DataLookup1D(data_,row,false),
    15                       DataLookup1D(data_,column,false),
    16                       DataLookup1D(weights_,row,false),
    17                       DataLookup1D(weights_,column,false));
     14        return (*kf_)(DataLookup1D(*data_,row,false),
     15                      DataLookup1D(*data_,column,false),
     16                      DataLookup1D(*weights_,row,false),
     17                      DataLookup1D(*weights_,column,false));
    1818      }
     19
     20  KernelWeighted_MEV::KernelWeighted_MEV(const MatrixLookup& data,
     21                                         const KernelFunction& kf,
     22                                         const MatrixLookup& weights)
     23    : Kernel(data,kf, weights)
     24  {
     25  }
     26
     27
     28  KernelWeighted_MEV::KernelWeighted_MEV(const KernelWeighted_MEV& other,
     29                                         const std::vector<size_t>& index)
     30    : Kernel(other, index)
     31  {
     32  }
     33
     34
     35  const Kernel*
     36  KernelWeighted_MEV::selected(const std::vector<size_t>& index) const
     37  {
     38    return new KernelWeighted_MEV(*this, index);
     39  }
    1940
    2041
  • trunk/lib/classifier/KernelWeighted_MEV.h

    r542 r545  
    3838    /// behaviour of the object is undefined
    3939    ///
    40     inline KernelWeighted_MEV(const MatrixLookup& data,
    41                               const KernelFunction& kf,
    42                               const MatrixLookup& weights)
    43     : Kernel(data,kf), weights_(weights) {}
     40    KernelWeighted_MEV(const MatrixLookup& data,
     41                       const KernelFunction& kf,
     42                       const MatrixLookup& weights);
     43
     44    ///
     45    /// @todo doc
     46    ///
     47    KernelWeighted_MEV(const KernelWeighted_MEV& other,
     48                       const std::vector<size_t>& index);
    4449
    4550    ///
     
    5459    inline double element(const DataLookup1D& vec, const size_t i) const
    5560    {
    56       return (*kf_)(vec, DataLookup1D(data_,i),
     61      return (*kf_)(vec, DataLookup1D(*data_,i),
    5762                    DataLookup1D(vec.size(),1.0),
    58                     DataLookup1D(weights_,i));
     63                    DataLookup1D(*weights_,i));
    5964    }
    6065
     66    ///
     67    /// @todo doc
     68    ///
    6169    inline double element(const DataLookup1D& vec, const DataLookup1D& w,
    6270                          const size_t i) const
    6371    {
    64       return (*kf_)(vec, DataLookup1D(data_,i),w,DataLookup1D(weights_,i));
     72      return (*kf_)(vec, DataLookup1D(*data_,i),w,DataLookup1D(*weights_,i));
    6573    }
     74
     75    ///
     76    /// @todo doc
     77    ///
     78    const Kernel* selected(const std::vector<size_t>& index) const;
     79
     80    inline bool weighted(void) const { return true; }
    6681
    6782  private:
     
    7186    KernelWeighted_MEV(const KernelWeighted_MEV&);
    7287
    73     const MatrixLookup& weights_;
    7488
    7589  };
  • trunk/lib/classifier/KernelWeighted_SEV.cc

    r527 r545  
    1414
    1515
    16 KernelWeighted_SEV::KernelWeighted_SEV(const MatrixLookup& data,
    17                                        const KernelFunction& kf,
    18                                        const MatrixLookup& weights)
    19   : Kernel(data,kf), weights_(weights)
    20 {
    21   kernel_matrix_ = gslapi::matrix(data.columns(),data.columns());
    22   for (size_t i=0; i<kernel_matrix_.rows(); i++)
    23     for (size_t j=i; j<kernel_matrix_.columns(); j++)
    24       kernel_matrix_(i,j) = kernel_matrix_(j,i) =
    25         (*kf_)(DataLookup1D(data_,i,false),
    26                DataLookup1D(data_,j,false),
    27                DataLookup1D(weights_,i,false),
    28                DataLookup1D(weights_,j,false));
    29 }
     16  KernelWeighted_SEV::KernelWeighted_SEV(const MatrixLookup& data,
     17                                         const KernelFunction& kf,
     18                                         const MatrixLookup& weights)
     19    : Kernel(data,kf,weights)
     20  {
     21    kernel_matrix_ = gslapi::matrix(data.columns(),data.columns());
     22    for (size_t i=0; i<kernel_matrix_.rows(); i++)
     23      for (size_t j=i; j<kernel_matrix_.columns(); j++)
     24        kernel_matrix_(i,j) = kernel_matrix_(j,i) =
     25          (*kf_)(DataLookup1D(*data_,i,false),
     26                 DataLookup1D(*data_,j,false),
     27                 DataLookup1D(*weights_,i,false),
     28                 DataLookup1D(*weights_,j,false));
     29  }
     30 
     31
     32  KernelWeighted_SEV::KernelWeighted_SEV(const KernelWeighted_SEV& other,
     33                                         const std::vector<size_t>& index)
     34    : Kernel(other, index)
     35  {
     36  }
     37 
     38
     39  const KernelWeighted_SEV*
     40  KernelWeighted_SEV::selected(const std::vector<size_t>& index) const
     41  {
     42    return new KernelWeighted_SEV(*this, index);
     43  }
     44
    3045
    3146
  • trunk/lib/classifier/KernelWeighted_SEV.h

    r542 r545  
    4343                       const MatrixLookup& weights);
    4444
     45    ///
     46    /// @todo doc
     47    ///
     48    KernelWeighted_SEV(const KernelWeighted_SEV& other,
     49                       const std::vector<size_t>& index);
    4550   
    4651    ///
     
    5661    inline double element(const DataLookup1D& vec, const size_t i) const
    5762    {
    58       return (*kf_)(vec, DataLookup1D(data_,i),
     63      return (*kf_)(vec, DataLookup1D(*data_,i),
    5964                    DataLookup1D(vec.size(),1.0),
    60                     DataLookup1D(weights_,i));
     65                    DataLookup1D(*weights_,i));
    6166    }
    6267
     
    6469                          const size_t i) const
    6570    {
    66       return (*kf_)(vec, DataLookup1D(data_,i),w,DataLookup1D(weights_,i));
     71      return (*kf_)(vec, DataLookup1D(*data_,i),w,DataLookup1D(*weights_,i));
    6772    }
     73
     74    ///
     75    /// @todo doc
     76    ///
     77    const KernelWeighted_SEV* selected(const std::vector<size_t>& index) const;
     78
     79    ///
     80    /// @return true
     81    ///
     82    inline bool weighted(void) const { return true; }
    6883
    6984  private:
     
    7287
    7388    gslapi::matrix kernel_matrix_;
    74     const MatrixLookup& weights_;
    7589
    7690  }; // class Kernel_SEV
  • trunk/lib/classifier/Kernel_MEV.cc

    r542 r545  
    88namespace classifier { 
    99
     10  Kernel_MEV::Kernel_MEV(const Kernel_MEV& kernel,
     11                         const std::vector<size_t>& index)
     12    : Kernel(kernel,index)
     13  {
     14  }
     15
     16
    1017  double Kernel_MEV::operator()(const size_t row, const size_t column) const
    1118  {
    12     return (*kf_)(DataLookup1D(data_,row,false),
    13                   DataLookup1D(data_,column,false));
     19    return (*kf_)(DataLookup1D(*data_,row,false),
     20                  DataLookup1D(*data_,column,false));
    1421  }
    1522
     23
     24  const Kernel_MEV* Kernel_MEV::selected(const std::vector<size_t>& index) const
     25  {
     26    return new Kernel_MEV(*this, index);
     27  }
     28
     29
     30
    1631}} // of namespace classifier and namespace theplu
  • trunk/lib/classifier/Kernel_MEV.h

    r542 r545  
    3737      : Kernel(data,kf) {}
    3838
     39    ///
     40    /// @todo doc
     41    ///
     42    Kernel_MEV(const Kernel_MEV& kernel, const std::vector<size_t>& index);
     43
     44
    3945    ///
    4046    ///   Destructor
     
    5258    ///
    5359    inline double element(const DataLookup1D& vec, const size_t i) const
    54     { return kf_->operator()(vec, DataLookup1D(data_,i)); }
     60    { return kf_->operator()(vec, DataLookup1D(*data_,i)); }
    5561     
    5662    inline double element(const DataLookup1D& vec, const DataLookup1D& w,
    5763                          const size_t i) const
    58     {return (*kf_)(vec, DataLookup1D(data_,i), w, DataLookup1D(w.size(),1.0));}
     64    {return (*kf_)(vec, DataLookup1D(*data_,i), w, DataLookup1D(w.size(),1.0));}
     65
     66    const Kernel_MEV* selected(const std::vector<size_t>& index) const;
     67
     68    inline bool weighted(void) const { return false; }
    5969
    6070  private:
  • trunk/lib/classifier/Kernel_SEV.cc

    r542 r545  
    2121      for (size_t j=i; j<kernel_matrix_.columns(); j++)
    2222        kernel_matrix_(i,j) = kernel_matrix_(j,i) =
    23           (*kf_)(DataLookup1D(data_,i,false),DataLookup1D(data_,j,false));
     23          (*kf_)(DataLookup1D(*data_,i,false),DataLookup1D(*data_,j,false));
    2424  }
    2525
     26  Kernel_SEV::Kernel_SEV(const Kernel_SEV& other,
     27                         const std::vector<size_t>& index)
     28    : Kernel(other, index)
     29  {
     30  }
    2631
    2732  double Kernel_SEV::element(const DataLookup1D& vec, const size_t i) const
    2833  {
    29     return kf_->operator()(vec, DataLookup1D(data_,i));
     34    return kf_->operator()(vec, DataLookup1D(*data_,i));
    3035  }
    3136
     
    3338                             const size_t i) const
    3439  {
    35     return (*kf_)(vec, DataLookup1D(data_,i), w, DataLookup1D(w.size(),1.0));
     40    return (*kf_)(vec, DataLookup1D(*data_,i), w, DataLookup1D(w.size(),1.0));
     41  }
     42
     43  const Kernel* Kernel_SEV::selected(const std::vector<size_t>& index) const
     44  {
     45    return new Kernel_SEV(*this, index);
    3646  }
    3747
  • trunk/lib/classifier/Kernel_SEV.h

    r542 r545  
    4646    Kernel_SEV(const Kernel_SEV&);
    4747
    48     ///
    49     ///   Destructor
    5048    ///
    51     inline virtual ~Kernel_SEV(void) {};
     49    /// @todo doc
     50    ///
     51    Kernel_SEV(const Kernel_SEV& kernel, const std::vector<size_t>& index);
    5252
    5353    ///
     
    6565                   const size_t i) const;
    6666
     67    const Kernel* selected(const std::vector<size_t>& index) const;
    6768
     69    inline bool weighted(void) const { return false; }
    6870  private:
    6971    gslapi::matrix kernel_matrix_;
  • trunk/lib/classifier/Makefile.am

    r527 r545  
    1616  GaussianKernelFunction.cc \
    1717  InputRanker.cc \
     18  Kernel.cc \
    1819  Kernel_MEV.cc \
    1920  Kernel_SEV.cc \
  • trunk/lib/classifier/MatrixLookup.h

    r537 r545  
    8585
    8686    ///
    87     /// Constructor taking the column (default) or row index vector
    88     /// as input.
     87    /// Constructor taking the column (default) or row index vector as
     88    /// input. If @a row is false the created MatrixLookup will have
     89    /// equally many rows as @a matrix.
    8990    ///
    9091    /// @note If underlying matrix goes out of scope or is deleted, the
  • trunk/lib/classifier/SVM.cc

    r544 r545  
    44
    55#include <c++_tools/classifier/DataLookup2D.h>
     6#include <c++_tools/classifier/InputRanker.h>
    67#include <c++_tools/gslapi/matrix.h>
    78#include <c++_tools/gslapi/vector.h>
     
    2930      max_epochs_(10000000),
    3031      output_(target.size(),0),
     32      owner_(false),
    3133      sample_(target.size()),
    3234      trained_(false),
     
    4143      bias_(0),
    4244      C_inverse_(0),
    43       kernel_(&kernel),
    4445      max_epochs_(10000000),
    4546      output_(target.size(),0),
     
    4849      tolerance_(0.00000001)
    4950  {
     51
     52    if (kernel.weighted()){
     53      const MatrixLookup* data = kernel.data();
     54      const MatrixLookup* weights = kernel.weights();
     55      ranker_ = new InputRanker(*data, target, score, *weights);
     56      delete data;
     57      delete weights;
     58    }
     59    else{
     60      const MatrixLookup* data = kernel.data();
     61      ranker_ = new InputRanker(*data, target, score);
     62      delete data;
     63    }
     64
     65    std::vector<size_t> index;
     66    index.reserve(nof_inputs);
     67    for (size_t i=0; i<nof_inputs; i++)
     68      index.push_back(ranker_->id(i));
     69    kernel_ = kernel.selected(index);
     70    owner_ = true;
     71
    5072  }
    5173
     
    5880    const KernelLookup& tmp = dynamic_cast<const KernelLookup&>(data);
    5981    SVM* sc;
    60     if (score_)
     82    if (ranker_)
    6183      sc = new SVM(tmp,target,*score_,nof_inputs_);
    6284    else
     
    6991  void SVM::predict(const DataLookup2D& input, gslapi::matrix& prediction) const
    7092  {
     93    // Peter, should check success of dynamic_cast
     94    const KernelLookup* input_kernel= &dynamic_cast<const KernelLookup&>(input);
     95
     96    if (ranker_) {// feature selection
     97      std::vector<size_t> index;
     98      index.reserve(nof_inputs_);
     99      for (size_t i=0; i<nof_inputs_; i++)
     100        index.push_back(ranker_->id(i));
     101      input_kernel = input_kernel->selected(index);
     102    }
     103
    71104    assert(input.rows()==alpha_.size());
    72105    prediction=gslapi::matrix(2,input.columns(),0);
    73106    for (size_t i = 0; i<input.columns(); i++){
    74107      for (size_t j = 0; i<input.rows(); i++)
    75         prediction(0,i) += target(j)*alpha_(i)*input(j,i);
     108        prediction(0,i) += target(j)*alpha_(i)*(*input_kernel)(j,i);
    76109      prediction(0,i) += bias_;
    77110    }
  • trunk/lib/classifier/SVM.h

    r543 r545  
    251251    unsigned long int max_epochs_;
    252252    gslapi::vector output_;
     253    bool owner_;
    253254    Index sample_;
    254255    bool trained_;
Note: See TracChangeset for help on using the changeset viewer.