source: branches/0.4-stable/yat/classifier/CrossValidationSampler.cc @ 1392

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

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1// $Id: CrossValidationSampler.cc 1392 2008-07-28 19:35:30Z peter $
2
3/*
4  Copyright (C) 2006, 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 2 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 this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "CrossValidationSampler.h"
26#include "Target.h"
27#include "yat/random/random.h"
28
29#include <algorithm>
30#include <cassert>
31#include <utility>
32#include <vector>
33
34namespace theplu {
35namespace yat {
36namespace classifier { 
37
38  CrossValidationSampler::CrossValidationSampler(const Target& target, 
39                                                 const size_t N, 
40                                                 const size_t k)
41    : Sampler(target, N), k_(k)
42  { 
43    assert(target.size()>1);
44    build(target, N, k);
45  }
46
47  CrossValidationSampler::~CrossValidationSampler()
48  {
49  }
50
51  void CrossValidationSampler::build(const Target& target, size_t N, size_t k)
52  {
53    std::vector<std::pair<size_t,size_t> > v;
54    for (size_t i=0; i<target.size(); i++)
55      v.push_back(std::make_pair(target(i),i));
56    // sorting with respect to class
57    std::sort(v.begin(),v.end());
58   
59    // my_begin[i] is index of first sample of class i
60    std::vector<size_t> my_begin;
61    my_begin.reserve(target.nof_classes());
62    my_begin.push_back(0);
63    for (size_t i=1; i<target.size(); i++)
64      while (v[i].first > my_begin.size()-1)
65        my_begin.push_back(i);
66    my_begin.push_back(target.size());
67
68    for (size_t i=0; i<N; ) {
69      // shuffle indices within class each class
70      for (size_t j=0; j+1<my_begin.size(); ++j)
71        random::random_shuffle(v.begin()+my_begin[j],v.begin()+my_begin[j+1]);
72     
73      for (size_t part=0; part<k && i<N; i++, part++) {
74        std::vector<size_t> training_index;
75        std::vector<size_t> validation_index;
76        for (size_t j=0; j<v.size(); j++) {
77          if (j%k==part)
78            validation_index.push_back(v[j].second);
79          else
80            training_index.push_back(v[j].second);
81        }
82
83        training_index_.push_back(utility::Index(training_index));
84        validation_index_.push_back(utility::Index(validation_index));
85      }
86    }
87    assert(training_index_.size()==N);
88    assert(validation_index_.size()==N);
89   
90    for (size_t i=0; i<N; ++i){
91      training_target_.push_back(Target(target,training_index_[i]));
92      validation_target_.push_back(Target(target,validation_index_[i]));
93    }
94    assert(training_target_.size()==N);
95    assert(validation_target_.size()==N);
96  }
97
98}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.