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

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

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

  • 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 680 2006-10-11 17:49:03Z 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 "CrossValidationSampler.h"
25#include "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 yat {
35namespace classifier { 
36
37  CrossValidationSampler::CrossValidationSampler(const Target& target, 
38                                                 const size_t N, 
39                                                 const size_t k)
40    : Sampler(target), k_(k)
41  { 
42    assert(target.size()>1);
43    build(target, N, k);
44  }
45
46  CrossValidationSampler::~CrossValidationSampler()
47  {
48  }
49
50  void CrossValidationSampler::build(const Target& target, size_t N, size_t k)
51  {
52    std::vector<std::pair<size_t,size_t> > v;
53    for (size_t i=0; i<target.size(); i++)
54      v.push_back(std::make_pair(target(i),i));
55    // sorting with respect to class
56    std::sort(v.begin(),v.end());
57   
58    // my_begin[i] is index of first sample of class i
59    std::vector<size_t> my_begin;
60    my_begin.reserve(target.nof_classes());
61    my_begin.push_back(0);
62    for (size_t i=1; i<target.size(); i++)
63      while (v[i].first > my_begin.size()-1)
64        my_begin.push_back(i);
65    my_begin.push_back(target.size());
66
67    random::DiscreteUniform rnd;
68
69    for (size_t i=0; i<N; ) {
70      // shuffle indices within class each class
71      for (size_t j=0; j<target.nof_classes(); j++)
72        random_shuffle(v.begin()+my_begin[j],v.begin()+my_begin[j+1],rnd);
73     
74      for (size_t part=0; part<k && i<N; i++, part++) {
75        std::vector<size_t> training_index;
76        std::vector<size_t> validation_index;
77        for (size_t j=0; j<v.size(); j++) {
78          if (j%k==part)
79            validation_index.push_back(v[j].second);
80          else
81            training_index.push_back(v[j].second);
82        }
83
84        training_index_.push_back(training_index);
85        validation_index_.push_back(validation_index);
86      }
87    }
88    assert(training_index_.size()==N);
89    assert(validation_index_.size()==N);
90   
91    for (size_t i=0; i<N; ++i){
92      training_target_.push_back(Target(target,training_index_[i]));
93      validation_target_.push_back(Target(target,validation_index_[i]));
94    }
95    assert(training_target_.size()==N);
96    assert(validation_target_.size()==N);
97  }
98
99}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.