Changeset 1587
- Timestamp:
- Oct 17, 2008, 5:31:42 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/NEWS
r1488 r1587 5 5 Version 0.5 (released DATE) 6 6 7 - MatrixLookupWeighted(const MatrixLookup&) modified (ticket:396) 7 8 - Upgraded to GLPv3 8 9 - Option::parse and OptionArg::do_parse takes references (ticket:418) -
trunk/test/Makefile.am
r1542 r1587 37 37 index_test inputranker_test \ 38 38 iterator_test kernel_lookup_test kernel_test \ 39 knn_test large_file_test matrix_lookup_test matrix_test \ 39 knn_test large_file_test matrix_lookup_test \ 40 matrix_lookup_weighted_test matrix_test \ 40 41 matrix_weighted_test nbc_test \ 41 42 ncc_test nni_test normalization_test pca_test \ … … 75 76 knn_test_SOURCES = knn_test.cc 76 77 large_file_test_SOURCES = large_file_test.cc 78 matrix_lookup_test_SOURCES = matrix_lookup_test.cc 79 matrix_lookup_weighted_test_SOURCES = matrix_lookup_weighted_test.cc 77 80 matrix_test_SOURCES = matrix_test.cc 78 81 matrix_weighted_test_SOURCES = matrix_weighted_test.cc 79 matrix_lookup_test_SOURCES = matrix_lookup_test.cc80 82 nbc_test_SOURCES = nbc_test.cc 81 83 ncc_test_SOURCES = ncc_test.cc -
trunk/test/ncc_test.cc
r1487 r1587 28 28 #include "yat/classifier/NCC.h" 29 29 #include "yat/classifier/Target.h" 30 #include "yat/utility/DataIterator.h" 31 #include "yat/utility/DataWeight.h" 30 32 #include "yat/utility/Matrix.h" 33 #include "yat/utility/MatrixWeighted.h" 31 34 #include "yat/statistics/EuclideanDistance.h" 32 35 #include "yat/statistics/PearsonDistance.h" … … 100 103 ////////////////////////////////////////////////////////////////////////// 101 104 suite.err() << "test of predictions using unweighted training and weighted test data\n"; 102 utility::Matrix weights1(3,4,1.0); 103 weights1(0,0)=weights1(1,1)=weights1(2,2)=weights1(1,3)=0.0; 104 classifier::MatrixLookupWeighted mlw1(data1,weights1); 105 utility::MatrixWeighted xw11(3,4); 106 xw11(0,0)=xw11(1,1)=xw11(2,2)=xw11(1,3)=utility::DataWeight(0,0); 107 std::copy(data1.begin(), data1.end(), utility::data_iterator(xw11.begin())); 108 classifier::MatrixLookupWeighted mlw1(xw11); 109 //classifier::MatrixLookupWeighted mlw1(data1,weights1); 105 110 ncc1.predict(mlw1,prediction1); 106 111 result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=sqrt(15.0); … … 116 121 ////////////////////////////////////////////////////////////////////////// 117 122 suite.err() << "test of predictions using nan centroids and unweighted test data\n"; 118 utility::Matrix weights2(3,4,1.0); 119 weights2(1,0)=weights2(1,1)=0.0; 120 classifier::MatrixLookupWeighted mlw2(data1,weights2); 123 utility::MatrixWeighted xw12(3,4); 124 xw12(1,0)=xw12(1,1)=utility::DataWeight(0,0); 125 std::copy(data1.begin(), data1.end(), utility::data_iterator(xw12.begin())); 126 classifier::MatrixLookupWeighted mlw2(xw12); 127 //classifier::MatrixLookupWeighted mlw2(data1,weights2); 121 128 classifier::NCC<statistics::EuclideanDistance> ncc2; 122 129 ncc2.train(mlw2,target1); … … 139 146 suite.err() << "test of predictions using nan centroids and weighted test data\n"; 140 147 suite.err() << "... using EuclideanDistance" << std::endl; 141 weights1(0,0)=weights1(2,0)=0;148 xw11(0,0).weight() = xw11(2,0).weight()=0; 142 149 classifier::NCC<statistics::EuclideanDistance> ncc3; 143 150 ncc3.train(mlw2,target1); … … 153 160 suite.equal(prediction1(0,2),sqrt(27.0)) )) { 154 161 suite.add(false); 162 if (!std::isnan(prediction1(0,0))) 163 suite.err() << "prediction1(0,0): " << prediction1(0,0) << " " 164 << "expected NaN\n"; 155 165 suite.err() << "Test failed: predictions incorrect" << std::endl; 156 166 } … … 183 193 ind[1]=3; 184 194 classifier::Target target2(target1,utility::Index(ind)); 185 classifier::MatrixLookupWeighted mlw3( data1,weights2,195 classifier::MatrixLookupWeighted mlw3(xw12, 186 196 utility::Index(data1.rows()), 187 197 utility::Index(ind)); … … 204 214 suite.err() << "test with Sorlie data\n"; 205 215 std::ifstream is(test::filename("data/sorlie_centroid_data.txt").c_str()); 206 utility::Matrix data(is,'\t');216 utility::MatrixWeighted data_weight(is,'\t'); 207 217 is.close(); 208 218 … … 211 221 is.close(); 212 222 213 // Generate weight matrix with 0 for missing values and 1 for others. 214 utility::Matrix weights(data.rows(),data.columns(),0.0); 215 utility::nan(data,weights); 216 217 classifier::MatrixLookupWeighted dataviewweighted(data,weights); 223 classifier::MatrixLookupWeighted dataviewweighted(data_weight); 218 224 classifier::NCC<statistics::PearsonDistance> ncc; 219 225 suite.err() << "training...\n"; … … 272 278 data1(i,3)=i+3; 273 279 } 280 utility::MatrixWeighted xw(data1); 274 281 std::vector<std::string> vec1(4, "pos"); 275 282 vec1[0]="neg"; … … 281 288 utility::Matrix result1(2,4); 282 289 283 utility::Matrix weights2(3,4,1.0);284 weights2(1,0)=weights2(1,1)=0.0;285 286 classifier::MatrixLookupWeighted mlw2( data1,weights2);290 xw(1,0).weight()=xw(1,1).weight()=0.0; 291 292 293 classifier::MatrixLookupWeighted mlw2(xw); 287 294 classifier::NCC<statistics::EuclideanDistance> ncc2; 288 295 ncc2.train(mlw2,target1); -
trunk/yat/classifier/MatrixLookupWeighted.cc
r1581 r1587 41 41 const utility::Index& rows, 42 42 const utility::Index& columns) 43 : column_index_(columns), row_index_(rows) 44 { 45 utility::Matrix* data = new utility::Matrix(m.rows(), m.columns()); 46 utility::Matrix* weight = new utility::Matrix(m.rows(), m.columns()); 47 for (size_t i=0; i<m.rows(); ++i) 48 for (size_t j=0; j<m.columns(); ++j) { 49 (*data)(i,j) = m(i,j).data(); 50 (*weight)(i,j) = m(i,j).weight(); 51 } 52 // smart pointers are taking ownership 53 data_ = MatrixP(data); 54 weights_ = MatrixP(weight); 43 : column_index_(columns), data_(MatrixWP(&m,false)), row_index_(rows) 44 { 45 assert(validate()); 55 46 } 56 47 … … 59 50 bool owner) 60 51 : column_index_(utility::Index(m.columns())), 52 data_(MatrixWP(&m,owner)), 61 53 row_index_(utility::Index(m.rows())) 62 54 { 63 // Peter, remember to take care of ownership (but for now leave a leak)64 65 utility::Matrix* data = new utility::Matrix(m.rows(), m.columns());66 utility::Matrix* weight = new utility::Matrix(m.rows(), m.columns());67 for (size_t i=0; i<m.rows(); ++i)68 for (size_t j=0; j<m.columns(); ++j) {69 (*data)(i,j) = m(i,j).data();70 (*weight)(i,j) = m(i,j).weight();71 }72 // smart pointers are taking ownership73 data_ = MatrixP(data);74 weights_ = MatrixP(weight);75 }76 77 78 MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,79 const utility::Matrix& weights,80 const bool own)81 : data_(MatrixP(&data, own)), weights_(MatrixP(&weights, own))82 {83 assert(data.rows()==weights.rows());84 assert(data.columns()==weights.columns());85 row_index_ = utility::Index(data.rows());86 column_index_ = utility::Index(data.columns());87 55 assert(validate()); 88 56 } … … 90 58 91 59 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& ml) 92 : column_index_(ml.column_index_), data_(ml.data_), 93 row_index_(ml.row_index_), 94 weights_(MatrixP(new utility::Matrix(data_->rows(),data_->columns(),1.0))) 60 : column_index_(ml.column_index_), 61 row_index_(ml.row_index_) 95 62 { 96 assert(validate()); 97 } 98 99 100 MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data, 101 const utility::Matrix& weights, 102 const utility::Index& row, 103 const utility::Index& col) 104 : column_index_(col), data_(MatrixP(&data, false)), 105 row_index_(row), weights_(MatrixP(&weights, false)) 106 { 107 assert(validate()); 108 } 109 110 111 112 /* 113 MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data, 114 const utility::Matrix& weights, 115 const utility::Index& index, 116 const bool row) 117 : data_(MatrixP(new utility::Matrix(data), false)), 118 weights_(MatrixP(new utility::Matrix(weights), false)) 119 { 120 assert(data.rows()==weights.rows()); 121 assert(data.columns()==weights.columns()); 122 if (row){ 123 row_index_=index; 124 column_index_ = utility::Index(data.columns()); 125 } 126 else{ 127 column_index_=index; 128 row_index_ = utility::Index(data.rows()); 129 } 130 assert(validate()); 131 } 132 */ 133 134 /* 135 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& dv, 136 const MatrixLookup& wv) 137 : DataLookup2D(dv), data_(dv.data_), weights_(dv.data_) 138 { 139 } 140 */ 141 63 utility::MatrixWeighted* mw = new utility::MatrixWeighted(*ml.data_); 64 data_ = MatrixWP(mw); 65 assert(validate()); 66 } 67 142 68 143 69 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other) 144 70 : column_index_(other.column_index_), data_(other.data_), 145 row_index_(other.row_index_) , weights_(other.weights_)71 row_index_(other.row_index_) 146 72 { 147 73 assert(validate()); … … 154 80 const utility::Index& col) 155 81 : column_index_(utility::Index(other.column_index_, col)), 156 data_(other.data_), row_index_(utility::Index(other.row_index_, row)), 157 weights_(other.weights_) 82 data_(other.data_), row_index_(utility::Index(other.row_index_, row)) 158 83 { 159 84 assert(validate()); … … 165 90 const utility::Index& index, 166 91 bool row) 167 : data_(other.data_), 168 weights_(other.weights_) 92 : data_(other.data_) 169 93 { 170 94 if (row){ … … 186 110 const double weight) 187 111 : column_index_(utility::Index(std::vector<size_t>(rows, 0))), 188 data_(MatrixP(new utility::Matrix(1,1,value))), 189 row_index_(utility::Index(std::vector<size_t>(columns, 0))), 190 weights_(MatrixP(new utility::Matrix(1,1,weight))) 112 data_(MatrixWP(new utility::MatrixWeighted(1,1,value, weight))), 113 row_index_(utility::Index(std::vector<size_t>(columns, 0))) 191 114 { 192 115 assert(validate()); … … 195 118 196 119 MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep) 197 : data_(Matrix P(new utility::Matrix(is,sep)))120 : data_(MatrixWP(new utility::MatrixWeighted(is,sep))) 198 121 { 199 122 column_index_ = utility::Index(data_->columns()); 200 123 row_index_ = utility::Index(data_->rows()); 201 utility::Matrix weights;202 utility::nan(*data_,weights);203 // Peter, should be possible to avoid this copying204 weights_= MatrixP(new utility::Matrix(weights));205 124 assert(validate()); 206 125 } … … 247 166 assert(row_index_[row]<data_->rows()); 248 167 assert(column_index_[column]<data_->columns()); 249 return (* data_)(row_index_[row], column_index_[column]);168 return (*this)(row, column).data(); 250 169 } 251 170 … … 290 209 if (column_index_[i]>=data_->columns()) 291 210 return false; 292 for (size_t i=0; i<row_index_.size(); ++i)293 if (row_index_[i]>=weights_->rows())294 return false;295 for (size_t i=0; i<column_index_.size(); ++i)296 if (column_index_[i]>=weights_->columns())297 return false;298 211 return true; 299 212 } … … 304 217 assert(row<rows()); 305 218 assert(column<columns()); 306 assert(row_index_[row]<weights_->rows()); 307 assert(column_index_[column]<weights_->columns()); 308 return (*weights_)(row_index_[row], column_index_[column]); 219 return (*this)(row, column).weight(); 309 220 } 310 221 … … 321 232 MatrixLookupWeighted::operator()(const size_t row, const size_t column) const 322 233 { 323 return utility::DataWeight(data(row, column), weight(row,column));234 return (*data_)(row_index_[row], column_index_[column]); 324 235 } 325 236 … … 333 244 row_index_=other.row_index_; 334 245 data_ = other.data_; 335 weights_ = other.weights_;336 246 } 337 247 assert(validate()); -
trunk/yat/classifier/MatrixLookupWeighted.h
r1581 r1587 71 71 class MatrixLookupWeighted 72 72 { 73 74 73 public: 75 74 /** … … 119 118 bool owner=false); 120 119 121 ///122 /// Constructor creating a lookup into the entire \a matrix and \a123 /// weights.124 ///125 /// @note If @a matrix or @a weights goes out of scope or is126 /// deleted, the MatrixLookupWeighted becomes invalid and the127 /// result of further use is undefined.128 ///129 /// \deprecated130 MatrixLookupWeighted(const utility::Matrix& matrix,131 const utility::Matrix& weights,132 const bool owner=false);// YAT_DEPRECATE;133 134 120 /** 135 121 Constructor creating a MatrixLookupWeighted from a MatrixLookup. A … … 138 124 \note no check for nan is performed. 139 125 140 @note If underlying utility::Matrix goes out of scope or 141 is deleted, the MatrixLookupWeighted becomes invalid and the 142 result of further use is undefined. 126 @note from yat 0.5 data is copied and further modifications in 127 \a matrix will not be reflected in MatrixLookupWeighted. 143 128 */ 144 129 explicit MatrixLookupWeighted(const MatrixLookup& matrix); 145 130 146 147 ///148 /// Constructor creating a lookup into a sub-matrix of @a matrix.149 /// The @a row and @a column define what sub-matrix to look into,150 /// in other words, the created MatrixLookupWeighted will fullfill151 /// the following:152 /// MatrixLookupWeighted(i,j)=matrix(row[i],column[j])153 /// weights(row[i],column[j]). This also means that number of154 /// rows in created MatrixLookupWeighted is equal to size of155 /// @a row, and number of columns is equal to size of @a column.156 ///157 /// @note If @a matrix or @a weights goes out of scope or is deleted, the158 /// MatrixLookupWeighted becomes invalid and the result of further use is159 /// undefined.160 ///161 /// \deprecated162 MatrixLookupWeighted(const utility::Matrix& matrix,163 const utility::Matrix& weights,164 const utility::Index& row,165 const utility::Index& column);// YAT_DEPRECATE;166 167 ///168 /// Constructor creating a lookup into a sub-matrix of @a matrix.169 ///170 /// If @a row_vectors is true the new MatrixLookupWeighted will be171 /// consist of the row vectors defined by @a index. This means172 /// that the created MatrixLookupWeighted will fullfill:173 /// MatrixLookupWeighted(i,j)=matrix(i,index[j])*weights(i,index[j])174 ///175 ///176 /// If @a row_vectors is false the new MatrixLookupWeighted will be consist177 /// of the rolumn vectors defined by @a index. This means that the178 /// created MatrixLookupWeighted will fullfill:179 ///180 ///181 /// @note If @a matrix or @a weights goes out of scope or is182 /// deleted, the MatrixLookupWeighted becomes invalid and the183 /// result of further use is undefined.184 ///185 /*186 MatrixLookupWeighted(const utility::Matrix& matrix,187 const utility::Matrix& weights,188 const utility::Index& index,189 const bool row_vectors);190 */191 131 192 132 /// … … 266 206 /// matrix. 267 207 /// 268 /// @see matrix(istream&) for details.208 /// @see utility::MatrixWeighted(istream&) for details. 269 209 /// 270 210 MatrixLookupWeighted(std::istream&, char sep='\0'); … … 356 296 357 297 private: 358 typedef utility::SmartPtr<const utility::Matrix > MatrixP;298 typedef utility::SmartPtr<const utility::MatrixWeighted> MatrixWP; 359 299 utility::Index column_index_; 360 Matrix P data_;300 MatrixWP data_; 361 301 utility::Index row_index_; 362 MatrixP weights_;363 302 364 303 // for assertions
Note: See TracChangeset
for help on using the changeset viewer.