contrib/mul/clsfy/clsfy_binary_hyperplane.cxx
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_binary_hyperplane.cxx
00002 // Copyright: (C) 2000 British Telecommunications PLC
00003 #include "clsfy_binary_hyperplane.h"
00004 //:
00005 // \file
00006 // \brief Implement a binary linear classifier
00007 // \author Ian Scott
00008 // \date 2000-05-26
00009 //
00010 //=======================================================================
00011 
00012 #include <vcl_string.h>
00013 #include <vcl_iostream.h>
00014 #include <vcl_vector.h>
00015 #include <vcl_cmath.h>
00016 #include <vsl/vsl_binary_io.h>
00017 #include <vnl/io/vnl_io_matrix.h>
00018 #include <vnl/io/vnl_io_vector.h>
00019 
00020 //=======================================================================
00021 
00022 //: Classify the input vector.
00023 // Returns either 0 (for negative class) or 1 (for positive class)
00024 unsigned clsfy_binary_hyperplane::classify(const vnl_vector<double> &input) const
00025 {
00026   if (dot_product(input, weights_) - bias_ > 0.0) return 1;
00027   else return 0;
00028 }
00029 
00030 
00031 //=======================================================================
00032 
00033 //: Find the posterior probability of the input being in the positive class.
00034 // The result is outputs(0)
00035 void clsfy_binary_hyperplane::class_probabilities(vcl_vector<double> &outputs,
00036                                                   const vnl_vector<double> &input) const
00037 {
00038   outputs.resize(1);
00039   outputs[0] = 1.0 / (1.0 + vcl_exp(-log_l(input)));
00040 }
00041 
00042 
00043 //=======================================================================
00044 
00045 //: Log likelihood of being in the positive class.
00046 // Class probability = 1 / (1+exp(-log_l))
00047 double clsfy_binary_hyperplane::log_l(const vnl_vector<double> &input) const
00048 {
00049   return dot_product(input, weights_) - bias_;
00050 }
00051 
00052 
00053 //=======================================================================
00054 
00055 
00056 vcl_string clsfy_binary_hyperplane::is_a() const
00057 {
00058   return vcl_string("clsfy_binary_hyperplane");
00059 }
00060 
00061 //=======================================================================
00062 
00063 bool clsfy_binary_hyperplane::is_class(vcl_string const& s) const
00064 {
00065   return s == clsfy_binary_hyperplane::is_a() || clsfy_classifier_base::is_class(s);
00066 }
00067 
00068 //=======================================================================
00069 
00070 //:
00071 // required if data is present in this class
00072 void clsfy_binary_hyperplane::print_summary(vcl_ostream& os) const
00073 {
00074   os << "bias: " << bias_ << "   weights: ";
00075   vsl_print_summary(os, weights_);
00076 }
00077 
00078 //=======================================================================
00079 
00080 short clsfy_binary_hyperplane::version_no() const
00081 {
00082   return 1;
00083 }
00084 
00085 //=======================================================================
00086 
00087 void clsfy_binary_hyperplane::b_write(vsl_b_ostream& bfs) const
00088 {
00089   vsl_b_write(bfs,version_no());
00090   vsl_b_write(bfs,weights_);
00091   vsl_b_write(bfs,bias_);
00092 }
00093 
00094 //=======================================================================
00095 
00096 void clsfy_binary_hyperplane::b_read(vsl_b_istream& bfs)
00097 {
00098   if (!bfs) return;
00099 
00100   short version;
00101   vsl_b_read(bfs,version);
00102   switch (version)
00103   {
00104     case (1):
00105       vsl_b_read(bfs,weights_);
00106       vsl_b_read(bfs,bias_);
00107       break;
00108     default:
00109       vcl_cerr << "I/O ERROR: clsfy_binary_hyperplane::b_read(vsl_b_istream&)\n"
00110                << "           Unknown version number "<< version << '\n';
00111       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00112   }
00113 }
00114