source: trunk/yat/classifier/CrossValidationSampler.cc @ 1000

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

trac moved to new location.

  • 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 1000 2007-12-23 20:09:15Z jari $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6
7  This file is part of the yat library, http://trac.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    random::DiscreteUniform rnd;
69
70    for (size_t i=0; i<N; ) {
71      // shuffle indices within class each class
72      for (size_t j=0; j<target.nof_classes(); j++)
73        random_shuffle(v.begin()+my_begin[j],v.begin()+my_begin[j+1],rnd);
74     
75      for (size_t part=0; part<k && i<N; i++, part++) {
76        std::vector<size_t> training_index;
77        std::vector<size_t> validation_index;
78        for (size_t j=0; j<v.size(); j++) {
79          if (j%k==part)
80            validation_index.push_back(v[j].second);
81          else
82            training_index.push_back(v[j].second);
83        }
84
85        training_index_.push_back(training_index);
86        validation_index_.push_back(validation_index);
87      }
88    }
89    assert(training_index_.size()==N);
90    assert(validation_index_.size()==N);
91   
92    for (size_t i=0; i<N; ++i){
93      training_target_.push_back(Target(target,training_index_[i]));
94      validation_target_.push_back(Target(target,validation_index_[i]));
95    }
96    assert(training_target_.size()==N);
97    assert(validation_target_.size()==N);
98  }
99
100}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.