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