Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

FractionSplitter.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // FractionSplitter.cc 00004 // 00005 // Copyright (C) 2003 Pascal Vincent 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: FractionSplitter.cc,v 1.5 2004/03/30 16:47:15 tihocan Exp $ 00037 ******************************************************* */ 00038 00040 #include "FractionSplitter.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00045 FractionSplitter::FractionSplitter() 00046 : Splitter(), 00047 round_to_closest(0) 00048 {} 00049 00050 00051 PLEARN_IMPLEMENT_OBJECT(FractionSplitter, 00052 "A Splitter that can extract several subparts of a dataset in each split.", 00053 "Ranges of the dataset are specified explicitly as start:end positions,\n" 00054 "that can be absolute or relative to the number of samples in the training set."); 00055 00056 void FractionSplitter::declareOptions(OptionList& ol) 00057 { 00058 00059 declareOption(ol, "round_to_closest", &FractionSplitter::round_to_closest, OptionBase::buildoption, 00060 "If set to 1, then the integer value found when using fractions will\n" 00061 "be the closest integer, instead of the integer part."); 00062 00063 declareOption(ol, "splits", &FractionSplitter::splits, OptionBase::buildoption, 00064 "A matrix of start:end pairs. Each row represents a split. \n" 00065 "Each start:end element represents a range of samples in the dataset to be splitted. \n" 00066 "start and end, which are positions in the datataset, can be specified as either \n" 00067 "a fraction of the dataset length (if <=1), or an absolute number of elements (if >1).\n" 00068 "The range includes all samples from start to end, but excluding the end sample \n" 00069 "(so that, for ex., the same value can be used as the start of the next range \n" 00070 "without having the two ranges ovelap). \n" 00071 "The value 1 is a bit special as it always means \"until last element inclusive\".\n" 00072 "Ex: 1 2 [ 0:0.80, 0.80:1 ] yields a single split with the first part being the first 80% \n" 00073 "of the data, and the second the next 20% \n"); 00074 00075 // Now call the parent class' declareOptions 00076 inherited::declareOptions(ol); 00077 } 00078 00079 void FractionSplitter::build_() 00080 { 00081 } 00082 00083 // ### Nothing to add here, simply calls build_ 00084 void FractionSplitter::build() 00085 { 00086 inherited::build(); 00087 build_(); 00088 } 00089 00090 void FractionSplitter::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00091 { 00092 Splitter::makeDeepCopyFromShallowCopy(copies); 00093 00094 // ### Call deepCopyField on all "pointer-like" fields 00095 // ### that you wish to be deepCopied rather than 00096 // ### shallow-copied. 00097 // ### ex: 00098 // deepCopyField(trainvec, copies); 00099 00100 // ### Remove this line when you have fully implemented this method. 00101 PLERROR("FractionSplitter::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00102 } 00103 00104 int FractionSplitter::nsplits() const 00105 { 00106 return splits.length(); 00107 } 00108 00109 int FractionSplitter::nSetsPerSplit() const 00110 { 00111 return splits.width(); 00112 } 00113 00114 00115 TVec<VMat> FractionSplitter::getSplit(int k) 00116 { 00117 TVec< pair<real,real> > frac_k = splits(k); 00118 int n = frac_k.length(); 00119 TVec<VMat> vms(n); 00120 int l = dataset.length(); 00121 int start = 0; 00122 int end = 0; 00123 for(int i=0; i<n; i++) 00124 { 00125 real fstart = frac_k[i].first; 00126 real fend = frac_k[i].second; 00127 00128 if(fstart>1) // absolute position 00129 start = int(fstart); 00130 else {// relative position 00131 if (round_to_closest) { 00132 start = int(fstart*l + 0.5); 00133 } else { 00134 start = int(fstart*l); 00135 } 00136 } 00137 00138 if(fend>1) // absolute end position 00139 end = int(fend); 00140 else if(fend==1) // until last element inclusive 00141 end = l; 00142 else {// relative end position 00143 if (round_to_closest) { 00144 end = int(fend*l + 0.5); 00145 } else { 00146 end = int(fend*l); 00147 } 00148 } 00149 00150 vms[i] = dataset.subMatRows(start, end-start); 00151 } 00152 return vms; 00153 } 00154 00155 } // end of namespace PLearn

Generated on Tue Aug 17 15:53:28 2004 for PLearn by doxygen 1.3.7