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

KFoldSplitter.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // KFoldSplitter.cc 00004 // 00005 // Copyright (C) 1998 Pascal Vincent 00006 // Copyright (C) 1999,2000 Pascal Vincent, Yoshua Bengio and University of Montreal 00007 // Copyright (C) 2002 Frederic Morin 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 /* ******************************************************* 00038 * $Id: KFoldSplitter.cc,v 1.11 2004/05/14 17:49:14 chrish42 Exp $ 00039 ******************************************************* */ 00040 00042 #include "KFoldSplitter.h" 00043 #include "VMat_maths.h" 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 KFoldSplitter::KFoldSplitter(int k) 00049 : K(k),append_train(0) 00050 {} 00051 00052 PLEARN_IMPLEMENT_OBJECT(KFoldSplitter, 00053 "K-fold cross-validation splitter.", 00054 "KFoldSplitter implements K splits of the dataset into a training-set and a test-set.\n" 00055 "If the number of splits is higher than the number of examples, leave-one-out cross-validation\n" 00056 "will be performed." 00057 ); 00058 00059 void KFoldSplitter::declareOptions(OptionList& ol) 00060 { 00061 declareOption(ol, "K", &KFoldSplitter::K, OptionBase::buildoption, 00062 "Split dataset in K parts."); 00063 00064 declareOption(ol, "append_train", &KFoldSplitter::append_train, OptionBase::buildoption, 00065 "If set to 1, the trainset will be appended after the test set (thus each split\n" 00066 "will contain three sets."); 00067 00068 inherited::declareOptions(ol); 00069 } 00070 00071 void KFoldSplitter::build_() 00072 { 00073 } 00074 00075 // ### Nothing to add here, simply calls build_ 00076 void KFoldSplitter::build() 00077 { 00078 inherited::build(); 00079 build_(); 00080 } 00081 00082 int KFoldSplitter::nsplits() const 00083 { 00084 return K; 00085 } 00086 00087 int KFoldSplitter::nSetsPerSplit() const 00088 { 00089 if (append_train) 00090 return 3; 00091 else 00092 return 2; 00093 } 00094 00095 TVec<VMat> KFoldSplitter::getSplit(int k) 00096 { 00097 if (k >= K) 00098 PLERROR("KFoldSplitter::getSplit() - k (%d) cannot be greater than K (%d)", k, K); 00099 00100 int n_data = dataset->length(); 00101 real test_fraction = K > 0 ? (n_data/(real)K) : 0; 00102 if ((int)(test_fraction) < 1) 00103 test_fraction = 1; // leave-one-out cross-validation 00104 00105 TVec<VMat> split_(2); 00106 split(dataset, test_fraction, split_[0], split_[1], k, true); 00107 if (append_train) { 00108 split_.append(split_[0]); 00109 } 00110 return split_; 00111 } 00112 00113 } // end of namespace PLearn

Generated on Tue Aug 17 15:56:45 2004 for PLearn by doxygen 1.3.7