Changeset 1241
 Timestamp:
 Mar 16, 2008, 6:47:42 AM (14 years ago)
 Location:
 trunk/test
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/nbc_test.cc
r1157 r1241 22 22 */ 23 23 24 #include "Suite.h" 25 24 26 #include "yat/classifier/MatrixLookup.h" 25 27 #include "yat/classifier/MatrixLookupWeighted.h" … … 35 37 using namespace theplu::yat; 36 38 37 int main( const int argc,constchar* argv[])39 int main(int argc, char* argv[]) 38 40 { 39 40 std::ostream* error; 41 if (argc>1 && argv[1]==std::string("v")) 42 error = &std::cerr; 43 else { 44 error = new std::ofstream("/dev/null"); 45 if (argc>1) 46 *error << "nbc_test v : for printing extra information\n"; 47 } 48 *error << "testing ncc" << std::endl; 49 bool ok = true; 41 test::Suite suite(argc, argv); 42 suite.err() << "testing ncc" << std::endl; 50 43 51 44 std::ifstream is("data/nm_data_centralized.txt"); … … 58 51 is.close(); 59 52 60 *error<< "Constructing NBC" << std::endl;53 suite.err() << "Constructing NBC" << std::endl; 61 54 classifier::NBC nbc; 62 *error<< "Training NBC" << std::endl;55 suite.err() << "Training NBC" << std::endl; 63 56 nbc.train(data,target); 64 57 utility::Matrix res; 65 *error<< "Predicting" << std::endl;58 suite.err() << "Predicting" << std::endl; 66 59 nbc.predict(data, res); 67 60 68 69 if(ok) 70 *error << "OK" << std::endl; 71 else 72 *error << "test failed" << std::endl; 73 74 if (error!=&std::cerr) 75 delete error; 76 77 if(ok) 78 return 0; 79 return 1; 80 61 return suite.return_value(); 81 62 } 
trunk/test/ncc_test.cc
r1210 r1241 22 22 */ 23 23 24 #include " yat/classifier/Kernel_MEV.h"25 #include "yat/classifier/KernelLookup.h" 24 #include "Suite.h" 25 26 26 #include "yat/classifier/MatrixLookup.h" 27 27 #include "yat/classifier/MatrixLookupWeighted.h" 28 28 #include "yat/classifier/NCC.h" 29 #include "yat/classifier/PolynomialKernelFunction.h"30 29 #include "yat/classifier/Target.h" 31 30 #include "yat/utility/Matrix.h" … … 45 44 using namespace theplu::yat; 46 45 47 double deviation(const utility::Matrix& a, const utility::Matrix& b) { 48 double sl=0; 49 for (size_t i=0; i<a.rows(); i++){ 50 for (size_t j=0; j<a.columns(); j++){ 51 sl += std::abs(a(i,j)b(i,j)); 52 } 53 } 54 sl /= (a.columns()*a.rows()); 55 return sl; 56 } 57 58 int main(const int argc,const char* argv[]) 46 int main(int argc,char* argv[]) 59 47 { 60 61 std::ostream* error; 62 if (argc>1 && argv[1]==std::string("v")) 63 error = &std::cerr; 64 else { 65 error = new std::ofstream("/dev/null"); 66 if (argc>1) 67 std::cout << "ncc_test v : for printing extra information\n"; 68 } 69 *error << "testing ncc" << std::endl; 70 bool ok = true; 48 test::Suite suite(argc, argv); 49 suite.err() << "testing ncc" << std::endl; 71 50 72 51 ///////////////////////////////////////////// … … 78 57 classifier::Target target(vec); 79 58 classifier::NCC<statistics::EuclideanDistance> ncctmp; 80 *error<< "training...\n";59 suite.err() << "training...\n"; 81 60 ncctmp.train(ml,target); 82 *error<< "done\n";61 suite.err() << "done\n"; 83 62 84 63 ///////////////////////////////////////////// 85 64 // A test of predictions using unweighted data 86 65 ///////////////////////////////////////////// 87 *error<< "test of predictions using unweighted test data\n";66 suite.err() << "test of predictions using unweighted test data\n"; 88 67 utility::Matrix data1(3,4); 89 68 for(size_t i=0;i<3;i++) { … … 104 83 utility::Matrix prediction1; 105 84 ncc1.predict(ml1,prediction1); 106 double slack_bound=2e7;107 85 utility::Matrix result1(2,4); 108 86 result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=sqrt(3.0); 109 87 result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=sqrt(11.0); 110 double slack = deviation(prediction1,result1); 111 if (slack > slack_bound  std::isnan(slack)){ 112 *error << "Difference to expected prediction too large\n"; 113 *error << "slack: " << slack << std::endl; 114 *error << "expected less than " << slack_bound << std::endl; 115 ok = false; 88 if (!suite.equal_range(prediction1.begin(), prediction1.end(), 89 result1.begin())) { 90 suite.add(false); 91 suite.err() << "Difference to expected prediction too large\n"; 116 92 } 117 93 … … 119 95 // A test of predictions using unweighted training and weighted test data 120 96 ////////////////////////////////////////////////////////////////////////// 121 *error<< "test of predictions using unweighted training and weighted test data\n";97 suite.err() << "test of predictions using unweighted training and weighted test data\n"; 122 98 utility::Matrix weights1(3,4,1.0); 123 99 weights1(0,0)=weights1(1,1)=weights1(2,2)=weights1(1,3)=0.0; … … 125 101 ncc1.predict(mlw1,prediction1); 126 102 result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=sqrt(15.0); 127 slack = deviation(prediction1,result1); 128 if (slack > slack_bound  std::isnan(slack)){ 129 *error << "Difference to expected prediction too large\n"; 130 *error << "slack: " << slack << std::endl; 131 *error << "expected less than " << slack_bound << std::endl; 132 ok = false; 103 if (!suite.equal_range(prediction1.begin(), prediction1.end(), 104 result1.begin())) { 105 suite.add(false); 106 suite.err() << "Difference to expected prediction too large\n"; 133 107 } 134 108 … … 137 111 // in centroids and unweighted test data 138 112 ////////////////////////////////////////////////////////////////////////// 139 *error<< "test of predictions using nan centroids and unweighted test data\n";113 suite.err() << "test of predictions using nan centroids and unweighted test data\n"; 140 114 utility::Matrix weights2(3,4,1.0); 141 115 weights2(1,0)=weights2(1,1)=0.0; … … 147 121 result1(1,0)=result1(1,1)=sqrt(11.0); 148 122 result1(0,2)=result1(0,3)=sqrt(15.0); 149 slack = deviation(prediction1,result1);150 123 if(!std::isnan(ncc2.centroids()(1,0))) 151 ok=false; 152 if (slack > slack_bound  std::isnan(slack)){ 153 *error << "Difference to expected prediction too large\n"; 154 *error << "slack: " << slack << std::endl; 155 *error << "expected less than " << slack_bound << std::endl; 156 ok = false; 124 suite.add(false); 125 if (!suite.equal_range(prediction1.begin(), prediction1.end(), 126 result1.begin())) { 127 suite.add(false); 128 suite.err() << "Difference to expected prediction too large\n"; 157 129 } 158 130 … … 161 133 // test sample has nonzero weights for. 162 134 ////////////////////////////////////////////////////////////////////////// 163 *error<< "test of predictions using nan centroids and weighted test data\n";164 *error<< "... using EuclideanDistance" << std::endl;135 suite.err() << "test of predictions using nan centroids and weighted test data\n"; 136 suite.err() << "... using EuclideanDistance" << std::endl; 165 137 weights1(0,0)=weights1(2,0)=0; 166 138 classifier::NCC<statistics::EuclideanDistance> ncc3; … … 168 140 ncc3.predict(mlw1,prediction1); 169 141 if(!std::isnan(ncc3.centroids()(1,0))) { 170 ok=false;171 *error<< "Training failed: expected nan in centroid" << std::endl;142 suite.add(false); 143 suite.err() << "Training failed: expected nan in centroid" << std::endl; 172 144 } 173 145 if(!(std::isnan(prediction1(0,0)) && 174 s td::abs(prediction1(1,0)sqrt(3.0))<slack_bound&&175 s td::abs(prediction1(0,1)sqrt(3.0))<slack_bound&&176 s td::abs(prediction1(1,1)sqrt(15.0))<slack_bound&&177 s td::abs(prediction1(0,2)sqrt(27.0))<slack_bound)) {178 ok=false;179 *error<< "Test failed: predictions incorrect" << std::endl;180 } 181 *error<< "... using PearsonDistance" << std::endl;;146 suite.equal(prediction1(1,0),sqrt(3.0)) && 147 suite.equal(prediction1(0,1),sqrt(3.0)) && 148 suite.equal(prediction1(1,1),sqrt(15.0)) && 149 suite.equal(prediction1(0,2),sqrt(27.0)) )) { 150 suite.add(false); 151 suite.err() << "Test failed: predictions incorrect" << std::endl; 152 } 153 suite.err() << "... using PearsonDistance" << std::endl;; 182 154 classifier::NCC<statistics::PearsonDistance> ncc4; 183 155 ncc4.train(mlw2,target1); 184 156 ncc4.predict(mlw1,prediction1); 185 157 if(!std::isnan(ncc4.centroids()(1,0))) { 186 ok=false;187 *error<< "Training failed: expected nan in centroid" << std::endl;158 suite.add(false); 159 suite.err() << "Training failed: expected nan in centroid" << std::endl; 188 160 } 189 161 if(!(std::isnan(prediction1(0,0)) && 190 162 std::isnan(prediction1(0,2)) && 191 163 std::isnan(prediction1(1,0)) && 192 s td::abs(prediction1(0,1))<slack_bound&&193 s td::abs(prediction1(1,2))<slack_bound&&194 s td::abs(prediction1(1,3))<slack_bound&&195 s td::abs(prediction1(0,3)2.0)<slack_bound&&196 s td::abs(prediction1(1,1)2.0)<slack_bound)) {197 ok=false;198 *error<< "Test failed: predictions incorrect" << std::endl;164 suite.equal(prediction1(0,1), 0) && 165 suite.equal(prediction1(1,2), 0) && 166 suite.equal(prediction1(1,3), 0) && 167 suite.equal(prediction1(0,3), 2.0) && 168 suite.equal(prediction1(1,1), 2.0) )) { 169 suite.add(false); 170 suite.err() << "Test failed: predictions incorrect" << std::endl; 199 171 } 200 172 … … 214 186 std::isnan(prediction1(0,2)) && std::isnan(prediction1(0,3)) && 215 187 std::isnan(prediction1(1,0)) && 216 s td::abs(prediction1(1,1)2.0)<slack_bound&&217 s td::abs(prediction1(1,2))<slack_bound&&218 s td::abs(prediction1(1,3))<slack_bound)) {219 *error<< "Difference to expected prediction too large\n";220 ok = false;188 suite.equal(prediction1(1,1), 2.0) && 189 suite.equal(prediction1(1,2),0) && 190 suite.equal(prediction1(1,3),0) )) { 191 suite.err() << "Difference to expected prediction too large\n"; 192 suite.add(false); 221 193 } 222 194 … … 224 196 // A test of predictions using Sorlie data 225 197 ////////////////////////////////////////////////////////////////////////// 226 *error<< "test with Sorlie data\n";198 suite.err() << "test with Sorlie data\n"; 227 199 std::ifstream is("data/sorlie_centroid_data.txt"); 228 200 utility::Matrix data(is,'\t'); … … 239 211 classifier::MatrixLookupWeighted dataviewweighted(data,weights); 240 212 classifier::NCC<statistics::PearsonDistance> ncc; 241 *error<< "training...\n";213 suite.err() << "training...\n"; 242 214 ncc.train(dataviewweighted,targets); 243 215 … … 249 221 if(centroids.rows() != ncc.centroids().rows()  250 222 centroids.columns() != ncc.centroids().columns()) { 251 *error<< "Error in the dimensionality of centroids\n";252 *error<< "Nof rows: " << centroids.rows() << " expected: "223 suite.err() << "Error in the dimensionality of centroids\n"; 224 suite.err() << "Nof rows: " << centroids.rows() << " expected: " 253 225 << ncc.centroids().rows() << std::endl; 254 *error<< "Nof columns: " << centroids.columns() << " expected: "226 suite.err() << "Nof columns: " << centroids.columns() << " expected: " 255 227 << ncc.centroids().columns() << std::endl; 256 228 } 257 229 258 slack = deviation(centroids,ncc.centroids()); 259 if (slack > slack_bound  std::isnan(slack)){ 260 *error << "Difference to stored centroids too large\n"; 261 *error << "slack: " << slack << std::endl; 262 *error << "expected less than " << slack_bound << std::endl; 263 ok = false; 264 } 265 266 *error << "...predicting...\n"; 230 if (!suite.equal_range(centroids.begin(), centroids.end(), 231 ncc.centroids().begin(), 100000)) { 232 suite.add(false); 233 suite.err() << "Difference to stored centroids too large\n"; 234 } 235 236 suite.err() << "...predicting...\n"; 267 237 utility::Matrix prediction; 268 238 ncc.predict(dataviewweighted,prediction); … … 273 243 is.close(); 274 244 275 slack = deviation(result,prediction); 276 if (slack > slack_bound  std::isnan(slack)){ 277 *error << "Difference to stored prediction too large\n"; 278 *error << "slack: " << slack << std::endl; 279 *error << "expected less than " << slack_bound << std::endl; 280 ok = false; 281 } 282 *error << "done\n"; 283 284 if(ok) 285 *error << "OK" << std::endl; 286 else 287 *error << "FAILED" << std::endl; 288 289 if (error!=&std::cerr) 290 delete error; 291 292 if(ok) 293 return 0; 294 return 1; 245 if (!suite.equal_range(result.begin(), result.end(), 246 prediction.begin(), 100000)) { 247 suite.add(false); 248 suite.err() << "Difference to stored prediction too large\n"; 249 } 250 251 return suite.return_value(); 295 252 }
Note: See TracChangeset
for help on using the changeset viewer.