SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2012 Heiko Strathmann 00008 * Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/evaluation/CrossValidationSplitting.h> 00012 #include <shogun/labels/Labels.h> 00013 00014 using namespace shogun; 00015 00016 CCrossValidationSplitting::CCrossValidationSplitting() : 00017 CSplittingStrategy() 00018 { 00019 m_rng = sg_rand; 00020 } 00021 00022 CCrossValidationSplitting::CCrossValidationSplitting( 00023 CLabels* labels, index_t num_subsets) : 00024 CSplittingStrategy(labels, num_subsets) 00025 { 00026 m_rng = sg_rand; 00027 } 00028 00029 void CCrossValidationSplitting::build_subsets() 00030 { 00031 /* ensure that subsets are empty and set flag to filled */ 00032 reset_subsets(); 00033 m_is_filled=true; 00034 00035 /* permute indices */ 00036 SGVector<index_t> indices(m_labels->get_num_labels()); 00037 indices.range_fill(); 00038 indices.permute(m_rng); 00039 00040 index_t num_subsets=m_subset_indices->get_num_elements(); 00041 00042 /* distribute indices to subsets */ 00043 index_t current_subset=0; 00044 for (index_t i=0; i<indices.vlen; ++i) 00045 { 00046 /* fill current subset */ 00047 CDynamicArray<index_t>* current=(CDynamicArray<index_t>*) 00048 m_subset_indices->get_element(current_subset); 00049 00050 /* add element of current index */ 00051 current->append_element(indices.vector[i]); 00052 00053 /* unref */ 00054 SG_UNREF(current); 00055 00056 /* iterate over subsets */ 00057 current_subset=(current_subset+1) % num_subsets; 00058 } 00059 00060 /* finally shuffle to avoid that subsets with low indices have more 00061 * elements, which happens if the number of class labels is not equal to 00062 * the number of subsets (external random state important for threads) */ 00063 m_subset_indices->shuffle(m_rng); 00064 }