source: trunk/c++_tools/classifier/Kernel.h @ 659

Last change on this file since 659 was 659, checked in by Peter, 15 years ago

fixes #147 and #136

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 5.4 KB
Line 
1#ifndef _theplu_classifier_kernel_
2#define _theplu_classifier_kernel_
3
4// $Id$
5
6#include <c++_tools/classifier/DataLookup2D.h>
7#include <c++_tools/classifier/KernelFunction.h>
8#include <c++_tools/classifier/MatrixLookupWeighted.h>
9
10#include <cctype>
11#include <vector>
12
13namespace theplu {
14namespace classifier {
15
16  class MatrixLookup;
17
18  ///
19  ///  @brief Abstract Base Class for Kernels.
20  ///
21  ///  Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$
22  ///  is number of samples. Each element in the Kernel corresponds is
23  ///  the scalar product of the corresponding pair of samples. At the
24  ///  time being there are two kinds of kernels. Kernel_SEV that is
25  ///  optimized to be fast and Kernel_MEV that is preferable when
26  ///  dealing with many samples and memory might be a
27  ///  bottleneck. Also there are the corresponding weighted versions
28  ///  to deal with weights (including missing values). A
29  ///  KernelFunction defines what kind of scalar product the Kernel
30  ///  represents, e.g. a Polynomial Kernel of degree 1 means we are
31  ///  dealing with the ordinary linear scalar product.
32  ///
33  /// @note If the KernelFunction is destroyed, the Kernel is no
34  /// longer defined.
35  ///
36  class Kernel
37  {
38   
39  public:
40
41    ///
42    /// Constructor taking the @a data matrix and KernelFunction as
43    /// input. Each column in the data matrix corresponds to one
44    /// sample and the Kernel matrix is built applying the
45    /// KernelFunction on each pair of columns in the data matrix.
46    ///
47    /// @note Can not handle NaNs.
48    ///
49    Kernel(const MatrixLookup& data, const KernelFunction& kf, 
50           const bool own=false); 
51
52    ///
53    /// Constructor taking the @a data matrix (with weights) and
54    /// KernelFunction as
55    /// input. Each column in the data matrix corresponds to one
56    /// sample and the Kernel matrix is built applying the
57    /// KernelFunction on each pair of columns in the data matrix.
58    ///
59    /// @note Can not handle NaNs.
60    ///
61    Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, 
62           const bool own=false); 
63
64    ///
65    /// The new kernel is created using selected features @a
66    /// index. Kernel will own its underlying data and delete it in
67    /// destructor.
68    ///
69    Kernel(const Kernel& kernel, const std::vector<size_t>& index);
70
71    ///
72    ///   Destructor
73    ///
74    virtual ~Kernel(void);
75
76    ///
77    /// @return element at position (\a row, \a column) of the Kernel
78    /// matrix
79    ///
80    virtual double operator()(const size_t row, const size_t column) const=0;
81
82    ///
83    /// @return const reference to the underlying data.
84    ///
85    inline const DataLookup2D& data(void) const { return *data_; }
86
87    ///
88    /// @brief number of samples
89    ///
90    inline size_t size(void) const { return data_->columns(); } 
91
92    ///
93    /// Calculates the scalar product (using the KernelFunction)
94    /// between vector @a vec and the \f$ i \f$ th column in the data
95    /// matrix.
96    ///   
97    double element(const DataLookup1D& vec, const size_t i) const;
98
99    ///
100    /// Calculates the weighted scalar product (using the
101    /// KernelFunction) between vector @a vec and the \f$ i \f$ th column
102    /// in the data matrix. Using a weight vector with all elements
103    /// equal to unity yields same result as the non-weighted version
104    /// above.
105    ///
106    double element(const DataLookupWeighted1D& vec, const size_t i) const;
107
108    ///
109    /// An interface for making new classifier objects. This function
110    /// allows for specification at run-time of which kernel to
111    /// instatiate (see 'Prototype' in Design Patterns).
112    ///
113    /// @Note Returns a dynamically allocated Kernel, which has
114    /// to be deleted by the caller to avoid memory leaks.
115    ///
116    virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0;
117
118
119    ///
120    /// An interface for making new classifier objects. This function
121    /// allows for specification at run-time of which kernel to
122    /// instatiate (see 'Prototype' in Design Patterns).
123    ///
124    /// @Note Returns a dynamically allocated Kernel, which has
125    /// to be deleted by the caller to avoid memory leaks.
126    ///
127    virtual const Kernel* make_kernel(const MatrixLookupWeighted&, 
128                                      const bool own=false) const=0;
129
130
131    ///
132    /// Created Kernel is built from selected features in data. The
133    /// @a index corresponds to which rows in data to use for the
134    /// calculation of the returned Kernel.
135    ///
136    /// @return Dynamically allocated Kernel based on selected features
137    ///
138    /// @Note Returns a dynamically allocated Kernel, which has
139    /// to be deleted by the caller to avoid memory leaks.
140    ///
141    /// @todo remove this function
142    virtual const Kernel* selected(const std::vector<size_t>& index) const=0;
143
144    ///
145    /// @return true if kernel is calculated using weights
146    ///
147    inline bool weighted(void) const { return data_w_; }
148
149  protected:
150    /// underlying data
151    const DataLookup2D* data_;
152    /// same as data_ if weifghted otherwise a NULL pointer
153    const MatrixLookupWeighted* data_w_;
154    /// type of Kernel Function e.g. Gaussian (aka RBF)
155    const KernelFunction* kf_;
156
157    ///
158    /// poiter telling how many owners to underlying data
159    /// (data_). NULL if this is not an owner.
160    ///
161    u_int* ref_count_;
162
163    ///
164    /// poiter telling how many owners to underlying weights
165    /// (data_w_). NULL if this is not an owner.
166    ///
167    u_int* ref_count_w_;
168
169  private:
170    ///
171    /// Copy constructor (not implemented)
172    ///
173    Kernel(const Kernel&);
174
175    const Kernel& operator=(const Kernel&);
176
177  }; // class Kernel
178
179}} // of namespace classifier and namespace theplu
180
181#endif
Note: See TracBrowser for help on using the repository browser.