Changeset 767 for trunk/yat/classifier/NBC.cc
 Timestamp:
 Feb 22, 2007, 4:14:40 PM (15 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/classifier/NBC.cc
r722 r767 30 30 #include "yat/utility/matrix.h" 31 31 32 #include <cassert> 32 33 #include <vector> 33 34 … … 51 52 52 53 53 54 54 const DataLookup2D& NBC::data(void) const 55 { 55 56 return data_; 56 57 } 57 58 58 59 … … 83 84 const MatrixLookupWeighted& data = 84 85 dynamic_cast<const MatrixLookupWeighted&>(data_); 85 86 aver[target_(j)].add(data.data(i,j), data.weight(i,j)); 86 87 } 87 88 else … … 90 91 for (size_t j=0; target_.nof_classes(); ++j){ 91 92 centroids_(i,j) = aver[j].mean(); 92 sigma _(i,j) = aver[j].variance();93 sigma2_(i,j) = aver[j].variance(); 93 94 } 94 95 } … … 103 104 std::cerr << "NBC::predict not implemented\n"; 104 105 exit(1); 106 assert(data_.rows()==input.rows()); 107 108 std::log(sigma_(i,c)) + 109 110 prediction = utility::matrix(centroids_.columns(),input.columns()); 111 for (size_t c=0; c<centroid_.columns(); ++c) { 112 double sum_ln_sigma=0; 113 for (size_t i=0; i<sigma2_.rows(); ++i) 114 sum_ln_sigma += log(sigma2_(i,c)); 115 sum_ln_sigma /= 2; 116 117 for (size_t s=0; s<input.columns(); ++s) { 118 // lnp = sum{ln(sigma_i)} + sum{(x_im_i)^2/(2sigma_i)} 119 prediction(c,s) = sum_ln_sigma; 120 for (size_t i=0; i<input.columns(); ++i) { 121 prediction(c,s) += std::pow(input(i,s)mean_(i,c),2)/sigma2_(i,c); 122 } 123 } 124 } 125 // exponentiate and normalize 105 126 } 106 127
Note: See TracChangeset
for help on using the changeset viewer.