contrib/mul/clsfy/clsfy_binary_hyperplane.h
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_binary_hyperplane.h
00002 // Copyright: (C) 2000 British Telecommunications PLC
00003 #ifndef clsfy_binary_hyperplane_bin_h_
00004 #define clsfy_binary_hyperplane_bin_h_
00005 //:
00006 // \file
00007 // \brief Describe a linear binary classifier
00008 // \author Ian Scott
00009 // \date 4 June 2001
00010 
00011 #include <clsfy/clsfy_classifier_base.h>
00012 #include <vnl/vnl_vector.h>
00013 #include <vcl_iosfwd.h>
00014 
00015 //:  A binary output hyperplane classifier.
00016 // The classifier could be considered as a single perceptron.
00017 
00018 class clsfy_binary_hyperplane : public clsfy_classifier_base
00019 {
00020  protected:
00021   //: Hyperplane normal
00022   vnl_vector<double> weights_;
00023   //: Bias: ||weights_|| * distance from origin to hyperplane
00024   double bias_;
00025 
00026  public:
00027 
00028   //: Find the posterior probability of the input being in the positive class.
00029   // The result is outputs(0)
00030   virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const;
00031 
00032   //: Classify the input vector.
00033   // Returns a number between 0 and nClasses-1 inclusive to represent the most likely class
00034   virtual unsigned classify(const vnl_vector<double> &input) const;
00035 
00036   //: Log likelihood of being in the positive class.
00037   // Class probability = 1 / (1+exp(-log_l))
00038   virtual double log_l(const vnl_vector<double> &input) const;
00039 
00040   //: Set the position of the hyperplane.
00041   // The object will take and maintain its own deep copy of the data.
00042   // This method should only be used by builders.
00043   void set(const vnl_vector<double> &weights, double bias)
00044   { weights_ = weights; bias_ = bias;}
00045 
00046   //: Get the classifier weight vector (= hyperplane normal)
00047   const vnl_vector<double> &weights() const { return weights_; }
00048 
00049   //: Get the classifier bias.
00050   double bias() const { return bias_; }
00051 
00052   //: The dimensionality of input vectors.
00053   virtual unsigned n_dims() const { return weights_.size();}
00054 
00055   //: The number of possible output classes.
00056   // 1 indicates a binary classifier
00057   virtual unsigned n_classes() const { return 1;}
00058 
00059   //: Version number for I/O
00060   short version_no() const;
00061 
00062   //: Name of the class
00063   virtual vcl_string is_a() const;
00064 
00065   //: Name of the class
00066   virtual bool is_class(vcl_string const& s) const;
00067 
00068   //: Print class to os
00069   virtual void print_summary(vcl_ostream& os) const;
00070 
00071   //: Save class to a binary File Stream
00072   virtual void b_write(vsl_b_ostream& bfs) const;
00073 
00074   //: Create a deep copy.
00075   // Client is responsible for deleting returned object.
00076   virtual clsfy_classifier_base* clone() const
00077   { return new clsfy_binary_hyperplane(*this); }
00078 
00079   //: Load the class from a Binary File Stream
00080   virtual void b_read(vsl_b_istream& bfs);
00081 };
00082 
00083 #endif // clsfy_binary_hyperplane_bin_h_