contrib/mul/clsfy/clsfy_knn_builder.cxx
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_knn_builder.cxx
00002 // Copyright (c) 2001: British Telecommunications plc
00003 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00004 #pragma implementation
00005 #endif
00006 //:
00007 // \file
00008 // \brief Implement a knn classifier builder
00009 // \author Ian Scott
00010 // \date 2001-10-07
00011 
00012 #include "clsfy_knn_builder.h"
00013 
00014 #include <vcl_iostream.h>
00015 #include <vcl_sstream.h>
00016 #include <vcl_string.h>
00017 #include <vcl_cassert.h>
00018 #include <vsl/vsl_binary_loader.h>
00019 #include <vul/vul_string.h>
00020 #include <mbl/mbl_parse_block.h>
00021 #include <mbl/mbl_read_props.h>
00022 #include <clsfy/clsfy_k_nearest_neighbour.h>
00023 
00024 //=======================================================================
00025 
00026 clsfy_knn_builder::clsfy_knn_builder():
00027 k_(1)
00028 {
00029 }
00030 
00031 
00032 //=======================================================================
00033 
00034 short clsfy_knn_builder::version_no() const
00035 {
00036   return 1;
00037 }
00038 
00039 //=======================================================================
00040 
00041 vcl_string clsfy_knn_builder::is_a() const
00042 {
00043   return vcl_string("clsfy_knn_builder");
00044 }
00045 
00046 //=======================================================================
00047 
00048 bool clsfy_knn_builder::is_class(vcl_string const& s) const
00049 {
00050   return s == clsfy_knn_builder::is_a() || clsfy_builder_base::is_class(s);
00051 }
00052 
00053 //=======================================================================
00054 
00055 clsfy_builder_base* clsfy_knn_builder::clone() const
00056 {
00057   return new clsfy_knn_builder(*this);
00058 }
00059 
00060 //=======================================================================
00061 
00062 void clsfy_knn_builder::print_summary(vcl_ostream& os) const
00063 {
00064   os << "k = " << k_;
00065 }
00066 
00067 //=======================================================================
00068 
00069 void clsfy_knn_builder::b_write(vsl_b_ostream& bfs) const
00070 {
00071   vsl_b_write(bfs, version_no());
00072   vsl_b_write(bfs, k_);
00073   vcl_cerr << "clsfy_knn_builder::b_write() NYI\n";
00074 }
00075 
00076 //=======================================================================
00077 
00078 void clsfy_knn_builder::b_read(vsl_b_istream& bfs)
00079 {
00080   if (!bfs) return;
00081 
00082   short version;
00083   vsl_b_read(bfs,version);
00084   switch (version)
00085   {
00086   case (1):
00087     vsl_b_read(bfs, k_);
00088     break;
00089   default:
00090     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, clsfy_knn_builder&)\n"
00091              << "           Unknown version number "<< version << '\n';
00092     bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00093   }
00094 }
00095 
00096 //=======================================================================
00097 
00098 //: Build model from data
00099 // return the mean error over the training set.
00100 // For many classifiers, you may use nClasses==1 to
00101 // indicate a binary classifier
00102 double clsfy_knn_builder::build(clsfy_classifier_base& model,
00103                                 mbl_data_wrapper<vnl_vector<double> >& inputs,
00104                                 unsigned /* nClasses */,
00105                                 const vcl_vector<unsigned> &outputs) const
00106 {
00107   assert(model.is_class("clsfy_k_nearest_neighbour")); // equiv to dynamic_cast<> != 0
00108   assert(inputs.size()==outputs.size());
00109 
00110   clsfy_k_nearest_neighbour &knn = (clsfy_k_nearest_neighbour&) model;
00111 
00112   vcl_vector<vnl_vector<double> > vin(inputs.size());
00113 
00114   inputs.reset();
00115   unsigned i=0;
00116   do
00117   {
00118     vin[i++] = inputs.current();
00119   } while (inputs.next());
00120 
00121   assert(i==inputs.size());
00122 
00123   knn.set(vin, outputs);
00124   knn.set_k(k_);
00125   return clsfy_test_error(model, inputs, outputs);
00126 }
00127 
00128 //=======================================================================
00129 
00130 unsigned clsfy_knn_builder::k() const
00131 {
00132   return k_;
00133 }
00134 
00135 //=======================================================================
00136 
00137 void clsfy_knn_builder::set_k(unsigned k)
00138 {
00139   k_ = k;
00140 }
00141 
00142 //=======================================================================
00143 //: Create empty classifier
00144 // Caller is responsible for deletion
00145 clsfy_classifier_base* clsfy_knn_builder::new_classifier() const
00146 {
00147   return new clsfy_k_nearest_neighbour();
00148 }
00149 
00150 //=======================================================================
00151 //: Initialise the parameters from a text stream.
00152 // The next non-ws character in the stream should be a '{'
00153 // \verbatim
00154 // {
00155 //   k: 3  (default 1)
00156 // }
00157 // \endverbatim
00158 // \throw mbl_exception_parse_error if the parse fails.
00159 void clsfy_knn_builder::config(vcl_istream &as)
00160 {
00161  vcl_string s = mbl_parse_block(as);
00162 
00163   vcl_istringstream ss(s);
00164   mbl_read_props_type props = mbl_read_props_ws(ss);
00165 
00166   {
00167     k_= vul_string_atoi(props.get_optional_property("k", "1"));
00168   }
00169 
00170   // Check for unused props
00171   mbl_read_props_look_for_unused_props(
00172     "clsfy_knn_builder::config", props, mbl_read_props_type());
00173 }