[612] | 1 | // $Id: CrossValidationSampler.cc 1392 2008-07-28 19:35:30Z peter $ |
---|
| 2 | |
---|
| 3 | /* |
---|
[1275] | 4 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
| 5 | Copyright (C) 2008 Peter Johansson |
---|
[612] | 6 | |
---|
[1392] | 7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
[612] | 8 | |
---|
[675] | 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. |
---|
[612] | 13 | |
---|
[675] | 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 |
---|
[612] | 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 | |
---|
[680] | 25 | #include "CrossValidationSampler.h" |
---|
| 26 | #include "Target.h" |
---|
[675] | 27 | #include "yat/random/random.h" |
---|
[612] | 28 | |
---|
| 29 | #include <algorithm> |
---|
| 30 | #include <cassert> |
---|
| 31 | #include <utility> |
---|
| 32 | #include <vector> |
---|
| 33 | |
---|
| 34 | namespace theplu { |
---|
[680] | 35 | namespace yat { |
---|
[612] | 36 | namespace classifier { |
---|
| 37 | |
---|
| 38 | CrossValidationSampler::CrossValidationSampler(const Target& target, |
---|
| 39 | const size_t N, |
---|
| 40 | const size_t k) |
---|
[823] | 41 | : Sampler(target, N), k_(k) |
---|
[612] | 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 |
---|
[1002] | 70 | for (size_t j=0; j+1<my_begin.size(); ++j) |
---|
[1004] | 71 | random::random_shuffle(v.begin()+my_begin[j],v.begin()+my_begin[j+1]); |
---|
[612] | 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 | |
---|
[1134] | 83 | training_index_.push_back(utility::Index(training_index)); |
---|
| 84 | validation_index_.push_back(utility::Index(validation_index)); |
---|
[612] | 85 | } |
---|
| 86 | } |
---|
| 87 | assert(training_index_.size()==N); |
---|
| 88 | assert(validation_index_.size()==N); |
---|
[615] | 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); |
---|
[612] | 96 | } |
---|
| 97 | |
---|
[680] | 98 | }}} // of namespace classifier, yat, and theplu |
---|