PLearn 0.1
DichotomizeDond2DiscreteVariables.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // DichotomizeDond2DiscreteVariables.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 "DichotomizeDond2DiscreteVariables"
00040 
00041 #include "DichotomizeDond2DiscreteVariables.h"
00042 #include <plearn/io/pl_log.h>
00043 
00044 namespace PLearn {
00045 using namespace std;
00046 
00047 PLEARN_IMPLEMENT_OBJECT(
00048     DichotomizeDond2DiscreteVariables,
00049     "Dichotomize variables with discrete values.",
00050     "Instructions are provided with the discrete_variable_instructions option.\n"
00051     "DEPRECATED use DichotomizeVMatrix.cc instead"
00052 );
00053 
00055 // DichotomizeDond2DiscreteVariables //
00057 DichotomizeDond2DiscreteVariables::DichotomizeDond2DiscreteVariables()
00058 {
00059 }
00060     
00062 // declareOptions //
00064 void DichotomizeDond2DiscreteVariables::declareOptions(OptionList& ol)
00065 {
00066     declareOption(ol, "discrete_variable_instructions", &DichotomizeDond2DiscreteVariables::discrete_variable_instructions,
00067                   OptionBase::buildoption,
00068                   "The instructions to dichotomize the variables in the form of field_name : TVec<pair>.\n"
00069                   "The pairs are values from : to, each creating a 0, 1 variable.\n"
00070                   "Variables with no specification will be kept as_is.\n");
00071 
00072     declareOption(ol, "output_path", &DichotomizeDond2DiscreteVariables::output_path,
00073                   OptionBase::buildoption,
00074                   "The file path for the fixed output file.");
00075 
00076     inherited::declareOptions(ol);
00077 }
00078 
00080 // makeDeepCopyFromShallowCopy //
00082 void DichotomizeDond2DiscreteVariables::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00083 {
00084     deepCopyField(discrete_variable_instructions, copies);
00085     deepCopyField(output_path, copies);
00086     inherited::makeDeepCopyFromShallowCopy(copies);
00087 
00088 }
00089 
00091 // build //
00093 void DichotomizeDond2DiscreteVariables::build()
00094 {
00095     // ### Nothing to add here, simply calls build_().
00096     inherited::build();
00097     build_();
00098 }
00099 
00101 // build_ //
00103 void DichotomizeDond2DiscreteVariables::build_()
00104 {
00105     MODULE_LOG << "build_() called" << endl;
00106     if (train_set)
00107     {
00108         dichotomizeDiscreteVariables();
00109     }
00110 }
00111 
00112 void DichotomizeDond2DiscreteVariables::dichotomizeDiscreteVariables()
00113 {    
00114     // initialize primary dataset
00115     int main_length = train_set->length();
00116     int main_width = train_set->width();
00117     Vec main_input(main_width);
00118     TVec<string> main_names(main_width);
00119     TVec<int> main_ins(main_width);
00120     main_ins.fill(-1);
00121     int ins_width = discrete_variable_instructions.size();
00122     main_names << train_set->fieldNames();
00123     for (int ins_col = 0; ins_col < ins_width; ins_col++)
00124     {
00125         int main_col;
00126         for (main_col = 0; main_col < main_width; main_col++)
00127         {
00128             if (discrete_variable_instructions[ins_col].first == main_names[main_col]) break;
00129         }
00130         if (main_col >= main_width) PLERROR("In DichotomizeDond2DiscreteVariables: no field with this name in data set: %s", (discrete_variable_instructions[ins_col].first).c_str());
00131         else main_ins[main_col] = ins_col;
00132     }
00133     
00134     // initialize output datasets
00135     int output_length = main_length;
00136     int output_width = 0;
00137     for (int main_col = 0; main_col < main_width; main_col++)
00138     {
00139         if (main_ins[main_col] < 0) output_width += 1;
00140         else
00141         {
00142             TVec<pair<real, real> > instruction_ptr = discrete_variable_instructions[main_ins[main_col]].second;
00143             output_width += instruction_ptr.size();
00144         }
00145     }
00146     TVec<string> output_names(output_width);
00147     int output_col = 0;
00148     for (int main_col = 0; main_col < main_width; main_col++)
00149     {
00150         if (main_ins[main_col] < 0)
00151         {
00152             output_names[output_col] = main_names[main_col];
00153             output_col += 1;
00154         }
00155         else
00156         {
00157            TVec<pair<real, real> > instruction_ptr = discrete_variable_instructions[main_ins[main_col]].second;
00158            if (instruction_ptr.size() == 0) continue;
00159            for (int ins_col = 0; ins_col < instruction_ptr.size(); ins_col++)
00160            {
00161                output_names[output_col] = main_names[main_col] + "_"
00162                                         + tostring(instruction_ptr[ins_col].first) + "_" 
00163                                         + tostring(instruction_ptr[ins_col].second);
00164                output_col += 1;
00165            }
00166         }    
00167     }
00168     output_file = new FileVMatrix(output_path + ".pmat", output_length, output_names);
00169     output_file->defineSizes(output_width, 0, 0);
00170     
00171     //Now, we can process the discrete variables.
00172     ProgressBar* pb = 0;
00173     pb = new ProgressBar( "Dichotomizing the discrete variables", main_length);
00174     Vec output_record(output_width);
00175 
00176     for (int main_row = 0; main_row < main_length; main_row++)
00177     {
00178         train_set->getRow(main_row, main_input);
00179         output_col = 0;
00180         for (int main_col = 0; main_col < main_width; main_col++)
00181         {
00182             if (main_ins[main_col] < 0)
00183             {
00184                 output_record[output_col] = main_input[main_col];
00185                 output_col += 1;
00186             }
00187             else
00188             {
00189                TVec<pair<real, real> > instruction_ptr = discrete_variable_instructions[main_ins[main_col]].second;
00190                if (instruction_ptr.size() == 0) continue;
00191                for (int ins_col = 0; ins_col < instruction_ptr.size(); ins_col++)
00192                {
00193                    if (is_missing(main_input[main_col])) output_record[output_col] = MISSING_VALUE;
00194                    else if (main_input[main_col] < instruction_ptr[ins_col].first || main_input[main_col] > instruction_ptr[ins_col].second) output_record[output_col] = 0.0;
00195                    else output_record[output_col] = 1.0;
00196                    output_col += 1;
00197                }
00198             }    
00199         }
00200         output_file->putRow(main_row, output_record);
00201         pb->update( main_row );
00202     }
00203     delete pb;
00204 }
00205 
00206 VMat DichotomizeDond2DiscreteVariables::getOutputFile()
00207 {
00208     return output_file;
00209 }
00210 
00211 int DichotomizeDond2DiscreteVariables::outputsize() const {return 0;}
00212 void DichotomizeDond2DiscreteVariables::train()
00213 {
00214         PLERROR("DichotomizeDond2DiscreteVariables: we are done here");
00215 }
00216 void DichotomizeDond2DiscreteVariables::computeOutput(const Vec&, Vec&) const {}
00217 void DichotomizeDond2DiscreteVariables::computeCostsFromOutputs(const Vec&, const Vec&, const Vec&, Vec&) const {}
00218 TVec<string> DichotomizeDond2DiscreteVariables::getTestCostNames() const
00219 {
00220     TVec<string> result;
00221     result.append( "MSE" );
00222     return result;
00223 }
00224 TVec<string> DichotomizeDond2DiscreteVariables::getTrainCostNames() const
00225 {
00226     TVec<string> result;
00227     result.append( "MSE" );
00228     return result;
00229 }
00230 
00231 } // end of namespace PLearn
00232 
00233 
00234 /*
00235   Local Variables:
00236   mode:c++
00237   c-basic-offset:4
00238   c-file-style:"stroustrup"
00239   c-file-offsets:((innamespace . 0)(inline-open . 0))
00240   indent-tabs-mode:nil
00241   fill-column:79
00242   End:
00243 */
00244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines