contrib/mul/clsfy/clsfy_classifier_base.h

Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_classifier_base.h
00002 // Copyright: (C) 2000 British Telecommunications plc
00003 #ifndef clsfy_classifier_base_h_
00004 #define clsfy_classifier_base_h_
00005 //:
00006 // \file
00007 // \brief Describe an abstract classifier
00008 // \author Ian Scott
00009 // \date 2000/05/10
00010 // \verbatim
00011 //  Modifications
00012 //  2 May 2001 IMS Converted to VXL
00013 // \endverbatim
00014 
00015 #include <vcl_string.h>
00016 #include <vcl_vector.h>
00017 #include <vnl/vnl_vector.h>
00018 #include <mbl/mbl_data_wrapper.h>
00019 #include <vsl/vsl_binary_io.h>
00020 
00021 //:  A common interface for 1-out-of-N classifiers
00022 // This class takes a vector and classifies into one of
00023 // N classes.
00024 //
00025 // Derived classes with binary in the name indicates that
00026 // the classifier works with only two classes, 0 and 1.
00027 
00028 class clsfy_classifier_base
00029 {
00030  public:
00031 
00032   // Dflt constructor
00033    clsfy_classifier_base() {}
00034 
00035   // Destructor
00036    virtual ~clsfy_classifier_base() {}
00037 
00038   //: Classify the input vector
00039   // returns a number between 0 and nClasses-1 inclusive to represent the most likely class
00040   virtual unsigned classify(const vnl_vector<double> &input) const;
00041 
00042   //: Return the probability the input being in each class.
00043   // output(i) 0<=i<nClasses, contains the probability that the input is in class i
00044   virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const = 0;
00045 
00046   //: Classify many input vectors
00047   virtual void classify_many(vcl_vector<unsigned> &outputs, mbl_data_wrapper<vnl_vector<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(const vnl_vector<double> &input) const = 0;
00053 
00054   //: The number of possible output classes.
00055   virtual unsigned n_classes() const = 0;
00056 
00057   //: The dimensionality of input vectors.
00058   virtual unsigned n_dims() 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_base* 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_base& b);
00081 
00082 //: Binary file stream output operator for class reference
00083 void vsl_b_write(vsl_b_ostream& bfs, const clsfy_classifier_base& b);
00084 
00085 //: Binary file stream input operator for class reference
00086 void vsl_b_read(vsl_b_istream& bfs, clsfy_classifier_base& b);
00087 
00088 //: Stream output operator for class reference
00089 vcl_ostream& operator<<(vcl_ostream& os, const clsfy_classifier_base& b);
00090 
00091 //: Stream output operator for class pointer
00092 vcl_ostream& operator<<(vcl_ostream& os, const clsfy_classifier_base* b);
00093 
00094 //: Stream output operator for class reference
00095 inline void vsl_print_summary(vcl_ostream& os, const clsfy_classifier_base& b)
00096 { os << b;}
00097 
00098 //: Stream output operator for class pointer
00099 inline void vsl_print_summary(vcl_ostream& os, const clsfy_classifier_base* 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_base &classifier,
00107                         mbl_data_wrapper<vnl_vector<double> > & test_inputs,
00108                         const vcl_vector<unsigned> & test_outputs);
00109 
00110 //: Calculate the fraction of test samples of a particular class which are classified incorrectly
00111 // \return -1 if there are no samples of test_class. 
00112 double clsfy_test_error(const clsfy_classifier_base &classifier,
00113                         mbl_data_wrapper<vnl_vector<double> > & test_inputs,
00114                         const vcl_vector<unsigned> & test_outputs,
00115                         unsigned test_class);
00116 
00117 
00118 #endif // clsfy_classifier_base_h_

Generated on Thu Jan 8 05:11:33 2009 for contrib/mul/clsfy by  doxygen 1.5.1