PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // FixDond2BinaryVariables.cc 00004 // 00005 // Copyright (C) 2006 Dan Popovici, Pascal Lamblin 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 // Authors: Dan Popovici 00036 00039 #define PL_LOG_MODULE_NAME "FixDond2BinaryVariables" 00040 00041 #include "FixDond2BinaryVariables.h" 00042 #include <plearn/io/pl_log.h> 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00047 PLEARN_IMPLEMENT_OBJECT( 00048 FixDond2BinaryVariables, 00049 "Fix binary variables with values 0, 1.0 or possibly is_missing.", 00050 "Instructions are provided with the binary_variable_instructions option.\n" 00051 ); 00052 00054 // FixDond2BinaryVariables // 00056 FixDond2BinaryVariables::FixDond2BinaryVariables() 00057 { 00058 } 00059 00061 // declareOptions // 00063 void FixDond2BinaryVariables::declareOptions(OptionList& ol) 00064 { 00065 declareOption(ol, "binary_variable_instructions", &FixDond2BinaryVariables::binary_variable_instructions, 00066 OptionBase::buildoption, 00067 "The instructions to fix the binary variables in the form of field_name : instruction.\n" 00068 "Supported instructions are 9_is_one, not_0_is_one, not_missing_is_one, not_1000_is_one.\n" 00069 "Variables with no specification will be kept as_is.\n"); 00070 00071 declareOption(ol, "output_path", &FixDond2BinaryVariables::output_path, 00072 OptionBase::buildoption, 00073 "The file path for the fixed output file."); 00074 00075 inherited::declareOptions(ol); 00076 } 00077 00079 // makeDeepCopyFromShallowCopy // 00081 void FixDond2BinaryVariables::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00082 { 00083 deepCopyField(binary_variable_instructions, copies); 00084 deepCopyField(output_path, copies); 00085 inherited::makeDeepCopyFromShallowCopy(copies); 00086 00087 } 00088 00090 // build // 00092 void FixDond2BinaryVariables::build() 00093 { 00094 // ### Nothing to add here, simply calls build_(). 00095 inherited::build(); 00096 build_(); 00097 } 00098 00100 // build_ // 00102 void FixDond2BinaryVariables::build_() 00103 { 00104 MODULE_LOG << "build_() called" << endl; 00105 if (train_set) 00106 { 00107 fixBinaryVariables(); 00108 } 00109 } 00110 00111 void FixDond2BinaryVariables::fixBinaryVariables() 00112 { 00113 // initialize primary dataset 00114 main_row = 0; 00115 main_col = 0; 00116 main_length = train_set->length(); 00117 main_width = train_set->width(); 00118 main_input.resize(main_width); 00119 main_names.resize(main_width); 00120 main_ins.resize(main_width); 00121 ins_width = binary_variable_instructions.size(); 00122 main_names << train_set->fieldNames(); 00123 for (main_col = 0; main_col < main_width; main_col++) 00124 { 00125 main_ins[main_col] = "as_is"; 00126 } 00127 for (ins_col = 0; ins_col < ins_width; ins_col++) 00128 { 00129 for (main_col = 0; main_col < main_width; main_col++) 00130 { 00131 if (binary_variable_instructions[ins_col].first == main_names[main_col]) break; 00132 } 00133 if (main_col >= main_width) PLERROR("In FixDond2BinaryVariables: no field with this name in train data set: %s", (binary_variable_instructions[ins_col].first).c_str()); 00134 if (binary_variable_instructions[ins_col].second == "9_is_one") main_ins[main_col] = "9_is_one"; 00135 else if (binary_variable_instructions[ins_col].second == "not_0_is_one") main_ins[main_col] = "not_0_is_one"; 00136 else if (binary_variable_instructions[ins_col].second == "not_missing_is_one") main_ins[main_col] = "not_missing_is_one"; 00137 else if (binary_variable_instructions[ins_col].second == "not_1000_is_one") main_ins[main_col] = "not_1000_is_one"; 00138 else PLERROR("In FixDond2BinaryVariables: unsupported instruction: %s", (binary_variable_instructions[ins_col].second).c_str()); 00139 } 00140 00141 // initialize output datasets 00142 output_file = new FileVMatrix(output_path + ".pmat", main_length, main_names); 00143 output_file->defineSizes(main_width, 0, 0); 00144 00145 //Now, we can process the binary variables. 00146 ProgressBar* pb = 0; 00147 pb = new ProgressBar( "Fixing the binary variables", main_length); 00148 for (main_row = 0; main_row < main_length; main_row++) 00149 { 00150 train_set->getRow(main_row, main_input); 00151 for (main_col = 0; main_col < main_width; main_col++) 00152 { 00153 if (main_ins[main_col] == "not_missing_is_one") 00154 { 00155 if (is_missing(main_input[main_col])) main_input[main_col] = 0.0; 00156 else main_input[main_col] = 1.0; 00157 } 00158 else if (main_ins[main_col] == "not_0_is_one") 00159 { 00160 if (is_missing(main_input[main_col])) continue; 00161 if (main_input[main_col] != 0.0) main_input[main_col] = 1.0; 00162 else main_input[main_col] = 0.0; 00163 } 00164 else if (main_ins[main_col] == "9_is_one") 00165 { 00166 if (is_missing(main_input[main_col])) continue; 00167 if (main_input[main_col] == 9.0) main_input[main_col] = 1.0; 00168 else main_input[main_col] = 0.0; 00169 } 00170 else if (main_ins[main_col] == "not_1000_is_one") 00171 { 00172 if (is_missing(main_input[main_col])) continue; 00173 if (main_input[main_col] != -1000.0) main_input[main_col] = 1.0; 00174 else main_input[main_col] = 0.0; 00175 } 00176 } 00177 output_file->putRow(main_row, main_input); 00178 pb->update( main_row ); 00179 } 00180 delete pb; 00181 } 00182 00183 VMat FixDond2BinaryVariables::getOutputFile() 00184 { 00185 return output_file; 00186 } 00187 00188 int FixDond2BinaryVariables::outputsize() const {return 0;} 00189 void FixDond2BinaryVariables::train() 00190 { 00191 PLERROR("FixDond2BinaryVariables: we are done here"); 00192 } 00193 void FixDond2BinaryVariables::computeOutput(const Vec&, Vec&) const {} 00194 void FixDond2BinaryVariables::computeCostsFromOutputs(const Vec&, const Vec&, const Vec&, Vec&) const {} 00195 TVec<string> FixDond2BinaryVariables::getTestCostNames() const 00196 { 00197 TVec<string> result; 00198 result.append( "MSE" ); 00199 return result; 00200 } 00201 TVec<string> FixDond2BinaryVariables::getTrainCostNames() const 00202 { 00203 TVec<string> result; 00204 result.append( "MSE" ); 00205 return result; 00206 } 00207 00208 } // end of namespace PLearn 00209 00210 00211 /* 00212 Local Variables: 00213 mode:c++ 00214 c-basic-offset:4 00215 c-file-style:"stroustrup" 00216 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00217 indent-tabs-mode:nil 00218 fill-column:79 00219 End: 00220 */ 00221 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :