1 | #ifndef _theplu_yat_classifier_kernel_ |
---|
2 | #define _theplu_yat_classifier_kernel_ |
---|
3 | |
---|
4 | // $Id$ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) The authors contributing to this file. |
---|
8 | |
---|
9 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "DataLookup2D.h" |
---|
28 | #include "KernelFunction.h" |
---|
29 | #include "MatrixLookupWeighted.h" |
---|
30 | |
---|
31 | #include <cctype> |
---|
32 | #include <vector> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace classifier { |
---|
37 | |
---|
38 | class MatrixLookup; |
---|
39 | |
---|
40 | /// |
---|
41 | /// @brief Abstract Base Class for Kernels. |
---|
42 | /// |
---|
43 | /// Class taking care of the \f$ NxN \f$ kernel matrix, where \f$ N \f$ |
---|
44 | /// is number of samples. Each element in the Kernel corresponds is |
---|
45 | /// the scalar product of the corresponding pair of samples. At the |
---|
46 | /// time being there are two kinds of kernels. Kernel_SEV that is |
---|
47 | /// optimized to be fast and Kernel_MEV that is preferable when |
---|
48 | /// dealing with many samples and memory might be a |
---|
49 | /// bottleneck. Also there are the corresponding weighted versions |
---|
50 | /// to deal with weights (including missing values). A |
---|
51 | /// KernelFunction defines what kind of scalar product the Kernel |
---|
52 | /// represents, e.g. a Polynomial Kernel of degree 1 means we are |
---|
53 | /// dealing with the ordinary linear scalar product. |
---|
54 | /// |
---|
55 | /// @note If the KernelFunction is destroyed, the Kernel is no |
---|
56 | /// longer defined. |
---|
57 | /// |
---|
58 | class Kernel |
---|
59 | { |
---|
60 | |
---|
61 | public: |
---|
62 | |
---|
63 | /// |
---|
64 | /// Constructor taking the @a data matrix and KernelFunction as |
---|
65 | /// input. Each column in the data matrix corresponds to one |
---|
66 | /// sample and the Kernel matrix is built applying the |
---|
67 | /// KernelFunction on each pair of columns in the data matrix. |
---|
68 | /// |
---|
69 | /// @note Can not handle NaNs. |
---|
70 | /// |
---|
71 | Kernel(const MatrixLookup& data, const KernelFunction& kf, |
---|
72 | const bool own=false); |
---|
73 | |
---|
74 | /// |
---|
75 | /// Constructor taking the @a data matrix (with weights) and |
---|
76 | /// KernelFunction as |
---|
77 | /// input. Each column in the data matrix corresponds to one |
---|
78 | /// sample and the Kernel matrix is built applying the |
---|
79 | /// KernelFunction on each pair of columns in the data matrix. |
---|
80 | /// |
---|
81 | /// @note Can not handle NaNs. |
---|
82 | /// |
---|
83 | Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, |
---|
84 | const bool own=false); |
---|
85 | |
---|
86 | /// |
---|
87 | /// The new kernel is created using selected features @a |
---|
88 | /// index. Kernel will own its underlying data and delete it in |
---|
89 | /// destructor. |
---|
90 | /// |
---|
91 | Kernel(const Kernel& kernel, const std::vector<size_t>& index); |
---|
92 | |
---|
93 | /// |
---|
94 | /// Destructor |
---|
95 | /// |
---|
96 | virtual ~Kernel(void); |
---|
97 | |
---|
98 | /// |
---|
99 | /// @return element at position (\a row, \a column) of the Kernel |
---|
100 | /// matrix |
---|
101 | /// |
---|
102 | virtual double operator()(const size_t row, const size_t column) const=0; |
---|
103 | |
---|
104 | /// |
---|
105 | /// @return const reference to the underlying data. |
---|
106 | /// |
---|
107 | const DataLookup2D& data(void) const; |
---|
108 | |
---|
109 | /// |
---|
110 | /// Calculates the scalar product (using the KernelFunction) |
---|
111 | /// between vector @a vec and the \f$ i \f$ th column in the data |
---|
112 | /// matrix. |
---|
113 | /// |
---|
114 | double element(const DataLookup1D& vec, const size_t i) const; |
---|
115 | |
---|
116 | /// |
---|
117 | /// Calculates the weighted scalar product (using the |
---|
118 | /// KernelFunction) between vector @a vec and the \f$ i \f$ th column |
---|
119 | /// in the data matrix. Using a weight vector with all elements |
---|
120 | /// equal to unity yields same result as the non-weighted version |
---|
121 | /// above. |
---|
122 | /// |
---|
123 | double element(const DataLookupWeighted1D& vec, const size_t i) const; |
---|
124 | |
---|
125 | /// |
---|
126 | /// An interface for making new classifier objects. This function |
---|
127 | /// allows for specification at run-time of which kernel to |
---|
128 | /// instatiate (see 'Prototype' in Design Patterns). |
---|
129 | /// |
---|
130 | /// @Note Returns a dynamically allocated Kernel, which has |
---|
131 | /// to be deleted by the caller to avoid memory leaks. |
---|
132 | /// |
---|
133 | virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0; |
---|
134 | |
---|
135 | |
---|
136 | /// |
---|
137 | /// An interface for making new classifier objects. This function |
---|
138 | /// allows for specification at run-time of which kernel to |
---|
139 | /// instatiate (see 'Prototype' in Design Patterns). |
---|
140 | /// |
---|
141 | /// @Note Returns a dynamically allocated Kernel, which has |
---|
142 | /// to be deleted by the caller to avoid memory leaks. |
---|
143 | /// |
---|
144 | virtual const Kernel* make_kernel(const MatrixLookupWeighted&, |
---|
145 | const bool own=false) const=0; |
---|
146 | |
---|
147 | |
---|
148 | /** |
---|
149 | \brief number of samples |
---|
150 | */ |
---|
151 | size_t size(void) const; |
---|
152 | |
---|
153 | /// |
---|
154 | /// @return true if kernel is calculated using weights |
---|
155 | /// |
---|
156 | bool weighted(void) const; |
---|
157 | |
---|
158 | protected: |
---|
159 | /// underlying data |
---|
160 | const DataLookup2D* data_; |
---|
161 | /// same as data_ if weifghted otherwise a NULL pointer |
---|
162 | const MatrixLookupWeighted* data_w_; |
---|
163 | /// type of Kernel Function e.g. Gaussian (aka RBF) |
---|
164 | const KernelFunction* kf_; |
---|
165 | |
---|
166 | /// |
---|
167 | /// poiter telling how many owners to underlying data |
---|
168 | /// (data_). NULL if this is not an owner. |
---|
169 | /// |
---|
170 | u_int* ref_count_; |
---|
171 | |
---|
172 | /// |
---|
173 | /// poiter telling how many owners to underlying weights |
---|
174 | /// (data_w_). NULL if this is not an owner. |
---|
175 | /// |
---|
176 | u_int* ref_count_w_; |
---|
177 | |
---|
178 | private: |
---|
179 | /// |
---|
180 | /// Copy constructor (not implemented) |
---|
181 | /// |
---|
182 | Kernel(const Kernel&); |
---|
183 | |
---|
184 | const Kernel& operator=(const Kernel&); |
---|
185 | |
---|
186 | }; // class Kernel |
---|
187 | |
---|
188 | }}} // of namespace classifier, yat, and theplu |
---|
189 | |
---|
190 | #endif |
---|