Ignore:
Timestamp:
Dec 16, 2012, 1:57:13 PM (9 years ago)
Author:
Peter
Message:

avoid creating a MatrixLookupWeighted? from a Matrix as it involves copying the Matrix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/NBC.cc

    r2881 r2909  
    2929#include "yat/statistics/AveragerWeighted.h"
    3030#include "yat/utility/Matrix.h"
     31#include "yat/utility/WeightedIterator.h"
    3132
    3233#include <cassert>
     
    167168    /// problems when calculating P = exp(- -lnP), we centralize matrix
    168169    /// by adding a constant.
    169     // lookup of prediction with zero weights for NaNs
    170     MatrixLookupWeighted mlw(prediction);
     170    utility::Matrix weights;
     171    // create zero/unity weight matrix (w=0 if NaN)
     172    nan(prediction, weights);
     173    using utility::weighted_iterator;
    171174    statistics::AveragerWeighted a;
    172     add(a, mlw.begin(), mlw.end());
     175    add(a, weighted_iterator(prediction.begin(), weights.begin()),
     176        weighted_iterator(prediction.end(), weights.end()));
    173177    prediction -= a.mean();
    174    
     178
    175179    // exponentiate
    176180    for (size_t i=0; i<prediction.rows(); ++i)
    177181      for (size_t j=0; j<prediction.columns(); ++j)
    178182        prediction(i,j) = std::exp(prediction(i,j));
    179    
     183
    180184    // normalize each row (label) to sum up to unity (probability)
    181185    for (size_t i=0; i<prediction.rows(); ++i){
    182186      // calculate sum of row ignoring NaNs
    183187      statistics::AveragerWeighted a;
    184       add(a, mlw.begin_row(i), mlw.end_row(i));
     188      add(a, weighted_iterator(prediction.begin_row(i), weights.begin_row(i)),
     189          weighted_iterator(prediction.end_row(i), weights.end_row(i)));
    185190      prediction.row_view(i) *= 1.0/a.sum_wx();
    186191    }
Note: See TracChangeset for help on using the changeset viewer.