# Changeset 1239

Ignore:
Timestamp:
Mar 16, 2008, 6:09:56 AM (14 years ago)
Message:

working on #223

Location:
trunk/test
Files:
4 edited

Unmodified
Removed
• ## trunk/test/Suite.cc

 r1234 #include "Suite.h" #include #include #include bool Suite::equal(double a, double b, u_int n) { last_error_bound_ = n*std::numeric_limits().epsilon()* double last_error_bound = n*std::numeric_limits().epsilon()* std::min(std::abs(a), std::abs(b)); return std::abs(a-b) <= last_error_bound_; } double Suite::last_error_bound(void) const { return last_error_bound_; if (!(std::abs(a-b) <= last_error_bound)){ err() << "Error: Comparing " << a << " and " << b << "\n  Difference: " << a - b << "\n  expected difference to be at most " << last_error_bound << std::endl; return false; } return true; }
• ## trunk/test/Suite.h

 r1234 bool equal(double a, double b, unsigned int N=1); double last_error_bound(void) const; template bool equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2, unsigned int N=1); /** bool ok_; std::ofstream* dev_null_; double last_error_bound_; }; template bool Suite::equal_range(Iterator1 first1, Iterator1 last1, Iterator2 first2, unsigned int N) { while (first1!=last1){ if (!this->equal(*first1, *first2, N) )  { return false; } ++first1; ++first2; } return true; } }}}
• ## trunk/test/distance_test.cc

 r1234 if (!suite.equal(dist1, dist2, N)) { suite.err() << "Error: " << msg << " failed.\n"; suite.err() << "  dist1: " << dist1 << "\n"; suite.err() << "  dist2: " << dist2 << "\n"; suite.err() << "  difference: " << dist1-dist2 << "\n"; suite.err() << "  error bound: " << suite.last_error_bound() << "\n"; suite.add(false); }
• ## trunk/test/knn_test.cc

 r1210 /* Copyright (C) 2007 Peter Johansson, Markus Ringnér Copyright (C) 2007 2008 Peter Johansson, Markus Ringnér This file is part of the yat library, http://trac.thep.lu.se/yat 02111-1307, USA. */ #include "Suite.h" #include "yat/classifier/KNN.h" } int main(const int argc,const char* argv[]) int main(int argc, char* argv[]) { std::ostream* error; if (argc>1 && argv[1]==std::string("-v")) error = &std::cerr; else { error = new std::ofstream("/dev/null"); if (argc>1) std::cout << "knn_test -v : for printing extra information\n"; } *error << "testing knn" << std::endl; bool ok = true; test::Suite suite(argc, argv); suite.err() << "testing knn" << std::endl; //////////////////////////////////////////////////////////////// // A test of training and predictions using unweighted data //////////////////////////////////////////////////////////////// *error << "test of predictions using unweighted training and test data\n"; suite.err() << "test of predictions using unweighted training " << "and test data\n"; utility::Matrix data1(3,4); for(size_t i=0;i<3;i++) { utility::Matrix prediction1; knn1.predict(ml1,prediction1); double slack_bound=2e-7; utility::Matrix result1(2,4); result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=2.0; result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=1.0; double slack = deviation(prediction1,result1); if (slack > slack_bound || std::isnan(slack)){ *error << "Difference to expected prediction too large\n"; *error << "slack: " << slack << std::endl; *error << "expected less than " << slack_bound << std::endl; ok = false; } suite.add(suite.equal_range(result1.begin(), result1.end(), prediction1.begin(), 1)); // A test of training unweighted and test weighted //////////////////////////////////////////////////////////////// *error << "test of predictions using unweighted training and weighted test data\n"; suite.err() << "test of predictions using unweighted training and weighted test data\n"; utility::Matrix weights1(3,4,1.0); weights1(2,0)=0; result1(0,0)=1.0; result1(1,0)=2.0; slack = deviation(prediction1,result1); if (slack > slack_bound || std::isnan(slack)){ *error << "Difference to expected prediction too large\n"; *error << "slack: " << slack << std::endl; *error << "expected less than " << slack_bound << std::endl; ok = false; } suite.add(suite.equal_range(result1.begin(), result1.end(), prediction1.begin(), 1)); //////////////////////////////////////////////////////////////// // A test of training and test both weighted //////////////////////////////////////////////////////////////// *error << "test of predictions using weighted training and test data\n"; *error << "... uniform neighbor weighting" << std::endl; suite.err() << "test of predictions using weighted training and test data\n"; suite.err() << "... uniform neighbor weighting" << std::endl; weights1(0,1)=0; utility::Matrix weights2(3,4,1.0); result1(0,1)=1.0; result1(1,1)=2.0; slack = deviation(prediction1,result1); if (slack > slack_bound || std::isnan(slack)){ *error << "Difference to expected prediction too large\n"; *error << "slack: " << slack << std::endl; *error << "expected less than " << slack_bound << std::endl; ok = false; } suite.add(suite.equal_range(result1.begin(), result1.end(), prediction1.begin(), 1)); // A test of reciprocal ranks weighting with training and test both weighted //////////////////////////////////////////////////////////////// *error << "... reciprokal rank neighbor weighting" << std::endl; suite.err() << "... reciprokal rank neighbor weighting" << std::endl; utility::Matrix data2(data1); data2(1,3)=7; result1(0,2)=result1(1,1)=1.0/2.0; result1(0,1)=result1(1,2)=4.0/3.0; slack = deviation(prediction1,result1); if (slack > slack_bound || std::isnan(slack)){ *error << "Difference to expected prediction too large\n"; *error << "slack: " << slack << std::endl; *error << "expected less than " << slack_bound << std::endl; ok = false; } suite.add(suite.equal_range(result1.begin(), result1.end(), prediction1.begin(), 1)); // A test of reciprocal distance weighting with training and test both weighted //////////////////////////////////////////////////////////////// *error << "... reciprokal distance neighbor weighting" << std::endl; suite.err() << "... reciprocal distance neighbor weighting" << std::endl; classifier::KNN knn4; if (!(std::isinf(prediction1(0,0)) && std::isinf(prediction1(0,1)) && std::isinf(prediction1(1,2)) && std::abs(prediction1(1,3)-(1.0/3.67423461417)) slack_bound || std::isnan(slack)){ *error << "Difference to expected prediction too large\n"; *error << "slack: " << slack << std::endl; *error << "expected less than " << slack_bound << std::endl; ok = false; } if(!ok) { *error << "knn_test failed" << std::endl; } else { *error << "OK" << std::endl; } if (error!=&std::cerr) delete error; if (ok=true) return 0; return -1; suite.add(suite.equal_range(result1.begin(), result1.end(), prediction1.begin(), 1)); return suite.return_value(); }
Note: See TracChangeset for help on using the changeset viewer.