00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 /* 00039 * $Id: VMField.cc,v 1.3 2004/02/20 21:14:44 chrish42 Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 #include "VMField.h" 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00049 VMField::VMField(const string& the_name, FieldType the_fieldtype) 00050 : name(the_name), fieldtype(the_fieldtype) {} 00051 00052 /* 00053 void VMField::print(ostream& out) const 00054 { 00055 out << name << "\t type: "; 00056 switch(fieldtype) 00057 { 00058 case VMField::UnknownType: 00059 out << "UnknownType\n"; 00060 break; 00061 case VMField::Continuous: 00062 out << "Continuous\n"; 00063 break; 00064 case VMField::DiscrGeneral: 00065 out << "DiscrGeneral\n"; 00066 break; 00067 case VMField::DiscrMonotonic: 00068 out << "DiscrMonotonic\n"; 00069 break; 00070 case VMField::DiscrFloat: 00071 out << "DiscrFloat\n"; 00072 break; 00073 case VMField::Date: 00074 out << "Date\n"; 00075 break; 00076 default: 00077 PLERROR("Can't write name of type"); 00078 } //writeField(out,"fieldtype", fieldtype); 00079 } 00080 00081 void VMField::write(ostream& out) const 00082 { 00083 writeHeader(out,"VMField"); 00084 writeField(out,"name", name); 00085 //writeField(out,"fieldtype", fieldtype); 00086 //out << "fieldtype: " << fieldtype << endl; 00087 writeFooter(out,"VMField"); 00088 } 00089 00090 void VMField::read(istream& in) 00091 { 00092 readHeader(in,"VMField"); 00093 readField(in,"name", name); 00094 //readField(in,"fieldtype", fieldtype); 00095 readFooter(in,"VMField"); 00096 } 00097 */ 00098 00101 VMFieldStat::VMFieldStat(int the_maxndiscrete) 00102 : nmissing_(0), nnonmissing_(0), npositive_(0), nnegative_(0), sum_(0.), 00103 sumsquare_(0.), min_(FLT_MAX), max_(-FLT_MAX), 00104 maxndiscrete(the_maxndiscrete) {} 00105 00106 void VMFieldStat::update(real val) 00107 { 00108 if(is_missing(val)) 00109 nmissing_++; 00110 else 00111 { 00112 nnonmissing_++; 00113 sum_ += val; 00114 sumsquare_ += val*val; 00115 if(val>0.) 00116 npositive_++; 00117 else if(val<0.) 00118 nnegative_++; 00119 if(val<min_) 00120 min_ = val; 00121 if(val>max_) 00122 max_ = val; 00123 if(maxndiscrete>0) 00124 { 00125 if(int(counts.size())<maxndiscrete) 00126 counts[val]++; 00127 else // reached maxndiscrete. Stop counting and reset counts. 00128 { 00129 maxndiscrete = -1; 00130 counts.clear(); 00131 } 00132 } 00133 } 00134 } 00135 00136 void VMFieldStat::write(ostream& out) const 00137 { 00138 out << nmissing_ << ' ' 00139 << nnonmissing_ << ' ' 00140 << npositive_ << ' ' 00141 << nnegative_ << ' ' 00142 << sum_ << ' ' 00143 << sumsquare_ << ' ' 00144 << min_ << ' ' 00145 << max_ << " "; 00146 00147 out << counts.size() << " "; 00148 00149 map<real,int>::const_iterator it = counts.begin(); 00150 map<real,int>::const_iterator countsend = counts.end(); 00151 while(it!=countsend) 00152 { 00153 out << it->first << ' ' << it->second << " "; 00154 ++it; 00155 } 00156 } 00157 00158 void VMFieldStat::read(istream& in) 00159 { 00160 in >> nmissing_ >> nnonmissing_ >> npositive_ >> nnegative_ 00161 >> sum_ >> sumsquare_ >> min_ >> max_ ; 00162 00163 int ndiscrete; 00164 real value; 00165 int count; 00166 counts.clear(); 00167 in >> ndiscrete; 00168 for(int k=0; k<ndiscrete; k++) 00169 { 00170 in >> value >> count; 00171 counts[value] = count; 00172 } 00173 } 00174 00175 } // end of namespace PLearn