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

Last change on this file since 823 was 823, checked in by Peter, 15 years ago

BootstrapSampler?. Fixes #134

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