contrib/mul/clsfy/clsfy_classifier_1d.h
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_classifier_1d.h
00002 #ifndef clsfy_classifier_1d_h_
00003 #define clsfy_classifier_1d_h_
00004 //:
00005 // \file
00006 // \brief Describe an abstract classifier of 1D data
00007 // \author Tim Cootes
00008 
00009 #include <vcl_vector.h>
00010 #include <vnl/vnl_vector.h>
00011 #include <mbl/mbl_data_wrapper.h>
00012 #include <vsl/vsl_binary_io.h>
00013 #include <vcl_iostream.h>
00014 
00015 //:  A common interface for 1-out-of-N classifiers of 1D data
00016 // This class takes a scalar and classifies into one of N classes.
00017 //
00018 // Derived classes with binary in the name indicates that
00019 // the classifier works with only two classes, 0 and 1.
00020 
00021 class clsfy_classifier_1d
00022 {
00023  public:
00024 
00025   // Dflt constructor
00026   clsfy_classifier_1d();
00027 
00028   // Destructor
00029   virtual ~clsfy_classifier_1d();
00030   // this is virtual in case it is referenced via a base class ptr e.g.  "delete ptr"
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(double input) const;
00035 
00036   //: Return parameters defining classifier in a vector (format depends on classifier)
00037   virtual vnl_vector<double> params() const = 0;
00038 
00039   //: Set parameters defining classifier with a vector (format depends on classifier)
00040   virtual void set_params(const vnl_vector<double>& p)=0;
00041 
00042   //: Return the probability the input being in each class.
00043   // output(i) 0<=i<n_classes, contains the probability that the input is in class i
00044   virtual void class_probabilities(vcl_vector<double> &outputs, double input) const = 0;
00045 
00046   //: Classify many input vectors
00047   virtual void classify_many(vcl_vector<unsigned> &outputs, mbl_data_wrapper<double> &inputs) const;
00048 
00049   //: Log likelihood of being in class (binary classifiers only)
00050   // class probability = 1 / (1+exp(-log_l))
00051   // Operation of this method is undefined for multiclass classifiers
00052   virtual double log_l(double input) const = 0;
00053 
00054   //: The number of possible output classes. If ==1, then it's a binary classifier.
00055   virtual unsigned  n_classes() const = 0;
00056 
00057   //: Equality operator for 1d classifiers
00058   virtual bool operator==(const clsfy_classifier_1d& x) const = 0;
00059 
00060   //: Name of the class
00061   virtual vcl_string is_a() const;
00062 
00063   //: Name of the class
00064   virtual bool is_class(vcl_string const& s) const;
00065 
00066   //: Create a copy on the heap and return base class pointer
00067   virtual clsfy_classifier_1d* clone() const = 0;
00068 
00069   //: Print class to os
00070   virtual void print_summary(vcl_ostream& os) const = 0;
00071 
00072   //: Save class to binary file stream
00073   virtual void b_write(vsl_b_ostream& bfs) const = 0;
00074 
00075   //: Load class from binary file stream
00076   virtual void b_read(vsl_b_istream& bfs) = 0;
00077 };
00078 
00079 //: Allows derived class to be loaded by base-class pointer
00080 void vsl_add_to_binary_loader(const clsfy_classifier_1d& b);
00081 
00082 //: Binary file stream output operator for class reference
00083 void vsl_b_write(vsl_b_ostream& bfs, const clsfy_classifier_1d& b);
00084 
00085 //: Binary file stream input operator for class reference
00086 void vsl_b_read(vsl_b_istream& bfs, clsfy_classifier_1d& b);
00087 
00088 //: Stream output operator for class reference
00089 vcl_ostream& operator<<(vcl_ostream& os, const clsfy_classifier_1d& b);
00090 
00091 //: Stream output operator for class pointer
00092 vcl_ostream& operator<<(vcl_ostream& os, const clsfy_classifier_1d* b);
00093 
00094 //: Stream output operator for class reference
00095 inline void vsl_print_summary(vcl_ostream& os, const clsfy_classifier_1d& b)
00096 { os << b;}
00097 
00098 //: Stream output operator for class pointer
00099 inline void vsl_print_summary(vcl_ostream& os, const clsfy_classifier_1d* b)
00100 { os << b;}
00101 
00102 
00103 //----------------------------------------------------------
00104 
00105 //: Calculate the fraction of test samples which are classified incorrectly
00106 double clsfy_test_error(const clsfy_classifier_1d &classifier,
00107                         mbl_data_wrapper<double> & test_inputs,
00108                         const vcl_vector<unsigned> & test_outputs);
00109 
00110 #endif // clsfy_classifier_1d_h_