Changeset 813
- Timestamp:
- Mar 16, 2007, 8:30:02 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/Makefile.am
r806 r813 30 30 ensemble_test feature_selection_test fileutil_test inputranker_test \ 31 31 kernel_test kernel_lookup_test matrix_test matrix_lookup_test \ 32 nbc_test \ 32 33 ncc_test nni_test pca_test regression_test rnd_test roc_test \ 33 34 score_test \ … … 57 58 matrix_test_SOURCES = matrix_test.cc 58 59 matrix_lookup_test_SOURCES = matrix_lookup_test.cc 60 nbc_test_SOURCES = nbc_test.cc 59 61 ncc_test_SOURCES = ncc_test.cc 60 62 nni_test_SOURCES = nni_test.cc -
trunk/yat/classifier/NBC.cc
r812 r813 90 90 aver[target_(j)].add(data_(i,j),1.0); 91 91 } 92 for (size_t j=0; target_.nof_classes(); ++j){ 92 assert(centroids_.columns()==target_.nof_classes()); 93 for (size_t j=0; j<target_.nof_classes(); ++j){ 94 assert(i<centroids_.rows()); 95 assert(j<centroids_.columns()); 93 96 centroids_(i,j) = aver[j].mean(); 97 assert(i<sigma2_.rows()); 98 assert(j<sigma2_.columns()); 94 99 sigma2_(i,j) = aver[j].variance(); 95 100 } … … 104 109 { 105 110 assert(data_.rows()==x.rows()); 111 assert(x.rows()==sigma2_.rows()); 112 assert(x.rows()==centroids_.rows()); 113 114 const MatrixLookupWeighted* w = 115 dynamic_cast<const MatrixLookupWeighted*>(&x); 106 116 107 117 // each row in prediction corresponds to a sample label (class) 108 118 prediction.resize(centroids_.columns(), x.columns(), 0); 109 119 // first calculate -lnP = sum sigma_i + (x_i-m_i)^2/2sigma_i^2 110 for (size_t label=0; label< prediction.columns(); ++label) {120 for (size_t label=0; label<centroids_.columns(); ++label) { 111 121 double sum_ln_sigma=0; 112 for (size_t i=0; i<x.rows(); ++i) 122 assert(label<sigma2_.columns()); 123 for (size_t i=0; i<x.rows(); ++i) { 124 assert(i<sigma2_.rows()); 113 125 sum_ln_sigma += std::log(sigma2_(i, label)); 126 } 114 127 sum_ln_sigma /= 2; // taking sum of log(sigma) not sigma2 115 128 for (size_t sample=0; sample<prediction.rows(); ++sample) { 116 129 for (size_t i=0; i<x.rows(); ++i) { 117 prediction(label, sample) += 118 std::pow(x(i, label)-centroids_(i, label),2)/sigma2_(i, label); 130 // weighted calculation 131 if (w){ 132 // taking care of NaN 133 if (w->weight(i, label)){ 134 prediction(label, sample) += w->weight(i, label)* 135 std::pow(w->data(i, label)-centroids_(i, label),2)/ 136 sigma2_(i, label); 137 } 138 } 139 // no weights 140 else { 141 prediction(label, sample) += 142 std::pow(x(i, label)-centroids_(i, label),2)/sigma2_(i, label); 143 } 119 144 } 120 145 } -
trunk/yat/classifier/NBC.h
r812 r813 83 83 84 84 /** 85 For each sample, calculate the probabilities the sample belong 86 to the corresponding class. 85 Each sample (column) in \a data is predicted and predictions 86 are returned in the corresponding column in passed \a res. Each 87 row in \a res corresponds to a class. The prediction is the 88 estimated probability that sample belong to class \f$ j \f$ 89 90 \f$ P_j = \frac{1}{Z}\prod_i{\frac{1}{\sigma_i}} 91 \exp(\frac{w_i(x_i-\mu_i)^2}{\sigma_i^2})\f$, where \f$ \mu_i 92 \f$ and \f$ \sigma_i^2 \f$ are the estimated mean and variance, 93 respectively. If \a data is a MatrixLookup is equivalent to 94 using all weight equal to unity. 87 95 */ 88 96 void predict(const DataLookup2D& data, utility::matrix& res) const; -
trunk/yat/utility/matrix.cc
r810 r813 379 379 { 380 380 assert(m_); 381 assert(row<rows()); 382 assert(column<columns()); 381 383 double* d=gsl_matrix_ptr(m_, row, column); 382 384 if (!d) … … 388 390 const double& matrix::operator()(size_t row, size_t column) const 389 391 { 392 assert(row<rows()); 393 assert(column<columns()); 390 394 const double* d=gsl_matrix_const_ptr(proxy_m_, row, column); 391 395 if (!d)
Note: See TracChangeset
for help on using the changeset viewer.