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 | |
---|
13 | namespace theplu { |
---|
14 | namespace 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 | |
---|
51 | /// |
---|
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. |
---|
57 | /// |
---|
58 | /// @note Can not handle NaNs. |
---|
59 | /// |
---|
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. |
---|
66 | /// |
---|
67 | Kernel(const Kernel& kernel, const std::vector<size_t>& index); |
---|
68 | |
---|
69 | /// |
---|
70 | /// Destructor |
---|
71 | /// |
---|
72 | virtual ~Kernel(void); |
---|
73 | |
---|
74 | /// |
---|
75 | /// @return element at position (\a row, \a column) of the Kernel |
---|
76 | /// matrix |
---|
77 | /// |
---|
78 | virtual double operator()(const size_t row, const size_t column) const=0; |
---|
79 | |
---|
80 | /// |
---|
81 | /// @return const reference to the underlying data. |
---|
82 | /// |
---|
83 | inline const DataLookup2D& data(void) const { return *data_; } |
---|
84 | |
---|
85 | /// |
---|
86 | /// @brief number of samples |
---|
87 | /// |
---|
88 | inline size_t size(void) const { return data_->columns(); } |
---|
89 | |
---|
90 | /// |
---|
91 | /// Calculates the scalar product (using the KernelFunction) |
---|
92 | /// between vector @a vec and the \f$ i \f$ th column in the data |
---|
93 | /// matrix. |
---|
94 | /// |
---|
95 | double element(const DataLookup1D& vec, const size_t i) const; |
---|
96 | |
---|
97 | /// |
---|
98 | /// Calculates the weighted scalar product (using the |
---|
99 | /// KernelFunction) between vector @a vec and the \f$ i \f$ th column |
---|
100 | /// in the data matrix. Using a weight vector with all elements |
---|
101 | /// equal to unity yields same result as the non-weighted version |
---|
102 | /// above. |
---|
103 | /// |
---|
104 | double element(const DataLookupWeighted1D& vec, const size_t i) const; |
---|
105 | |
---|
106 | /// |
---|
107 | /// Created Kernel is built from selected features in data. The |
---|
108 | /// @a index corresponds to which rows in data to use for the |
---|
109 | /// calculation of the returned Kernel. |
---|
110 | /// |
---|
111 | /// @return Dynamically allocated Kernel based on selected features |
---|
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* selected(const std::vector<size_t>& index) const=0; |
---|
117 | |
---|
118 | /// |
---|
119 | /// @return true if kernel is calculated using weights |
---|
120 | /// |
---|
121 | inline bool weighted(void) const { return data_w_; } |
---|
122 | |
---|
123 | protected: |
---|
124 | /// underlyung data |
---|
125 | const DataLookup2D* data_; |
---|
126 | /// same as data_ if weifghted otherwise a NULL pointer |
---|
127 | const MatrixLookupWeighted* data_w_; |
---|
128 | /// type of Kernel Function e.g. Gaussian (aka RBF) |
---|
129 | const KernelFunction* kf_; |
---|
130 | /// if true we own data and will delete it in destructor |
---|
131 | const bool data_owner_; |
---|
132 | /// if true we own data_w and will delete it in destructor |
---|
133 | bool weight_owner_; |
---|
134 | |
---|
135 | private: |
---|
136 | /// |
---|
137 | /// Copy constructor (not implemented) |
---|
138 | /// |
---|
139 | Kernel(const Kernel&); |
---|
140 | |
---|
141 | const Kernel& operator=(const Kernel&); |
---|
142 | |
---|
143 | }; // class Kernel |
---|
144 | |
---|
145 | }} // of namespace classifier and namespace theplu |
---|
146 | |
---|
147 | #endif |
---|