source: trunk/yat/classifier/BootstrapSampler.cc @ 1615

Last change on this file since 1615 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1// $Id: BootstrapSampler.cc 1487 2008-09-10 08:41:36Z jari $
2
3/*
4  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "BootstrapSampler.h"
24#include "Target.h"
25#include "yat/random/random.h"
26
27#include <cassert>
28
29namespace theplu {
30namespace yat {
31namespace classifier { 
32
33  BootstrapSampler::BootstrapSampler(const Target& target, size_t N)
34    : Sampler(target, N)
35  { 
36    assert(target.size()>1);
37    build(target, N);
38  }
39
40  BootstrapSampler::~BootstrapSampler()
41  {
42  }
43
44  void BootstrapSampler::build(const Target& target, size_t N)
45  {
46    // index[i] gives all indices that belong to class i
47    std::vector<std::vector<size_t> > index(target.nof_classes());
48
49    for (size_t i = 0; i<target.size(); ++i)
50      index[target(i)].push_back(i);
51
52    random::DiscreteUniform rnd;
53
54    for (size_t i = 0; i<N; ++i){
55      std::vector<size_t> training_index;
56      std::vector<size_t> validation_index;
57      training_index.reserve(target.size());
58      validation_index.reserve(target.size());
59     
60      for (size_t label=0; label<target.nof_classes(); ++label) {
61        std::vector<char> drawn(index[label].size(),0);
62        for (size_t j=0; j<index[label].size(); ++j) {
63          size_t k = rnd(index[label].size());
64          training_index.push_back(index[label][k]);
65          drawn[k]=1;
66        }
67        for (size_t j=0; j<index[label].size(); ++j)
68          if (!drawn[j]) {
69            validation_index.push_back(index[label][j]);
70          }
71      }
72      training_index_.push_back(utility::Index(training_index));
73      training_target_.push_back(Target(target,utility::Index(training_index)));
74      validation_index_.push_back(utility::Index(validation_index));
75      validation_target_.push_back(Target(target,
76                                          utility::Index(validation_index)));
77    }
78
79  }
80
81}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.