PLearn 0.1
VMField.cc
Go to the documentation of this file.
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 9230 2008-07-10 22:15:34Z saintmlx $
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 bool VMField::operator==(const VMField& other) const
00053 {
00054     return (name==other.name && fieldtype==other.fieldtype);
00055 }
00056 
00057 bool VMField::operator!=(const VMField& other) const
00058 {
00059     return !((*this)==other);
00060 }
00061 
00062 PStream& operator>>(PStream& in, VMField::FieldType& x) // dummy placeholder; do not call
00063 {
00064     PLERROR("operator>> for VMField::FieldType not implemented (yet).");
00065     return in; // shut up compiler
00066 }
00067 
00068 PStream& operator>>(PStream& in, VMField& x)
00069 {
00070     int y;
00071     in >> ws >> x.name >> ws >> y;
00072     x.fieldtype= static_cast<VMField::FieldType>(y);
00073     return in;
00074 }
00075 PStream& operator<<(PStream& out, const VMField& x)
00076 {
00077     out << x.name << x.fieldtype;
00078     return out;
00079 }
00080 
00083 VMFieldStat::VMFieldStat(int the_maxndiscrete)
00084     : nmissing_(0), nnonmissing_(0), npositive_(0), nnegative_(0), sum_(0.),
00085       sumsquare_(0.), min_(FLT_MAX), max_(-FLT_MAX),
00086       maxndiscrete(the_maxndiscrete) {}
00087 
00088 void VMFieldStat::update(real val)
00089 {
00090     if(is_missing(val))
00091         nmissing_++;
00092     else
00093     {
00094         nnonmissing_++;
00095         sum_ += val;
00096         sumsquare_ += val*val;
00097         if(val>0.)
00098             npositive_++;
00099         else if(val<0.)
00100             nnegative_++;
00101         if(val<min_)
00102             min_ = val;
00103         if(val>max_)
00104             max_ = val;
00105         if(maxndiscrete>0)
00106         {
00107             if(int(counts.size())<maxndiscrete)
00108                 counts[val]++;
00109             else // reached maxndiscrete. Stop counting and reset counts.
00110             {
00111                 maxndiscrete = -1;
00112                 counts.clear();
00113             }
00114         }
00115     }
00116 }
00117 
00118 void VMFieldStat::write(PStream& out) const
00119 {
00120     out << nmissing_ << ' '
00121         << nnonmissing_ << ' '
00122         << npositive_ << ' '
00123         << nnegative_ << ' '
00124         << sum_ << ' '
00125         << sumsquare_ << ' '
00126         << min_ << ' '
00127         << max_ << "    ";
00128 
00129     out << (unsigned int) counts.size() << "  ";
00130 
00131     map<real,int>::const_iterator it = counts.begin();
00132     map<real,int>::const_iterator countsend = counts.end();
00133     while(it!=countsend)
00134     {
00135         out << it->first << ' ' << it->second << "  ";
00136         ++it;
00137     }
00138 }
00139 
00140 void VMFieldStat::read(PStream& in)
00141 {
00142     in >> nmissing_ >> nnonmissing_ >> npositive_ >> nnegative_
00143        >> sum_ >> sumsquare_ >> min_ >> max_ ;
00144 
00145     int ndiscrete;
00146     real value;
00147     int count;
00148     counts.clear();
00149     in >> ndiscrete;
00150     for(int k=0; k<ndiscrete; k++)
00151     {
00152         in >> value >> count;
00153         counts[value] = count;
00154     }
00155 }
00156 
00157 } // end of namespace PLearn
00158 
00159 
00160 /*
00161   Local Variables:
00162   mode:c++
00163   c-basic-offset:4
00164   c-file-style:"stroustrup"
00165   c-file-offsets:((innamespace . 0)(inline-open . 0))
00166   indent-tabs-mode:nil
00167   fill-column:79
00168   End:
00169 */
00170 // 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