contrib/mul/clsfy/clsfy_null_classifier.cxx
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_null_classifier.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 // Copyright (c) 2009: Imorphics plc
00006 
00007 //:
00008 // \file
00009 // \brief  Implement a null classifier
00010 // \author iscott
00011 // \date    25 Aug 2009
00012 
00013 #include "clsfy_null_classifier.h"
00014 
00015 #include <vcl_iostream.h>
00016 #include <vcl_string.h>
00017 #include <vcl_algorithm.h>
00018 #include <vnl/vnl_math.h>
00019 
00020 
00021 //=======================================================================
00022 
00023 vcl_string clsfy_null_classifier::is_a() const
00024 {
00025   return vcl_string("clsfy_null_classifier");
00026 }
00027 
00028 //=======================================================================
00029 
00030 bool clsfy_null_classifier::is_class(vcl_string const& s) const
00031 {
00032   return s == clsfy_null_classifier::is_a() || clsfy_classifier_base::is_class(s);
00033 }
00034 
00035 //=======================================================================
00036 
00037 clsfy_classifier_base* clsfy_null_classifier::clone() const
00038 {
00039   return new clsfy_null_classifier(*this);
00040 }
00041 
00042 //=======================================================================
00043 
00044 void clsfy_null_classifier::print_summary(vcl_ostream& os) const
00045 {
00046   os << "n_dims: " << n_dims_ << " default class: " << default_class_;
00047 }
00048 
00049 //=======================================================================
00050 void clsfy_null_classifier::b_write(vsl_b_ostream& bfs) const
00051 {
00052   const short version_no = 1;
00053   vsl_b_write(bfs, version_no);
00054   vsl_b_write(bfs, n_dims_);
00055   vsl_b_write(bfs, default_class_);
00056 }
00057 
00058 //=======================================================================
00059 
00060   // required if data is present in this base class
00061 void clsfy_null_classifier::b_read(vsl_b_istream& bfs)
00062 {
00063   if (!bfs) return;
00064 
00065   short version;
00066   vsl_b_read(bfs, version);
00067   switch (version)
00068   {
00069   case 1:
00070     vsl_b_read(bfs, n_dims_);
00071     vsl_b_read(bfs, default_class_);
00072     break;
00073   default:
00074     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, clsfy_null_classifier&)\n"
00075              << "           Unknown version number "<< version << '\n';
00076     bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00077   }
00078 }
00079 
00080 //=======================================================================
00081 
00082 //: The number of possible output classes.
00083 unsigned clsfy_null_classifier::n_classes() const
00084 {
00085   return vcl_max(1u,default_class_);
00086 }
00087 
00088 //=======================================================================
00089 
00090 //: Return the probability the input being in each class.
00091 // output(i) i<nClasses, contains the probability that the input is in class i
00092 void clsfy_null_classifier::class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const
00093 {
00094   unsigned n=n_classes();
00095   outputs.resize(n);
00096   for (unsigned i=0; i<n; ++i)
00097     outputs[i] = default_class_?1.0:0.0;
00098 }
00099 
00100 //=======================================================================
00101 
00102 //: Log likelihood of being in class (binary classifiers only)
00103 // class probability = 1 / (1+exp(-log_l))
00104 // Operation of this method is undefined for multiclass classifiers.
00105 double clsfy_null_classifier::log_l(const vnl_vector<double> &input) const
00106 {
00107   return default_class_?1.0:-1.0 * vnl_huge_val(double());
00108 }