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

Last change on this file since 848 was 848, checked in by Jari Häkkinen, 14 years ago

Merged log:/branches/0.3.1#838:846 to trunk.

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