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