source: trunk/yat/classifier/BootstrapSampler.cc

Last change on this file was 2919, checked in by Peter, 9 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1// $Id: BootstrapSampler.cc 2919 2012-12-19 06:54:23Z peter $
2
3/*
4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2012 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 <config.h>
24
25#include "BootstrapSampler.h"
26#include "Target.h"
27#include "yat/random/random.h"
28
29#include <cassert>
30
31namespace theplu {
32namespace yat {
33namespace classifier { 
34
35  BootstrapSampler::BootstrapSampler(const Target& target, size_t N)
36    : Sampler(target, N)
37  { 
38    assert(target.size()>1);
39    build(target, N);
40  }
41
42  BootstrapSampler::~BootstrapSampler()
43  {
44  }
45
46  void BootstrapSampler::build(const Target& target, size_t N)
47  {
48    // index[i] gives all indices that belong to class i
49    std::vector<std::vector<size_t> > index(target.nof_classes());
50
51    for (size_t i = 0; i<target.size(); ++i)
52      index[target(i)].push_back(i);
53
54    random::DiscreteUniform rnd;
55
56    for (size_t i = 0; i<N; ++i){
57      std::vector<size_t> training_index;
58      std::vector<size_t> validation_index;
59      training_index.reserve(target.size());
60      validation_index.reserve(target.size());
61     
62      for (size_t label=0; label<target.nof_classes(); ++label) {
63        std::vector<char> drawn(index[label].size(),0);
64        for (size_t j=0; j<index[label].size(); ++j) {
65          size_t k = rnd(index[label].size());
66          training_index.push_back(index[label][k]);
67          drawn[k]=1;
68        }
69        for (size_t j=0; j<index[label].size(); ++j)
70          if (!drawn[j]) {
71            validation_index.push_back(index[label][j]);
72          }
73      }
74      training_index_.push_back(utility::Index(training_index));
75      training_target_.push_back(Target(target,utility::Index(training_index)));
76      validation_index_.push_back(utility::Index(validation_index));
77      validation_target_.push_back(Target(target,
78                                          utility::Index(validation_index)));
79    }
80
81  }
82
83}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.