source: trunk/c++_tools/classifier/CrossValidationSampler.cc @ 675

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

References #83. Changing project name to yat. Compilation will fail in this revision.

  • 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 675 2006-10-10 12:08:45Z jari $
2
3/*
4  Copyright (C) 2006 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 "yat/classifier/CrossValidationSampler.h"
25#include "yat/classifier/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 classifier { 
35
36  CrossValidationSampler::CrossValidationSampler(const Target& target, 
37                                                 const size_t N, 
38                                                 const size_t k)
39    : Sampler(target), k_(k)
40  { 
41    assert(target.size()>1);
42    build(target, N, k);
43  }
44
45  CrossValidationSampler::~CrossValidationSampler()
46  {
47  }
48
49  void CrossValidationSampler::build(const Target& target, size_t N, size_t k)
50  {
51    std::vector<std::pair<size_t,size_t> > v;
52    for (size_t i=0; i<target.size(); i++)
53      v.push_back(std::make_pair(target(i),i));
54    // sorting with respect to class
55    std::sort(v.begin(),v.end());
56   
57    // my_begin[i] is index of first sample of class i
58    std::vector<size_t> my_begin;
59    my_begin.reserve(target.nof_classes());
60    my_begin.push_back(0);
61    for (size_t i=1; i<target.size(); i++)
62      while (v[i].first > my_begin.size()-1)
63        my_begin.push_back(i);
64    my_begin.push_back(target.size());
65
66    random::DiscreteUniform rnd;
67
68    for (size_t i=0; i<N; ) {
69      // shuffle indices within class each class
70      for (size_t j=0; j<target.nof_classes(); j++)
71        random_shuffle(v.begin()+my_begin[j],v.begin()+my_begin[j+1],rnd);
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(training_index);
84        validation_index_.push_back(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 and namespace theplu
Note: See TracBrowser for help on using the repository browser.