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

Last change on this file since 1134 was 1134, checked in by Peter, 14 years ago

using Index class instead of std::vector<size_t>

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