- Timestamp:
- Feb 26, 2008, 11:09:04 PM (16 years ago)
- Location:
- trunk/yat/classifier
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/classifier/DataLookup2D.cc
r1134 r1169 38 38 39 39 DataLookup2D::DataLookup2D(const bool own) 40 : ref_count_(NULL)41 40 { 42 if (own)43 ref_count_ = new u_int(1);44 41 } 45 42 … … 48 45 const utility::Index& row, 49 46 const utility::Index& col) 50 : ref_count_(NULL)51 47 { 52 48 row_index_ = utility::Index(m.row_index_, row); … … 59 55 const utility::Index& index, 60 56 const bool row) 61 : ref_count_(NULL)62 57 { 63 58 if (row){ … … 75 70 const utility::Index& col, 76 71 const bool own) 77 : row_index_(row),column_index_(col) , ref_count_(NULL)72 : row_index_(row),column_index_(col) 78 73 { 79 if (own)80 ref_count_ = new u_int(1);81 74 } 82 75 … … 84 77 85 78 DataLookup2D::DataLookup2D(const DataLookup2D& mv) 86 : row_index_(mv.row_index_),column_index_(mv.column_index_), 87 ref_count_(NULL) 79 : row_index_(mv.row_index_),column_index_(mv.column_index_) 88 80 { 89 81 } … … 92 84 DataLookup2D::DataLookup2D(const size_t rows, const size_t columns) 93 85 : row_index_(std::vector<size_t>(rows,0)), 94 column_index_(std::vector<size_t>(columns,0)), ref_count_(NULL) 95 86 column_index_(std::vector<size_t>(columns,0)) 96 87 { 97 88 } … … 116 107 117 108 118 double DataLookup2D::weight(size_t i, size_t j) const119 {120 return 1.0;121 }122 123 124 109 const DataLookup2D& DataLookup2D::operator=(const DataLookup2D& other) 125 110 { … … 132 117 } 133 118 134 std::ostream& operator<<(std::ostream& s, const DataLookup2D& m)135 {136 s.setf(std::ios::dec);137 s.precision(12);138 for(size_t i=0, j=0; i<m.rows(); i++)139 for (j=0; j<m.columns(); j++) {140 s << m(i,j);141 if (j<m.columns()-1)142 s << s.fill();143 else if (i<m.rows()-1)144 s << "\n";145 }146 return s;147 }148 149 119 }}} // of namespace classifier, yat, and theplu -
trunk/yat/classifier/DataLookup2D.h
r1167 r1169 123 123 size_t rows(void) const; 124 124 125 /**126 \return data127 */128 virtual double weight(size_t i, size_t j) const;129 130 125 /// 131 126 /// Is lookup weighted? … … 156 151 utility::Index column_index_; 157 152 158 ///159 /// poiter telling how many owners to underlying data. NULL if160 /// this is not an owner.161 ///162 u_int* ref_count_;163 164 153 }; 165 154 -
trunk/yat/classifier/MatrixLookup.cc
r1168 r1169 36 36 37 37 MatrixLookup::MatrixLookup(const utility::Matrix& data, const bool own) 38 : DataLookup2D(own), data_( &data)38 : DataLookup2D(own), data_(MatrixP(&data, own)) 39 39 { 40 40 column_index_ = utility::Index(data.columns()); … … 47 47 const utility::Index& row, 48 48 const utility::Index& col) 49 : DataLookup2D(row,col), data_( &data)49 : DataLookup2D(row,col), data_(MatrixP(&data, false)) 50 50 { 51 51 } … … 56 56 const utility::Index& index, 57 57 const bool row) 58 : DataLookup2D(), data_( &data)58 : DataLookup2D(), data_(MatrixP(&data, false)) 59 59 { 60 60 if (row){ … … 73 73 : DataLookup2D(other), data_(other.data_) 74 74 { 75 ref_count_=other.ref_count_;76 if (ref_count_)77 ++(*ref_count_);78 75 } 79 76 … … 85 82 : DataLookup2D(other,row,col), data_(other.data_) 86 83 { 87 ref_count_=other.ref_count_;88 if (ref_count_)89 ++(*ref_count_);90 84 } 91 85 … … 96 90 : DataLookup2D(other,index,row), data_(other.data_) 97 91 { 98 ref_count_=other.ref_count_;99 if (ref_count_)100 ++(*ref_count_);101 102 92 } 103 93 … … 108 98 : DataLookup2D(rows,columns) 109 99 { 110 data_ = new utility::Matrix(1,1,value); 111 ref_count_= new u_int(1); 100 data_ = MatrixP(new utility::Matrix(1,1,value)); 112 101 } 113 102 … … 116 105 : DataLookup2D() 117 106 { 118 data_ = new utility::Matrix(is,sep); 119 ref_count_= new u_int(1); 107 data_ = MatrixP(new utility::Matrix(is,sep)); 120 108 row_index_ = utility::Index(data_->rows()); 121 109 column_index_ = utility::Index(data_->columns()); … … 125 113 MatrixLookup::~MatrixLookup(void) 126 114 { 127 if (ref_count_)128 if (!--(*ref_count_))129 delete data_;130 115 } 131 116 … … 191 176 { 192 177 if (this!=&other){ 193 if (ref_count_ && !--(*ref_count_))194 delete data_;195 178 DataLookup2D::operator=(other); 196 179 data_ = other.data_; 197 ref_count_=other.ref_count_;198 if (ref_count_)199 ++(*ref_count_);200 180 } 201 181 return *this; … … 203 183 204 184 185 std::ostream& operator<<(std::ostream& s, const MatrixLookup& m) 186 { 187 s.setf(std::ios::dec); 188 s.precision(12); 189 for(size_t i=0, j=0; i<m.rows(); i++) 190 for (j=0; j<m.columns(); j++) { 191 s << m(i,j); 192 if (j<m.columns()-1) 193 s << s.fill(); 194 else if (i<m.rows()-1) 195 s << "\n"; 196 } 197 return s; 198 } 199 205 200 206 201 -
trunk/yat/classifier/MatrixLookup.h
r1168 r1169 31 31 #include "yat/utility/Index.h" 32 32 #include "yat/utility/iterator_traits.h" 33 #include "yat/utility/SmartPtr.h" 33 34 #include "yat/utility/StrideIterator.h" 34 35 … … 295 296 friend class MatrixLookupWeighted; 296 297 297 const utility::Matrix* data_; 298 typedef utility::SmartPtr<const utility::Matrix> MatrixP; 299 MatrixP data_; 298 300 }; 299 301 302 /// 303 /// The output operator DataLookup2D 304 /// 305 std::ostream& operator<< (std::ostream& s, const MatrixLookup&); 306 300 307 }}} // of namespace classifier, yat, and theplu 301 308 -
trunk/yat/classifier/MatrixLookupWeighted.cc
r1168 r1169 38 38 const utility::Matrix& weights, 39 39 const bool own) 40 : DataLookup2D(own), data_(&data), weights_(&weights), 41 ref_count_weights_(NULL) 40 : data_(MatrixP(&data, own)), weights_(MatrixP(&weights, own)) 42 41 { 43 42 assert(data.rows()==weights.rows()); … … 49 48 50 49 MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data) 51 : DataLookup2D(), data_( &data)50 : DataLookup2D(), data_(MatrixP(&data, false)) 52 51 { 53 52 utility::Matrix weights; 54 53 utility::nan(*data_,weights); 55 weights_= new utility::Matrix(weights); 56 ref_count_weights_=new u_int(1); 54 weights_= MatrixP(new utility::Matrix(weights)); 57 55 row_index_ = utility::Index(data.rows()); 58 56 column_index_ = utility::Index(data.columns()); … … 63 61 : DataLookup2D(ml), data_(ml.data_) 64 62 { 65 weights_= new utility::Matrix(data_->rows(), data_->columns(), 1.0); 66 ref_count_weights_=new u_int(1); 67 ref_count_=ml.ref_count_; 68 if (ref_count_) 69 ++(*ref_count_); 70 63 weights_= MatrixP(new utility::Matrix(data_->rows(), data_->columns(),1.0)); 71 64 } 72 65 … … 76 69 const utility::Index& row, 77 70 const utility::Index& col) 78 : DataLookup2D(row,col), data_( &data), weights_(&weights),79 ref_count_weights_(NULL)71 : DataLookup2D(row,col), data_(MatrixP(new utility::Matrix(data), false)), 72 weights_(MatrixP(new utility::Matrix(weights), false)) 80 73 { 81 74 } … … 87 80 const utility::Index& index, 88 81 const bool row) 89 : DataLookup2D(), data_( &data), weights_(&weights),90 ref_count_weights_(NULL)82 : DataLookup2D(), data_(MatrixP(new utility::Matrix(data), false)), 83 weights_(MatrixP(new utility::Matrix(weights), false)) 91 84 { 92 85 assert(data.rows()==weights.rows()); … … 115 108 : DataLookup2D(other), data_(other.data_), weights_(other.weights_) 116 109 { 117 ref_count_ = other.ref_count_;118 if (ref_count_)119 ++(*ref_count_);120 ref_count_weights_ = other.ref_count_weights_;121 if (ref_count_weights_)122 ++(*ref_count_weights_);123 124 110 } 125 111 … … 131 117 : DataLookup2D(other,row,col), data_(other.data_), weights_(other.weights_) 132 118 { 133 ref_count_ = other.ref_count_;134 if (ref_count_)135 ++(*ref_count_);136 ref_count_weights_ = other.ref_count_weights_;137 if (ref_count_weights_)138 ++(*ref_count_weights_);139 119 } 140 120 … … 147 127 weights_(other.weights_) 148 128 { 149 ref_count_ = other.ref_count_;150 if (ref_count_)151 ++(*ref_count_);152 ref_count_weights_ = other.ref_count_weights_;153 if (ref_count_weights_)154 ++(*ref_count_weights_);155 156 129 } 157 130 … … 162 135 const double value, 163 136 const double weight) 164 : DataLookup2D(rows,columns) 165 { 166 data_ = new utility::Matrix(1,1,value); 167 ref_count_=new u_int(1); 168 weights_ = new utility::Matrix(1,1,weight); 169 ref_count_weights_=new u_int(1); 137 : DataLookup2D(rows,columns), 138 data_(MatrixP(new utility::Matrix(1,1,value))), 139 weights_(MatrixP(new utility::Matrix(1,1,weight))) 140 { 170 141 } 171 142 … … 174 145 : DataLookup2D() 175 146 { 176 data_ = new utility::Matrix(is,sep); 177 ref_count_=new u_int(1); 147 data_ = MatrixP(new utility::Matrix(is,sep)); 178 148 row_index_ = utility::Index(data_->rows()); 179 149 column_index_ = utility::Index(data_->columns()); 180 150 utility::Matrix weights; 181 151 utility::nan(*data_,weights); 182 weights_= new utility::Matrix(weights);183 ref_count_weights_=new u_int(1);152 // Peter, should be possible to avoid this copying 153 weights_= MatrixP(new utility::Matrix(weights)); 184 154 } 185 155 … … 187 157 MatrixLookupWeighted::~MatrixLookupWeighted(void) 188 158 { 189 if (ref_count_)190 if (!--(*ref_count_))191 delete data_;192 if (ref_count_weights_)193 if (!--(*ref_count_weights_))194 delete weights_;195 159 } 196 160 … … 274 238 { 275 239 if (this!=&other){ 276 if (ref_count_ && !--(*ref_count_))277 delete data_;278 if (ref_count_weights_ && !--(*ref_count_weights_))279 delete weights_;280 240 DataLookup2D::operator=(other); 281 241 data_ = other.data_; 282 ref_count_=other.ref_count_;283 if (ref_count_)284 ++(*ref_count_);285 242 weights_ = other.weights_; 286 ref_count_weights_ = other.ref_count_weights_;287 if (ref_count_weights_)288 ++(*ref_count_weights_);289 243 } 290 244 return *this; … … 292 246 293 247 294 std::ostream& operator<<(std::ostream& s, const MatrixLookupWeighted& m)295 {296 s.setf(std::ios::dec);297 s.precision(12);298 for(size_t i=0, j=0; i<m.rows(); i++)299 for (j=0; j<m.columns(); j++) {300 if (m.weight(i,j))301 s << m.data(i,j);302 if (j<m.columns()-1)303 s << s.fill();304 else if (i<m.rows()-1)305 s << "\n";306 }307 return s;308 }309 310 311 312 248 }}} // of namespace classifier, yat, and theplu -
trunk/yat/classifier/MatrixLookupWeighted.h
r1168 r1169 29 29 #include "yat/utility/Container2DIterator.h" 30 30 #include "yat/utility/IteratorPolicy.h" 31 #include "yat/utility/SmartPtr.h" 31 32 #include "yat/utility/StrideIterator.h" 32 33 … … 331 332 332 333 private: 333 const utility::Matrix* data_;334 const utility::Matrix* weights_;335 u_int* ref_count_weights_;334 typedef utility::SmartPtr<const utility::Matrix> MatrixP; 335 MatrixP data_; 336 MatrixP weights_; 336 337 }; 337 338 -
trunk/yat/classifier/NBC.h
r1160 r1169 89 89 90 90 \f$ P_j = \frac{1}{Z}\prod_i{\frac{1}{\sqrt{2\pi\sigma_i^2}}} 91 \exp(\frac{ w_i(x_i-\mu_i)^2}{\sigma_i^2})\f$, where \f$ \mu_i91 \exp(\frac{(x_i-\mu_i)^2}{\sigma_i^2})\f$, where \f$ \mu_i 92 92 \f$ and \f$ \sigma_i^2 \f$ are the estimated mean and variance, 93 93 respectively. If a \f$ \sigma_i \f$ could not be estimated … … 95 95 words, that feature is ignored for the prediction of that 96 96 particular class. Z is chosen such that total probability, \f$ 97 \sum P_j \f$, equals unity. If \a data is a MatrixLookup is 98 equivalent to using all weight equal to unity. 97 \sum P_j \f$, equals unity. 99 98 */ 100 99 void predict(const MatrixLookup& data, utility::Matrix& res) const; 101 100 102 101 /** 103 @see above 102 Each sample (column) in \a data is predicted and predictions 103 are returned in the corresponding column in passed \a res. Each 104 row in \a res corresponds to a class. The prediction is the 105 estimated probability that sample belong to class \f$ j \f$ 104 106 */ 105 107 void predict(const MatrixLookupWeighted& data, utility::Matrix& res) const;
Note: See TracChangeset
for help on using the changeset viewer.