contrib/mul/clsfy/clsfy_random_forest.h
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_random_forest.h
00002 #ifndef clsfy_random_forest_h_
00003 #define clsfy_random_forest_h_
00004 //:
00005 // \file
00006 // \brief Binary tree classifier
00007 // \author Martin Roberts
00008 #include <clsfy/clsfy_classifier_base.h>
00009 #include <clsfy/clsfy_binary_tree.h>
00010 #include <mbl/mbl_cloneable_ptr.h>
00011 #include <vcl_iosfwd.h>
00012 
00013 
00014 //: Forest of clsfy_binary_tree trees
00015 // Note as it uses clsfy_binary_tree this is a 2-state (binary) classifier also
00016 class clsfy_random_forest : public clsfy_classifier_base
00017 {
00018   public:
00019     
00020   //: Constructor
00021     
00022     clsfy_random_forest(); 
00023     
00024     virtual ~clsfy_random_forest();
00025 
00026     
00027   //: Return the classification of the given probe vector.
00028     virtual unsigned classify(const vnl_vector<double> &input) const;
00029 
00030     //: Provides a probability-like value that the input being in each class.
00031     // output(i) i<nClasses, contains the probability that the input is in class i
00032     virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const;
00033 
00034     //: This value has properties of a Log likelihood of being in class (binary classifiers only)
00035     // class probability = exp(logL) / (1+exp(logL))
00036     virtual double log_l(const vnl_vector<double> &input) const;
00037 
00038     //: The number of possible output classes.
00039     virtual unsigned n_classes() const {return 1;}
00040 
00041     //: The dimensionality of input vectors.
00042     virtual unsigned n_dims() const;
00043 
00044     //: Storage version number
00045     virtual short version_no() const;
00046 
00047     //: Name of the class
00048     virtual vcl_string is_a() const;
00049 
00050     //: Name of the class
00051     virtual bool is_class(vcl_string const& s) const;
00052 
00053     //: Create a copy on the heap and return base class pointer
00054     virtual clsfy_classifier_base* clone() const;
00055 
00056     //: Print class to os
00057     virtual void print_summary(vcl_ostream& os) const;
00058 
00059     //: Save class to binary file stream
00060     virtual void b_write(vsl_b_ostream& bfs) const;
00061 
00062     //: Load class from binary file stream
00063     virtual void b_read(vsl_b_istream& bfs);
00064 
00065     unsigned ntrees() const {return trees_.size();}
00066 
00067     void prune();
00068     
00069     //Append the tres of forest2 onto this
00070     clsfy_random_forest& operator+=(const clsfy_random_forest& forest2);
00071 
00072     //: Merge the sub-forests in the input filenames into a single larger one
00073     friend void merge_sub_forests(const vcl_vector<vcl_string>& filenames,
00074                            clsfy_random_forest& large_forest);
00075     //: Merge the sub-forests pointed to the input vector a single larger one
00076     friend void merge_sub_forests(const vcl_vector< clsfy_random_forest*>& sub_forests,
00077                            clsfy_random_forest& large_forest);
00078 
00079     //: Merge the two input forests 
00080     friend clsfy_random_forest operator+(const clsfy_random_forest& forest1,
00081                                          const clsfy_random_forest& forest2);
00082 
00083 
00084     //: Provides a probability-like value that the input being in each class.
00085     // output(i) i<nClasses, contains the probability that the input is in class i
00086     // Do Out of bag estimates, i.e. only use trees whose vector of indices does not include this_indexs
00087     virtual void class_probabilities_oob(vcl_vector<double> &outputs,
00088                                          const vnl_vector<double> &input,
00089                                          const vcl_vector<vcl_vector<unsigned > >& oobIndices,
00090                                          unsigned this_index) const;
00091     
00092 
00093     //: Return the classification of the given probe vector using out of bag trees only.
00094     //See also class_probabilities_oob
00095     virtual unsigned classify_oob(const vnl_vector<double> &input,
00096                                   const vcl_vector<vcl_vector<unsigned > >& oobIndices,
00097                                   unsigned this_index) const;
00098 
00099   private:
00100 
00101     //: The trees in this forest
00102     vcl_vector<mbl_cloneable_ptr<clsfy_classifier_base> > trees_;
00103 
00104     friend class clsfy_random_forest_builder;
00105 };
00106 
00107 #endif // clsfy_random_forest_h_