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

Last change on this file since 1615 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • 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 1487 2008-09-10 08:41:36Z jari $
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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "CrossValidationSampler.h"
24#include "Target.h"
25#include "yat/random/random.h"
26
27#include <algorithm>
28#include <cassert>
29#include <utility>
30#include <vector>
31
32namespace theplu {
33namespace yat {
34namespace classifier { 
35
36  CrossValidationSampler::CrossValidationSampler(const Target& target, 
37                                                 const size_t N, 
38                                                 const size_t k)
39    : Sampler(target, N), 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    for (size_t i=0; i<N; ) {
67      // shuffle indices within class each class
68      for (size_t j=0; j+1<my_begin.size(); ++j)
69        random::random_shuffle(v.begin()+my_begin[j],v.begin()+my_begin[j+1]);
70     
71      for (size_t part=0; part<k && i<N; i++, part++) {
72        std::vector<size_t> training_index;
73        std::vector<size_t> validation_index;
74        for (size_t j=0; j<v.size(); j++) {
75          if (j%k==part)
76            validation_index.push_back(v[j].second);
77          else
78            training_index.push_back(v[j].second);
79        }
80
81        training_index_.push_back(utility::Index(training_index));
82        validation_index_.push_back(utility::Index(validation_index));
83      }
84    }
85    assert(training_index_.size()==N);
86    assert(validation_index_.size()==N);
87   
88    for (size_t i=0; i<N; ++i){
89      training_target_.push_back(Target(target,training_index_[i]));
90      validation_target_.push_back(Target(target,validation_index_[i]));
91    }
92    assert(training_target_.size()==N);
93    assert(validation_target_.size()==N);
94  }
95
96}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.