source: trunk/lib/classifier/Kernel.h @ 545

Last change on this file since 545 was 545, checked in by Peter, 17 years ago

added feature selection for SVM

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// $Id: Kernel.h 545 2006-03-06 13:35:45Z peter $
2
3#ifndef _theplu_classifier_kernel_
4#define _theplu_classifier_kernel_
5
6#include <c++_tools/gslapi/matrix.h>
7#include <c++_tools/gslapi/vector.h>
8#include <c++_tools/classifier/KernelFunction.h>
9#include <c++_tools/classifier/MatrixLookup.h>
10
11#include <cctype>
12#include <vector>
13
14namespace theplu {
15namespace classifier {
16
17  ///
18  ///   @brief Base Class for Kernels.
19  ///
20  ///   Class taking care of the \f$NxN\f$ kernel matrix, where
21  ///   \f$N\f$ is number of samples. Each element in the Kernel
22  ///   matrix is the scalar product of the corresponding pair of
23  ///   samples. Type of Kernel is defined by a KernelFunction.
24  ///   
25  ///   @note If the KernelFunction is destroyed, the Kernel is no
26  ///   longer defined.
27  ///
28  class Kernel
29  {
30   
31  public:
32
33    ///
34    ///   Constructor taking the data matrix and KernelFunction as
35    ///   input.Each column in the data matrix corresponds to one
36    ///   sample.
37    ///
38    ///   @note Can not handle NaNs.
39    ///
40    Kernel(const MatrixLookup& data, const KernelFunction& kf); 
41
42    Kernel(const MatrixLookup& data, const KernelFunction& kf, 
43           const MatrixLookup& weight); 
44   
45    ///
46    /// @todo doc
47    ///
48    Kernel(const Kernel& kernel, const std::vector<size_t>& index);
49
50    ///
51    ///   Destructor
52    ///
53    virtual ~Kernel(void);
54
55    ///
56    /// @return element at position (\a row, \a column) of the Kernel
57    /// matrix
58    ///
59    virtual double operator()(const size_t row, const size_t column) const=0;
60
61    ///
62    /// @return number columns in Kernel
63    ///
64    inline size_t columns(void) const { return size(); } 
65
66    inline const MatrixLookup& data(void) const { return *data_; }
67
68    ///
69    /// @return number of rows in Kernel
70    ///
71    inline size_t rows(void) const { return size(); } 
72
73    ///
74    /// @brief number of samples
75    ///
76    inline size_t size(void) const { return data_->columns(); } 
77
78   
79    virtual double element(const DataLookup1D& vec, const size_t i) const=0;
80    virtual double element(const DataLookup1D& vec, const DataLookup1D& w,
81                           const size_t i) const=0;
82
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
102  protected:
103    const MatrixLookup* data_;
104    const MatrixLookup* weights_;
105    const KernelFunction* kf_;
106    const bool data_owner_;
107    const bool weight_owner_;
108
109  private:
110    ///
111    /// Copy constructor (not implemented)
112    ///
113    Kernel(const Kernel&);
114
115
116  }; // class Kernel
117
118}} // of namespace classifier and namespace theplu
119
120#endif
Note: See TracBrowser for help on using the repository browser.