Changeset 527


Ignore:
Timestamp:
Mar 1, 2006, 12:23:53 PM (16 years ago)
Author:
Peter
Message:

Modified Kernel to be built from MatrixLookup? rather than
gslapi::matrix. Also changed interface to create DataLookup1D from
DataLookup2D - is now coherent with gslapi.

Location:
trunk
Files:
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/classifier/DataLookup1D.cc

    r505 r527  
    1717  }
    1818
     19  double DataLookup1D::operator*(const DataLookup1D& other) const
     20  {
     21    assert(other.size()==size());
     22    double res=0;
     23    for (size_t i = 0; i<size(); i++)
     24      res += (*this)(i)*other(i);
     25    return res;
     26  }
     27
     28  std::ostream& operator<< (std::ostream& os, const DataLookup1D& x)
     29  {
     30    os.setf(std::ios::dec);
     31    os.precision(12);
     32    for (size_t i = 0; i < x.size(); ++i) {
     33      os << x(i);
     34      if ( (i+1)<x.size() )
     35        os << " ";
     36    }
     37    return os;
     38  }
    1939}} // of namespace classifier and namespace theplu
  • trunk/lib/classifier/DataLookup1D.h

    r505 r527  
    77
    88#include <cassert>
     9#include <iostream>
    910#include <vector>
    1011
     
    2728    ///
    2829    DataLookup1D(const DataLookup2D&, const size_t,
    29                  const bool column_vector=true);
     30                 const bool row_vector=true);
    3031
    3132    ///
     
    3839    ///
    3940    inline size_t size(void) const
    40       { return column_vector_ ? matrix_->rows() : matrix_->columns(); }
     41      { return column_vector_ ? matrix_->columns() : matrix_->rows(); }
    4142
    4243    ///
     
    4546    inline double operator()(const size_t i) const
    4647    { assert(i<size());
    47     return column_vector_ ? (*matrix_)(i,index_) : (*matrix_)(index_,i); }
     48      return column_vector_ ? (*matrix_)(index_,i) : (*matrix_)(i,index_); }
     49
     50    ///
     51    /// scalar product
     52    ///
     53    double operator*(const DataLookup1D&) const;
    4854
    4955  private:
     
    5561
    5662  }; 
     63  ///
     64  /// The output operator DataLook1D
     65  ///
     66  /// @todo test that output can be used from gslapi::vector istream constructor
     67  std::ostream& operator<< (std::ostream& s, const DataLookup1D&);
    5768 
    5869}} // of namespace classifier and namespace theplu
  • trunk/lib/classifier/GaussianKernelFunction.cc

    r451 r527  
    44
    55#include <c++_tools/classifier/KernelFunction.h>
     6#include <c++_tools/classifier/DataLookup1D.h>
    67#include <c++_tools/gslapi/matrix.h>
    7 #include <c++_tools/gslapi/vector.h>
     8
    89
    910#include <math.h>
     
    1213namespace classifier { 
    1314
    14 GaussianKernelFunction::GaussianKernelFunction(double sigma)
    15   : KernelFunction(), sigma_(sigma)
    16 {
    17 }
     15  GaussianKernelFunction::GaussianKernelFunction(double sigma)
     16    : KernelFunction(), sigma2_(sigma*sigma)
     17  {
     18  }
    1819
    19 double GaussianKernelFunction::operator()(const gslapi::vector& a1,
    20                                           const gslapi::vector& a2,
    21                                           const gslapi::vector& w1,
    22                                           const gslapi::vector& w2) const   
    23 {
    24   double distance = 0;
    25   double normalization_factor = 0;
    26   for (size_t i=0; i<a1.size(); i++) {
    27     distance += w1(i) * w2(i) * (a1(i)-a2(i)) * (a1(i)-a2(i));
    28     normalization_factor += w1(i) * w2(i);
     20  double GaussianKernelFunction::operator()(const DataLookup1D& x,
     21                                            const DataLookup1D& y) const
     22  {
     23    assert(x.size()==y.size());
     24    double d2 = 0;
     25    for (size_t i=0; i<x.size(); i++){
     26      double d = x(i)-y(i);
     27      d2 += d*d;
     28    }
     29    return exp(-d2/sigma2_);
    2930  }
    30 // to make it coherent with no weight case
    31   normalization_factor /= a1.size();
    32   return exp(distance/normalization_factor/pow(sigma_,2));
    33 }
    3431
     32  double GaussianKernelFunction::operator()(const DataLookup1D& a1,
     33                                            const DataLookup1D& a2,
     34                                            const DataLookup1D& w1,
     35                                            const DataLookup1D& w2) const   
     36  {
     37    assert(a1.size()==a2.size());
     38    assert(w1.size()==w2.size());
     39    assert(a1.size()==w1.size());
     40    double d2 = 0;
     41    double normalization_factor = 0;
     42    for (size_t i=0; i<a1.size(); i++) {
     43      // ignoring Nan with accompanied weight zero
     44      if (w1(i) && w2(i)){
     45        d2 += w1(i) * w2(i) * (a1(i)-a2(i)) * (a1(i)-a2(i));
     46        normalization_factor += w1(i) * w2(i);
     47      }
     48    }
     49    // to make it coherent with no weight case
     50    normalization_factor /= a1.size();
     51    return exp(d2/normalization_factor/sigma2_);
     52  }
     53 
    3554
    3655
  • trunk/lib/classifier/GaussianKernelFunction.h

    r451 r527  
    66#include <c++_tools/gslapi/vector.h>
    77#include <c++_tools/classifier/KernelFunction.h>
     8#include <c++_tools/classifier/DataLookup1D.h>
    89
    910#include <cmath>
    1011
    1112namespace theplu {
    12 class gslapi::vector;
    13 
    1413namespace classifier {
    1514
     
    3736    /// Gaussian kernel. @return \f$ exp((x - y)^{2}/\sigma^2) \f$ \n
    3837    ///
    39     inline double operator()(const gslapi::vector& a1,
    40                              const gslapi::vector& a2) const
    41     { gslapi::vector a(a1); return exp(-((a-=a2)*a)/(sigma_*sigma_)); }
     38    double operator()(const DataLookup1D& a1,
     39                      const DataLookup1D& a2) const;
    4240
    4341    ///
     
    4543    ///Gaussian kernel with weights.
    4644    ///
    47     double operator()(const gslapi::vector& a1,
    48                       const gslapi::vector& a2,
    49                       const gslapi::vector& w1,
    50                       const gslapi::vector& w2) const;
     45    double operator()(const DataLookup1D& a1,
     46                      const DataLookup1D& a2,
     47                      const DataLookup1D& w1,
     48                      const DataLookup1D& w2) const;
    5149         
    5250  private:
    53     double sigma_;
     51    double sigma2_;
    5452
    5553  }; // class GaussianKernelFunction
  • trunk/lib/classifier/InputRanker.cc

    r482 r527  
    2828    std::vector<std::pair<double, size_t> > score;
    2929    for (size_t i=0; i<nof_genes; i++) {
    30       DataLookup1D vector_value(data,i,false);
    31       double area = score_object.score(target,vector_value);
    32       //double area = score_object.score(target,DataLookup1D(data,i,false));
     30      double area = score_object.score(target,DataLookup1D(data,i,true));
     31      // Peter, remove stupid tmp
    3332      std::pair<double, size_t> tmp(area,i);
    3433      score.push_back(tmp);
     
    5857    std::vector<std::pair<double, size_t> > score;
    5958    for (size_t i=0; i<nof_genes; i++) {
    60       double area = score_object.score(target, DataLookup1D(data,i,false),
    61                                        DataLookup1D(weight,i,false));
     59      double area = score_object.score(target, DataLookup1D(data,i,true),
     60                                       DataLookup1D(weight,i,true));
    6261      std::pair<double, size_t> tmp(area,i);
    6362      score.push_back(tmp);
  • trunk/lib/classifier/Kernel.h

    r523 r527  
    77#include <c++_tools/gslapi/vector.h>
    88#include <c++_tools/classifier/KernelFunction.h>
     9#include <c++_tools/classifier/MatrixLookup.h>
    910
    1011#include <cctype>
     
    3637    ///   @note Can not handle NaNs.
    3738    ///
    38     Kernel(const gslapi::matrix& data, const KernelFunction& kf)
     39    Kernel(const MatrixLookup& data, const KernelFunction& kf)
    3940      : data_(data), kf_(&kf) {};
    4041   
    41     ///
    42     /// Constructor using weights
    43     ///
    44     Kernel(const gslapi::matrix& data, const KernelFunction& kf,
    45            const gslapi::matrix& weights)
    46       : data_(data), kf_(&kf) {};
    47    
    4842    ///
    4943    ///   Destructor
     
    5549    /// matrix
    5650    ///
    57     virtual double operator()(const size_t row,const size_t column) const=0;
     51    virtual double operator()(const size_t row, const size_t column) const=0;
    5852
    5953    ///
     
    6357
    6458   
    65     virtual double element(const gslapi::vector& vec, const size_t i) const=0;
     59    //virtual double element(const gslapi::vector& vec, const size_t i) const=0;
    6660
    6761  protected:
    68     const gslapi::matrix& data_;
     62    // Peter should be a copy
     63    const MatrixLookup& data_;
    6964    const KernelFunction* kf_;
    7065
  • trunk/lib/classifier/KernelFunction.h

    r451 r527  
    55
    66namespace theplu {
    7 
    8 namespace gslapi {
    9   class vector;
    10   class matrix;
    11 }
    12 
    137namespace classifier {
     8  class DataLookup1D;
    149
    1510  ///
     
    3328    /// @return scalar product of two vector in feature space.
    3429    ///
    35     virtual double operator()(const gslapi::vector&,
    36                               const gslapi::vector&) const = 0;
     30    virtual double operator()(const DataLookup1D&,
     31                              const DataLookup1D&) const = 0;
    3732   
    3833    ///
    3934    /// @return scalar product of two vector in feature space.
    4035    ///
    41     virtual double operator()(const gslapi::vector&,
    42                               const gslapi::vector&,
    43                               const gslapi::vector&,
    44                               const gslapi::vector&) const = 0;
     36    virtual double operator()(const DataLookup1D&,
     37                              const DataLookup1D&,
     38                              const DataLookup1D&,
     39                              const DataLookup1D&) const = 0;
    4540   
    4641  }; // class KernelFunction
  • trunk/lib/classifier/KernelLookup.h

    r523 r527  
    8484    { return (*kernel_)(row_index_[row],column_index_[column]); }
    8585
    86     inline double element(const gslapi::vector& vec, const size_t i) const
    87     { return kernel_->element(vec, row_index_[i]); }
     86    //inline double element(const gslapi::vector& vec, const size_t i) const
     87    //{ return kernel_->element(vec, row_index_[i]); }
    8888
    8989  private:
  • trunk/lib/classifier/KernelWeighted_MEV.h

    r523 r527  
    55
    66#include <c++_tools/classifier/Kernel.h>
     7
     8#include <c++_tools/classifier/DataLookup1D.h>
    79#include <c++_tools/classifier/KernelFunction.h>
    8 #include <c++_tools/gslapi/vector.h>
    9 #include <c++_tools/gslapi/matrix.h>
     10#include <c++_tools/classifier/MatrixLookup.h>
     11//#include <c++_tools/gslapi/matrix.h>
    1012
    1113namespace theplu {
     
    3638    /// behaviour of the object is undefined
    3739    ///
    38     inline KernelWeighted_MEV(const gslapi::matrix& data,
     40    inline KernelWeighted_MEV(const MatrixLookup& data,
    3941                              const KernelFunction& kf,
    40                               const gslapi::matrix& weights)
     42                              const MatrixLookup& weights)
    4143    : Kernel(data,kf), weights_(weights) {}
    4244
     
    4547    /// matrix
    4648    ///
    47     inline double operator()(const size_t row, const size_t column) const
    48       { return (*kf_)(gslapi::vector(data_,row,false),
    49                       gslapi::vector(data_,column,false),
    50                       gslapi::vector(weights_,row,false),
    51                       gslapi::vector(weights_,column,false)); }
     49    double operator()(const size_t row, const size_t column) const;
    5250
    5351    ///
    5452    /// @return kernel element between data @a ve and training sample @a i
    5553    ///
    56     inline double element(const gslapi::vector& vec, const size_t i) const
    57     {
    58       return (*kf_)(vec, gslapi::vector(data_,i),
    59                            gslapi::vector(vec.size(),1.0),
    60                            gslapi::vector(weights_,i));
    61     }
     54    //inline double element(const gslapi::vector& vec, const size_t i) const
     55    //{
     56    //  return (*kf_)(vec, gslapi::vector(data_,i),
     57    //                       gslapi::vector(vec.size(),1.0),
     58    //                       gslapi::vector(weights_,i));
     59    //}
    6260
    6361  private:
     
    6765    KernelWeighted_MEV(const KernelWeighted_MEV&);
    6866
    69     const gslapi::matrix& weights_;
     67    const MatrixLookup& weights_;
    7068
    7169  };
  • trunk/lib/classifier/KernelWeighted_SEV.cc

    r513 r527  
    33#include <c++_tools/classifier/KernelWeighted_SEV.h>
    44
     5#include <c++_tools/classifier/DataLookup1D.h>
    56#include <c++_tools/classifier/Kernel.h>
    67#include <c++_tools/classifier/KernelFunction.h>
     8#include <c++_tools/classifier/MatrixLookup.h>
    79#include <c++_tools/gslapi/matrix.h>
    810#include <c++_tools/gslapi/vector.h>
     
    1214
    1315
    14 KernelWeighted_SEV::KernelWeighted_SEV(const gslapi::matrix& data,
     16KernelWeighted_SEV::KernelWeighted_SEV(const MatrixLookup& data,
    1517                                       const KernelFunction& kf,
    16                                        const gslapi::matrix& weights)
     18                                       const MatrixLookup& weights)
    1719  : Kernel(data,kf), weights_(weights)
    1820{
     
    2123    for (size_t j=i; j<kernel_matrix_.columns(); j++)
    2224      kernel_matrix_(i,j) = kernel_matrix_(j,i) =
    23         (*kf_)(gslapi::vector(data_,i,false),gslapi::vector(data_,j,false),
    24                gslapi::vector(weights_,i,false),
    25                gslapi::vector(weights_,j,false));
     25        (*kf_)(DataLookup1D(data_,i,false),
     26               DataLookup1D(data_,j,false),
     27               DataLookup1D(weights_,i,false),
     28               DataLookup1D(weights_,j,false));
    2629}
    2730
  • trunk/lib/classifier/KernelWeighted_SEV.h

    r523 r527  
    44#define _theplu_classifier_kernel_weighted_sev_
    55
     6#include <c++_tools/classifier/DataLookup1D.h>
    67#include <c++_tools/classifier/Kernel.h>
     8#include <c++_tools/classifier/MatrixLookup.h>
    79#include <c++_tools/gslapi/matrix.h>
    810#include <c++_tools/gslapi/vector.h>
     
    3840    /// behaviour of the object is undefined
    3941    ///
    40     KernelWeighted_SEV(const gslapi::matrix& data, const KernelFunction& kf,
    41                        const gslapi::matrix& weights);
     42    KernelWeighted_SEV(const MatrixLookup& data, const KernelFunction& kf,
     43                       const MatrixLookup& weights);
    4244
    4345   
     
    5254    /// @return kernel element between data @a ve and training sample @a i
    5355    ///
    54     inline double element(const gslapi::vector& vec, const size_t i) const
    55     {
    56       return (*kf_)(vec, gslapi::vector(data_,i),
    57                     gslapi::vector(vec.size(),1.0),
    58                     gslapi::vector(weights_,i));
    59     }
     56    //inline double element(const DataLookup1D& vec, const size_t i) const
     57    //{
     58    //  return (*kf_)(vec, DataLookup1D(data_,i),
     59    //                DataLookup1D(vec.size(),1.0),
     60    //                DataLookup1D(weights_,i));
     61    //}
    6062
    6163
     
    6567
    6668    gslapi::matrix kernel_matrix_;
    67     const gslapi::matrix& weights_;
     69    const MatrixLookup& weights_;
    6870
    6971  }; // class Kernel_SEV
  • trunk/lib/classifier/Kernel_MEV.h

    r523 r527  
    44#define _theplu_classifier_kernel_mev_
    55
     6#include <c++_tools/classifier/DataLookup1D.h>
    67#include <c++_tools/classifier/Kernel.h>
    78#include <c++_tools/classifier/KernelFunction.h>
     
    3334    ///   sample. @note Can not handle NaNs.
    3435    ///
    35     inline Kernel_MEV(const gslapi::matrix& data, const KernelFunction& kf)
     36    inline Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf)
    3637      : Kernel(data,kf) {}
    37 
    38     ///
    39     ///   Constructor taking the \a data matrix, the KernelFunction and a
    40     ///   \a weight matrix as input. Each column in the data matrix
    41     ///   corresponds to one sample.
    42     inline Kernel_MEV(const gslapi::matrix& data, const KernelFunction& kf,
    43                       const gslapi::matrix& weight)
    44     : Kernel(data,kf,weight) {}
    4538
    4639    ///
     
    5346    /// matrix
    5447    ///
    55     inline double operator()(const size_t row, const size_t column) const
    56       { return (*kf_)(gslapi::vector(data_,row,false),
    57                       gslapi::vector(data_,column,false)); }
    58 
     48    double operator()(const size_t row, const size_t column) const;
     49   
    5950    ///
    6051    /// @return kernel element between data @a ve and training sample @a i
    6152    ///
    62     inline double element(const gslapi::vector& vec, const size_t i) const
    63     { return kf_->operator()(vec, gslapi::vector(data_,i)); }
     53    //inline double element(const gslapi::vector& vec, const size_t i) const
     54    //{ return kf_->operator()(vec, gslapi::vector(data_,i)); }
    6455     
    6556
  • trunk/lib/classifier/Kernel_SEV.cc

    r523 r527  
    33#include <c++_tools/classifier/Kernel_SEV.h>
    44
     5#include <c++_tools/classifier/DataLookup1D.h>
    56#include <c++_tools/classifier/Kernel.h>
    67#include <c++_tools/classifier/KernelFunction.h>
     8#include <c++_tools/classifier/MatrixLookup.h>
    79#include <c++_tools/gslapi/matrix.h>
    810#include <c++_tools/gslapi/vector.h>
     
    1214
    1315
    14   Kernel_SEV::Kernel_SEV(const gslapi::matrix& data, const KernelFunction& kf)
     16  Kernel_SEV::Kernel_SEV(const MatrixLookup& data, const KernelFunction& kf)
    1517    : Kernel(data,kf)
    1618  {
    1719    kernel_matrix_ = gslapi::matrix(data.columns(),data.columns());
    1820    for (size_t i=0; i<kernel_matrix_.rows(); i++)
    19       for (size_t j=i; j<kernel_matrix_.columns(); j++) 
     21      for (size_t j=i; j<kernel_matrix_.columns(); j++)
    2022        kernel_matrix_(i,j) = kernel_matrix_(j,i) =
    21           (*kf_)(gslapi::vector(data_,i,false),gslapi::vector(data_,j,false));
     23          (*kf_)(DataLookup1D(data_,i,false),DataLookup1D(data_,j,false));
    2224  }
    23 
    24   double Kernel_SEV::element(const gslapi::vector& vec, const size_t i) const
     25  double Kernel_SEV::element(const DataLookup1D& vec, const size_t i) const
    2526  {
    26     return kf_->operator()(vec, gslapi::vector(data_,i));
     27    return kf_->operator()(vec, DataLookup1D(data_,i));
    2728  }
    2829
  • trunk/lib/classifier/Kernel_SEV.h

    r523 r527  
    1111namespace classifier {
    1212
     13  class DataLookup1D;
    1314  class KernelFunction;
    1415
     
    3839    ///   input. @note Can not handle NaNs. When dealing with missing values,
    3940    ///   use constructor taking a weight matrix.
    40     Kernel_SEV(const gslapi::matrix&, const KernelFunction&);
     41    Kernel_SEV(const MatrixLookup&, const KernelFunction&);
    4142   
    4243    ///
     
    6061    /// @return kernel element between data @a ve and training sample @a i
    6162    ///
    62     double element(const gslapi::vector& vec, const size_t i) const;
     63    double element(const DataLookup1D& vec, const size_t i) const;
    6364
    6465
  • trunk/lib/classifier/Makefile.am

    r513 r527  
    1616  GaussianKernelFunction.cc \
    1717  InputRanker.cc \
     18  Kernel_MEV.cc \
    1819  Kernel_SEV.cc \
    1920  KernelLookup.cc \
     21  KernelWeighted_MEV.cc \
     22  KernelWeighted_SEV.cc \
    2023  MatrixLookup.cc \
    21   KernelWeighted_SEV.cc \
    2224  NCC.cc \
    2325  PolynomialKernelFunction.cc \
  • trunk/lib/classifier/MatrixLookup.h

    r482 r527  
    2626    /// Constructor
    2727    ///
    28     MatrixLookup(const gslapi::matrix&);
     28    explicit MatrixLookup(const gslapi::matrix&);
    2929
    3030    ///
  • trunk/lib/classifier/NCC.cc

    r526 r527  
    119119    prediction=gslapi::matrix(centroids_.columns(), input.columns());   
    120120    for(size_t j=0; j<input.columns();j++) {     
    121       DataLookup1D in(input,j,true);
     121      DataLookup1D in(input,j,false);
    122122      gslapi::vector out;
    123123      predict(in,out);
  • trunk/lib/classifier/PolynomialKernelFunction.cc

    r524 r527  
    33
    44#include <c++_tools/classifier/PolynomialKernelFunction.h>
     5#include <c++_tools/classifier/DataLookup1D.h>
    56
    67#include <c++_tools/gslapi/matrix.h>
     
    1920}
    2021
    21 double PolynomialKernelFunction::operator()(const gslapi::vector& a1,
    22                                             const gslapi::vector& a2,
    23                                             const gslapi::vector& w1,
    24                                             const gslapi::vector& w2) const   
     22double PolynomialKernelFunction::operator()(const DataLookup1D& a1,
     23                                            const DataLookup1D& a2,
     24                                            const DataLookup1D& w1,
     25                                            const DataLookup1D& w2) const   
    2526{
    2627  statistics::AveragerPairWeighted averager;
    27   averager.add(a1,a2,w1,w2);
     28  for (size_t i=0; i<a1.size(); i++)
     29    averager.add(a1(i),a2(i),w1(i),w2(i));
    2830
    2931  // a1.size() term to make it coherent with no weight case
  • trunk/lib/classifier/PolynomialKernelFunction.h

    r524 r527  
    55
    66#include <c++_tools/classifier/KernelFunction.h>
    7 #include <c++_tools/gslapi/vector.h>
     7#include <c++_tools/classifier/DataLookup1D.h>
     8
    89
    910#include <cmath>
     
    3839    ///y)^{order} \f$ \n If order is one (linear): \f$ x \cdot y \f$
    3940    ///   
    40     inline double operator()(const gslapi::vector& a1,
    41                              const gslapi::vector& a2) const
    42       { return ((order_>1) ? pow(1+a1*a2,order_) : a1*a2); }
     41    inline double operator()(const DataLookup1D& a1,
     42                             const DataLookup1D& a2) const
     43    { return ((order_>1) ? pow(1+a1*a2,order_) : a1*a2); }
    4344
    4445
     
    5354    ///
    5455    ///
    55     double operator()(const gslapi::vector& x, const gslapi::vector& y,
    56                       const gslapi::vector& wx, const gslapi::vector& wy) const;
     56    double operator()(const DataLookup1D& x, const DataLookup1D& y,
     57                      const DataLookup1D& wx, const DataLookup1D& wy) const;
    5758   
    5859  private:
  • trunk/lib/classifier/SVM.cc

    r523 r527  
    6161  double SVM::predict(const gslapi::vector& x) const
    6262  {
     63    // predict won't work until kernel::element is working
     64    assert(0);
    6365    double y=0;
    64     for (size_t i=0; i<alpha_.size(); i++)
    65       y += alpha_(i)*target_(i)*kernel_->element(x,i);
     66    //for (size_t i=0; i<alpha_.size(); i++)
     67      //y += alpha_(i)*target_(i)*kernel_->element(x,i);
    6668
    6769    return y+bias_;
  • trunk/lib/classifier/SVM.h

    r523 r527  
    106106    /// input.
    107107    ///
    108     /// @note @a target must be a KernelLookup if the target or kernel
    109     /// is destroyed the SVM is no longer defined.
    110     ///
    111     SVM(const KernelLookup&, const Target&);
     108    /// @note if the @a target or @a kernel
     109    /// is destroyed the behaviour is undefined.
     110    ///
     111    SVM(const KernelLookup& kernel, const Target& target);
    112112
    113113    ///
  • trunk/lib/gslapi/vector.cc

    r475 r527  
    77#include <c++_tools/classifier/DataLookup1D.h>
    88
     9#include <iostream>
    910#include <sstream>
    1011#include <vector>
  • trunk/test/ensemble_test.cc

    r521 r527  
    99#include <c++_tools/classifier/Kernel_SEV.h>
    1010#include <c++_tools/classifier/Kernel_MEV.h>
     11#include <c++_tools/classifier/MatrixLookup.h>
    1112#include <c++_tools/classifier/PolynomialKernelFunction.h>
    1213#include <c++_tools/classifier/SVM.h>
     
    3637
    3738  std::ifstream is("data/nm_data_centralized.txt");
    38   gslapi::matrix data(is);
     39  gslapi::matrix data_core(is);
    3940  is.close();
    4041
     42  classifier::MatrixLookup data(data_core);
    4143  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction();
    4244  classifier::Kernel_SEV kernel(data,*kf);
  • trunk/test/kernel_test.cc

    r513 r527  
    2323using namespace theplu;
    2424
    25 bool test_MEV(const gslapi::matrix& data, const classifier::KernelFunction* kf,
     25bool test_MEV(const classifier::MatrixLookup& data,
     26              const classifier::KernelFunction* kf,
    2627              const gslapi::matrix& control, const double error_bound,
    2728              std::ostream* error);
    2829
    29 bool test_SEV(const gslapi::matrix& data, const classifier::KernelFunction* kf,
     30bool test_SEV(const classifier::MatrixLookup& data,
     31              const classifier::KernelFunction* kf,
    3032              const gslapi::matrix& control, const double error_bound,
    3133              std::ostream* error);
     
    4648  bool ok = true;
    4749
     50  gslapi::matrix data2_core(2,3);
     51  data2_core(0,0)=0;
     52  data2_core(1,0)=0;
     53  data2_core(0,1)=0;
     54  data2_core(1,1)=1;
     55  data2_core(0,2)=1;
     56  data2_core(1,2)=0;
     57  classifier::MatrixLookup data2(data2_core);
     58  classifier::KernelFunction* kf2 = new classifier::PolynomialKernelFunction();
     59  classifier::Kernel_SEV kernel2(data2,*kf2);
     60  assert(kernel2.size()==3);
     61  if(kernel2(0,0) || kernel2(0,1) || kernel2(0,2) ||
     62     kernel2(1,0) || kernel2(1,1)!=1 || kernel2(1,2) ||
     63     kernel2(2,0) || kernel2(2,1) || kernel2(2,2)!=1){
     64    ok = false;
     65    *error << "Found:\n";
     66    for (size_t i=0; i<3; i++){
     67      for (size_t j=0; j<3; j++)
     68        *error << kernel2(i,j) << " ";
     69      *error << std::endl;
     70    }
     71    *error << "Expected:\n0 0 0\n0 1 0\n0 0 1" << std::endl;
     72  }
     73  classifier::KernelLookup kv2(kernel2);
     74  delete kf2;
     75
    4876  // Peter, the hardcoded number below should be changed.
    4977  double error_bound = 1e-8;
    5078  std::ifstream is("data/nm_data_centralized.txt");
    51   gslapi::matrix transposed_data(is);
     79  gslapi::matrix data_core(is);
    5280  is.close();
    53   // Because how the kernel is treated is changed, data must be transposed.
    54   gslapi::matrix data=transposed_data;
     81
     82  classifier::MatrixLookup data(data_core);
    5583
    5684  is.open("data/nm_kernel.txt");
     
    7098  delete kf;
    7199
    72   // Checking that a GaussianKernelFunction object can be built at
    73   // compile time.
     100  // Checking that a GaussianKernelFunction object can be built.
    74101  kf = new classifier::GaussianKernelFunction();
    75102  delete kf;
     
    83110}
    84111
    85 bool test_MEV(const gslapi::matrix& data, const classifier::KernelFunction* kf,
     112bool test_MEV(const classifier::MatrixLookup& data,
     113              const classifier::KernelFunction* kf,
    86114              const gslapi::matrix& control, const double error_bound,
    87115              std::ostream* error)
    88116{
    89117  // at least testing constructors
     118  *error << "testing KernelWeighted_MEV" << std::endl;
    90119  classifier::KernelWeighted_MEV kernel_weighted(data,*kf,data);
    91120
     121  *error << "testing Kernel_MEV" << std::endl;
    92122  classifier::Kernel_MEV kernel(data,*kf);
    93123  for(u_int i=0;i<control.rows();i++)
     
    122152}
    123153
    124 bool test_SEV(const gslapi::matrix& data, const classifier::KernelFunction* kf,
     154bool test_SEV(const classifier::MatrixLookup& data,
     155              const classifier::KernelFunction* kf,
    125156              const gslapi::matrix& control, const double error_bound,
    126157              std::ostream* error)
    127158{
    128159  // at least testing constructors
    129   classifier::KernelWeighted_MEV kernel_weighted(data,*kf,data);
     160  *error << "testing KernelWeighted_SEV" << std::endl;
     161  classifier::KernelWeighted_SEV kernel_weighted(data,*kf,data);
    130162
     163  *error << "testing Kernel_SEV" << std::endl;
    131164  classifier::Kernel_SEV kernel(data,*kf);
    132165  for(u_int i=0;i<control.rows();i++)
  • trunk/test/svm_test.cc

    r514 r527  
    3232  bool ok = true;
    3333
    34   gslapi::matrix data2(2,3);
    35   data2(0,0)=0;
    36   data2(1,0)=0;
    37   data2(0,1)=0;
    38   data2(1,1)=1;
    39   data2(0,2)=1;
    40   data2(1,2)=0;
     34  gslapi::matrix data2_core(2,3);
     35  data2_core(0,0)=0;
     36  data2_core(1,0)=0;
     37  data2_core(0,1)=0;
     38  data2_core(1,1)=1;
     39  data2_core(0,2)=1;
     40  data2_core(1,2)=0;
     41  classifier::MatrixLookup data2(data2_core);
    4142  std::vector<std::string> label;
    4243  label.reserve(3);
     
    4950  assert(kernel2.size()==3);
    5051  assert(target2.size()==3);
     52  for (size_t i=0; i<3; i++){
     53    for (size_t j=0; j<3; j++)
     54      *error << kernel2(i,j) << " ";
     55    *error << std::endl;
     56  }
    5157  classifier::KernelLookup kv2(kernel2);
    5258  *error << "testing with linear kernel" << std::endl;
     
    8288
    8389  std::ifstream is("data/nm_data_centralized.txt");
    84   gslapi::matrix transposed_data(is);
     90  gslapi::matrix data_core(is);
    8591  is.close();
    86   // Because how the kernel is treated is changed, data must be transposed.
    87   gslapi::matrix data=transposed_data;
     92
     93  classifier::MatrixLookup data(data_core);
    8894
    8995  classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction();
Note: See TracChangeset for help on using the changeset viewer.