00001 // This is mul/clsfy/clsfy_k_nearest_neighbour.h 00002 // Copyright: (C) 2000 British Telecommunications plc. 00003 #ifndef clsfy_k_nearest_neighbour_h_ 00004 #define clsfy_k_nearest_neighbour_h_ 00005 //: 00006 // \file 00007 // \brief Describe a KNN 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 <clsfy/clsfy_classifier_base.h> 00016 #include <vcl_iosfwd.h> 00017 00018 //: A Binary k-Nearest Neighbour classifier 00019 class clsfy_k_nearest_neighbour : public clsfy_classifier_base 00020 { 00021 //: The number of nearest neighbours to look for. 00022 unsigned k_; 00023 00024 //: The set of training input values. Size should equal number of outputs. 00025 vcl_vector<vnl_vector<double> > trainInputs_; 00026 00027 //: The set of training output values. Size should equal number of inputs. 00028 vcl_vector<unsigned> trainOutputs_; 00029 00030 public: 00031 //: Construct a KNN classifier. 00032 clsfy_k_nearest_neighbour():k_(1) {} 00033 00034 //: Return the classification of the given probe vector. 00035 virtual unsigned classify(const vnl_vector<double> &input) const; 00036 00037 //: Provides a probability-like value that the input being in each class. 00038 // output(i) i<nClasses, contains the probability that the input is in class i 00039 virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const; 00040 00041 //: This value has properties of a Log likelihood of being in class (binary classifiers only) 00042 // class probability = exp(logL) / (1+exp(logL)) 00043 virtual double log_l(const vnl_vector<double> &input) const; 00044 00045 //: Set the training data. 00046 void set(const vcl_vector<vnl_vector<double> > &inputs, const vcl_vector<unsigned> &outputs); 00047 00048 //: The number of nearest neighbours to look for. 00049 // The default value is 1. 00050 unsigned k() const {return k_;} 00051 00052 //: Set the number of nearest neighbours to look for. 00053 // The default value is 1. 00054 void set_k(unsigned k) {k_ = k;} 00055 00056 //: Return a reference to the training vectors. 00057 // Vectors are ordered similarly to training_classes() 00058 const vcl_vector<vnl_vector<double> >& training_vectors() const {return trainInputs_;} 00059 00060 //: Return a reference to the training classes 00061 // Vectors are ordered similarly to training_vectors() 00062 const vcl_vector<unsigned >& training_classes() const {return trainOutputs_;} 00063 00064 //: The number of possible output classes. 00065 virtual unsigned n_classes() const {return 1;} 00066 00067 //: The dimensionality of input vectors. 00068 virtual unsigned n_dims() const; 00069 00070 //: Storage version number 00071 virtual short version_no() const; 00072 00073 //: Name of the class 00074 virtual vcl_string is_a() const; 00075 00076 //: Name of the class 00077 virtual bool is_class(vcl_string const& s) const; 00078 00079 //: Create a copy on the heap and return base class pointer 00080 virtual clsfy_classifier_base* clone() const; 00081 00082 //: Print class to os 00083 virtual void print_summary(vcl_ostream& os) const; 00084 00085 //: Save class to binary file stream 00086 virtual void b_write(vsl_b_ostream& bfs) const; 00087 00088 //: Load class from binary file stream 00089 virtual void b_read(vsl_b_istream& bfs); 00090 }; 00091 00092 #endif // clsfy_k_nearest_neighbour_h_