00001
00002
00003 #include "clsfy_binary_hyperplane.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <vcl_string.h>
00013 #include <vcl_iostream.h>
00014 #include <vcl_vector.h>
00015 #include <vcl_cmath.h>
00016 #include <vsl/vsl_binary_io.h>
00017 #include <vnl/io/vnl_io_matrix.h>
00018 #include <vnl/io/vnl_io_vector.h>
00019
00020
00021
00022
00023
00024 unsigned clsfy_binary_hyperplane::classify(const vnl_vector<double> &input) const
00025 {
00026 if (dot_product(input, weights_) - bias_ > 0.0) return 1;
00027 else return 0;
00028 }
00029
00030
00031
00032
00033
00034
00035 void clsfy_binary_hyperplane::class_probabilities(vcl_vector<double> &outputs,
00036 const vnl_vector<double> &input) const
00037 {
00038 outputs.resize(1);
00039 outputs[0] = 1.0 / (1.0 + vcl_exp(-log_l(input)));
00040 }
00041
00042
00043
00044
00045
00046
00047 double clsfy_binary_hyperplane::log_l(const vnl_vector<double> &input) const
00048 {
00049 return dot_product(input, weights_) - bias_;
00050 }
00051
00052
00053
00054
00055
00056 vcl_string clsfy_binary_hyperplane::is_a() const
00057 {
00058 return vcl_string("clsfy_binary_hyperplane");
00059 }
00060
00061
00062
00063 bool clsfy_binary_hyperplane::is_class(vcl_string const& s) const
00064 {
00065 return s == clsfy_binary_hyperplane::is_a() || clsfy_classifier_base::is_class(s);
00066 }
00067
00068
00069
00070
00071 void clsfy_binary_hyperplane::print_summary(vcl_ostream& os) const
00072 {
00073 os << "bias: " << bias_ << " weights: ";
00074 vsl_print_summary(os, weights_);
00075 }
00076
00077
00078
00079 short clsfy_binary_hyperplane::version_no() const
00080 {
00081 return 1;
00082 }
00083
00084
00085
00086 void clsfy_binary_hyperplane::b_write(vsl_b_ostream& bfs) const
00087 {
00088 vsl_b_write(bfs,version_no());
00089 vsl_b_write(bfs,weights_);
00090 vsl_b_write(bfs,bias_);
00091 }
00092
00093
00094
00095 void clsfy_binary_hyperplane::b_read(vsl_b_istream& bfs)
00096 {
00097 if (!bfs) return;
00098
00099 short version;
00100 vsl_b_read(bfs,version);
00101 switch (version)
00102 {
00103 case (1):
00104 vsl_b_read(bfs,weights_);
00105 vsl_b_read(bfs,bias_);
00106 break;
00107 default:
00108 vcl_cerr << "I/O ERROR: clsfy_binary_hyperplane::b_read(vsl_b_istream&)\n"
00109 << " Unknown version number "<< version << "\n";
00110 bfs.is().clear(vcl_ios::badbit);
00111 }
00112 }
00113