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

TrainTestSplitter.cc

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

Generated on Tue Aug 17 16:09:02 2004 for PLearn by doxygen 1.3.7