00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 2003 Olivier Delalleau 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 00035 /* ******************************************************* 00036 * $Id: BootstrapVMatrix.cc,v 1.9 2004/07/21 16:30:55 chrish42 Exp $ 00037 ******************************************************* */ 00038 00039 #include "BootstrapVMatrix.h" 00040 #include <plearn/math/random.h> 00041 #include <plearn/math/TMat_sort.h> 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00048 PLEARN_IMPLEMENT_OBJECT(BootstrapVMatrix, 00049 "A VMatrix that sees a bootstrap subset of its parent VMatrix.\n" 00050 "This is not a real bootstrap since a sample can only appear once." 00051 , 00052 "" 00053 ); 00054 00056 // BootstrapVMatrix // 00058 BootstrapVMatrix::BootstrapVMatrix() 00059 : frac(0.6667), 00060 shuffle(false) 00061 {} 00062 00063 BootstrapVMatrix::BootstrapVMatrix(VMat m, real frac, bool shuffle) 00064 { 00065 this->frac = frac; 00066 this->source = m; 00067 this->shuffle = shuffle; 00068 build(); 00069 } 00070 00072 // declareOptions // 00074 void BootstrapVMatrix::declareOptions(OptionList &ol) 00075 { 00076 declareOption(ol, "shuffle", &BootstrapVMatrix::shuffle, OptionBase::buildoption, 00077 "If set to 1, the indices will be shuffled instead of being sorted."); 00078 00079 declareOption(ol, "frac", &BootstrapVMatrix::frac, OptionBase::buildoption, 00080 "The fraction of elements we keep (default = 0.6667)."); 00081 00082 inherited::declareOptions(ol); 00083 00084 // Hide the 'indices' option, because it will be overridden at build time. 00085 redeclareOption(ol, "indices", &SelectRowsVMatrix::indices, OptionBase::nosave,""); 00086 } 00087 00089 // build // 00091 void BootstrapVMatrix::build() 00092 { 00093 inherited::build(); 00094 build_(); 00095 } 00096 00098 // build_ // 00100 void BootstrapVMatrix::build_() 00101 { 00102 if (source) { 00103 indices = TVec<int>(0, source.length()-1, 1); // Range-vector 00104 shuffleElements(indices); 00105 indices = indices.subVec(0,int(frac * source.length())); 00106 if (!shuffle) { 00107 sortElements(indices); 00108 } 00109 // Because we changed the indices, a rebuild may be needed. 00110 inherited::build(); 00111 } 00112 } 00113 00114 } // end of namespcae PLearn