contrib/mul/clsfy/clsfy_knn_builder.h
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_knn_builder.h
00002 // Copyright: (C) 2001 British Telecommunications plc
00003 #ifndef clsfy_knn_builder_h_
00004 #define clsfy_knn_builder_h_
00005 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00006 #pragma interface
00007 #endif
00008 //:
00009 // \file
00010 // \brief Describe a knn classifier builder
00011 // \author Ian Scott
00012 // \date 2001-10-07
00013 
00014 #include <clsfy/clsfy_builder_base.h>
00015 #include <vcl_vector.h>
00016 #include <vcl_string.h>
00017 #include <vcl_iosfwd.h>
00018 #include <mbl/mbl_data_wrapper.h>
00019 #include <vnl/vnl_vector.h>
00020 
00021 
00022 //: Builds clsfy_k_nearest_neighbour classifiers.
00023 // This class doesn't do any calculation, it is easy to create a
00024 // knn classifier directly - but it allows for consistent swappable
00025 // classifiers.
00026 class clsfy_knn_builder : public clsfy_builder_base
00027 {
00028   //: The number of nearest neighbours to look for.
00029   unsigned k_;
00030 
00031  public:
00032   // Dflt ctor
00033   clsfy_knn_builder();
00034 
00035   //: Create empty model
00036   // Caller is responsible for deletion
00037   virtual clsfy_classifier_base* new_classifier() const;
00038 
00039   //: Build classifier from data
00040   // return the mean error over the training set.
00041   virtual double build(clsfy_classifier_base& model,
00042                        mbl_data_wrapper<vnl_vector<double> >& inputs,
00043                        unsigned nClasses,
00044                        const vcl_vector<unsigned> &outputs) const;
00045 
00046   //: Name of the class
00047   virtual vcl_string is_a() const;
00048 
00049   //: Name of the class
00050   virtual bool is_class(vcl_string const& s) const;
00051 
00052   //: IO Version number
00053   short version_no() const;
00054 
00055   //: Create a copy on the heap and return base class pointer
00056   virtual clsfy_builder_base* clone() const;
00057 
00058   //: Print class to os
00059   virtual void print_summary(vcl_ostream& os) const;
00060 
00061   //: Save class to binary file stream
00062   virtual void b_write(vsl_b_ostream& bfs) const;
00063 
00064   //: Load class from binary file stream
00065   virtual void b_read(vsl_b_istream& bfs);
00066 
00067   //: The number of nearest neighbours to look for.
00068   unsigned k() const;
00069 
00070   //: Set the number of nearest neighbours to look for.
00071   void set_k(unsigned);
00072 
00073   //: Initialise the parameters from a text stream.
00074   void config(vcl_istream &as);
00075 };
00076 
00077 #endif // clsfy_knn_builder_h_