source: trunk/yat/classifier/KernelLookup.cc @ 1126

Last change on this file since 1126 was 1126, checked in by Peter, 16 years ago

working on ticket:234

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 9.4 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of the yat library, http://trac.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "KernelLookup.h"
25#include "DataLookup2D.h"
26#include "MatrixLookup.h"
27#include "MatrixLookupWeighted.h"
28#include "yat/utility/Matrix.h"
29
30#include <cassert>
31
32namespace theplu {
33namespace yat {
34namespace classifier {
35
36  KernelLookup::KernelLookup(const Kernel& kernel, const bool own)
37    : column_index_(utility::Index(kernel.size())), 
38      kernel_(utility::SmartPtr<const Kernel>(&kernel, own)),
39      row_index_(utility::Index(kernel.size()))
40  {
41  }
42
43
44  KernelLookup::KernelLookup(const Kernel& kernel,
45                             const utility::Index& row, 
46                             const utility::Index& column,
47                             const bool owner)
48    : column_index_(column), 
49      kernel_(utility::SmartPtr<const Kernel>(&kernel, false)),
50      row_index_(row)
51  {
52  }
53
54
55  KernelLookup::KernelLookup(const KernelLookup& other, 
56                             const utility::Index& row, 
57                             const utility::Index& column)
58    : column_index_(utility::Index(other.column_index_, column)), 
59      kernel_(other.kernel_),
60      row_index_(utility::Index(other.row_index_, row))
61  {
62  }
63 
64
65  KernelLookup::KernelLookup(const KernelLookup& other)
66    : column_index_(other.column_index_), 
67      kernel_(other.kernel_),
68      row_index_(other.row_index_)
69  {
70  }
71 
72
73  KernelLookup::KernelLookup(const KernelLookup& other, 
74                             const utility::Index& index, 
75                             const bool row)
76    : kernel_(other.kernel_)
77  {
78    if (row) {
79      row_index_ = utility::Index(other.row_index_, index);
80      column_index_ = other.column_index_;
81    }
82    else {
83      row_index_ = other.row_index_;
84      column_index_ = utility::Index(other.column_index_, index);
85    }
86  }
87 
88
89  KernelLookup::~KernelLookup(void)
90  {
91  }
92
93
94  KernelLookup::const_iterator KernelLookup::begin(void) const
95  {
96    return const_iterator(const_iterator::iterator_type(*this, 0, 0), 1);
97  }
98
99
100  KernelLookup::const_column_iterator
101  KernelLookup::begin_column(size_t i) const
102  {
103    return const_column_iterator(const_column_iterator::iterator_type(*this,
104                                                                      0,i), 
105                                 columns());
106  }
107
108
109  KernelLookup::const_row_iterator KernelLookup::begin_row(size_t i) const
110  {
111    return const_row_iterator(const_row_iterator::iterator_type(*this,i,0), 1);
112  }
113
114
115  size_t KernelLookup::columns(void) const
116  {
117    return column_index_.size();
118  }
119
120
121  const DataLookup2D* KernelLookup::data(void) const
122  {
123    return kernel_->data().training_data(column_index_.vector());
124  }
125
126
127  double KernelLookup::element(const DataLookup1D& vec, size_t i) const
128  {
129    return kernel_->element(vec, row_index_[i]);
130  }
131
132
133  double KernelLookup::element(const DataLookupWeighted1D& vec, size_t i) const
134  {
135    return kernel_->element(vec, row_index_[i]);
136  }
137
138
139  KernelLookup::const_iterator KernelLookup::end(void) const
140  {
141    return const_iterator(const_iterator::iterator_type(*this, rows(), 0), 1);
142  }
143
144
145  KernelLookup::const_column_iterator KernelLookup::end_column(size_t i) const
146  {
147    return const_column_iterator(const_column_iterator::iterator_type(*this, 
148                                                                      rows(),i),
149                                 columns());
150  }
151
152
153  KernelLookup::const_row_iterator KernelLookup::end_row(size_t i) const
154  {
155    return const_row_iterator(const_row_iterator::iterator_type(*this,i+1,0),1);
156  }
157
158
159  size_t KernelLookup::rows(void) const
160  {
161    return row_index_.size();
162  }
163
164
165  const KernelLookup* 
166  KernelLookup::selected(const utility::Index& inputs) const
167  {
168    const Kernel* kernel;
169    if (kernel_->weighted()){
170      const MatrixLookupWeighted* ml = 
171        dynamic_cast<const MatrixLookupWeighted*>(data());
172      assert(ml);
173      const MatrixLookupWeighted* ms = 
174        new MatrixLookupWeighted(*ml,inputs.vector(),true);
175      kernel = kernel_->make_kernel(*ms, false);
176    }
177    else {
178      const MatrixLookup* m = 
179        dynamic_cast<const MatrixLookup*>(data());
180      assert(m);
181      // matrix with selected features
182      const MatrixLookup* ms = new MatrixLookup(*m,inputs.vector(),true);
183      kernel = kernel_->make_kernel(*ms,true);
184    }
185    return new KernelLookup(*kernel, true);
186  }
187
188
189  const KernelLookup* KernelLookup::test_kernel(const MatrixLookup& data) const
190  {
191
192    assert(data.rows()==kernel_->data().rows());
193    if (!weighted()){
194      utility::Matrix* data_all = 
195        new utility::Matrix(data.rows(), row_index_.size()+data.columns());
196
197      for (size_t i=0; i<data_all->rows(); ++i) {
198
199        // first some columns from data in kernel_
200        for (size_t j=0; j<row_index_.size(); ++j){
201          (*data_all)(i,j) = kernel_->data()(i,row_index_[j]); 
202        }
203       
204        // last columns are equal to new data
205        for (size_t j=0;j<data.columns(); ++j){
206          (*data_all)(i,j+row_index_.size()) = data(i,j);
207        }
208      }
209      std::vector<size_t> column_index;
210      column_index.reserve(data.columns());
211      for (size_t i=0;i<data.columns(); ++i)
212        column_index.push_back(i+row_index_.size());
213
214      std::vector<size_t> row_index;
215      row_index.reserve(row_index_.size());
216      for (size_t i=0;i<row_index_.size(); ++i)
217        row_index.push_back(i);
218
219      const MatrixLookup* tmp = new MatrixLookup(*data_all, true);
220
221      const Kernel* kernel = 
222        kernel_->make_kernel(*tmp, true);
223
224      return new KernelLookup(*kernel, utility::Index(row_index), 
225                              utility::Index(column_index), true);
226    }
227
228    // kernel_ holds MatrixLookupWeighted, hence new Kernel also
229    // should hold a MatrixLookupweighted.
230    utility::Matrix* data_all = 
231      new utility::Matrix(data.rows(), rows()+data.columns());
232    utility::Matrix* weight_all = 
233      new utility::Matrix(data.rows(), rows()+data.columns(), 1.0);
234    const MatrixLookupWeighted& kernel_data = 
235      dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
236
237    for (size_t i=0; i<data.rows(); ++i){
238
239      // first some columns from data in kernel_
240      for (size_t j=0; j<row_index_.size(); ++j){
241        (*data_all)(i,j) = kernel_data.data(i,row_index_[j]); 
242        (*weight_all)(i,j) = kernel_data.weight(i,row_index_[j]);
243      }
244
245      // last columns are equal to new data
246      for (size_t j=0;j<data.columns(); ++j){
247        (*data_all)(i,j+row_index_.size()) = data(i,j);
248      }
249    }
250    std::vector<size_t> column_index;
251    column_index.reserve(data.columns());
252    for (size_t i=0;i<data.columns(); ++i)
253      column_index.push_back(i+row_index_.size());
254
255    std::vector<size_t> row_index;
256    row_index.reserve(row_index_.size());
257    for (size_t i=0;i<row_index_.size(); ++i)
258      row_index.push_back(i);
259
260    MatrixLookupWeighted* tmp = new MatrixLookupWeighted(*data_all, 
261                                                         *weight_all, true);
262    const Kernel* kernel = kernel_->make_kernel(*tmp, true);
263    return new KernelLookup(*kernel, row_index_, utility::Index(column_index), 
264                            true);
265  }
266
267
268
269  const KernelLookup* 
270  KernelLookup::test_kernel(const MatrixLookupWeighted& data) const
271  {
272    utility::Matrix* data_all = 
273      new utility::Matrix(data.rows(), rows()+data.columns());
274    utility::Matrix* weight_all = 
275      new utility::Matrix(data.rows(), rows()+data.columns(), 1.0);
276
277    if (weighted()){
278      const MatrixLookupWeighted& kernel_data = 
279        dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
280   
281      for (size_t i=0; i<data.rows(); ++i){
282        // first columns are equal to data in kernel_
283        for (size_t j=0; j<row_index_.size(); ++j){
284          (*data_all)(i,j) = kernel_data.data(i,row_index_[j]);
285          (*weight_all)(i,j) = kernel_data.weight(i,row_index_[j]);
286        }
287      }
288    }
289    else {
290
291        dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
292   
293      for (size_t i=0; i<data.rows(); ++i){
294        // first columns are equal to data in kernel_
295        for (size_t j=0; j<row_index_.size(); ++j)
296          (*data_all)(i,j) = kernel_->data()(i,row_index_[j]);
297      }
298    }
299
300    // last columns are equal to new data
301    for (size_t i=0; i<data.rows(); ++i){
302      for (size_t j=0;j<data.columns(); ++j){
303        (*data_all)(i,j+row_index_.size()) = data.data(i,j);
304        (*weight_all)(i,j+row_index_.size()) = data.weight(i,j);
305      }
306    }
307   
308    std::vector<size_t> column_index;
309    column_index.reserve(data.columns());
310    for (size_t i=0;i<data.columns(); ++i)
311      column_index.push_back(i+row_index_.size());
312    const Kernel* kernel = 
313      kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true));
314    return new KernelLookup(*kernel, row_index_, utility::Index(column_index), 
315                            true);
316  }
317
318
319  const KernelLookup*
320  KernelLookup::training_data(const std::vector<size_t>& train) const
321  {
322    return new KernelLookup(*this,utility::Index(train),utility::Index(train));
323  }
324
325
326  const KernelLookup*
327  KernelLookup::validation_data(const std::vector<size_t>& train,
328                                const std::vector<size_t>& validation) const
329  {
330    return new KernelLookup(*this,utility::Index(train),
331                            utility::Index(validation));
332  }
333
334
335  bool KernelLookup::weighted(void) const
336  {
337    return kernel_->weighted();
338  }
339
340
341  double KernelLookup::operator()(size_t row, size_t column) const
342  {
343    return (*kernel_)(row_index_[row],column_index_[column]);
344  }
345
346}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.