source: trunk/test/knn_test.cc @ 916

Last change on this file since 916 was 916, checked in by Peter, 16 years ago

Sorry this commit is a bit to big.

Adding a yat_assert. The yat assert are turned on by providing a
'-DYAT_DEBUG' flag to preprocessor if normal cassert is turned
on. This flag is activated for developers running configure with
--enable-debug. The motivation is that we can use these yat_asserts in
header files and the yat_asserts will be invisible to the normal user
also if he uses C-asserts.

added output operator in DataLookup2D and removed output operator in
MatrixLookup?

Removed template function add_values in Averager and weighted version

Added function to AveragerWeighted? taking iterator to four ranges.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.5 KB
Line 
1// $Id: knn_test.cc 916 2007-09-30 00:50:10Z peter $
2
3#include "yat/classifier/KNN.h"
4#include "yat/classifier/MatrixLookupWeighted.h"
5#include "yat/statistics/euclidean_vector_distance.h"
6#include "yat/statistics/pearson_vector_distance.h"
7#include "yat/utility/matrix.h"
8
9
10#include <cassert>
11#include <fstream>
12#include <iostream>
13#include <list>
14#include <string>
15#include <vector>
16
17
18using namespace theplu::yat;
19
20int main(const int argc,const char* argv[])
21
22{ 
23  std::ostream* error;
24  if (argc>1 && argv[1]==std::string("-v"))
25    error = &std::cerr;
26  else {
27    error = new std::ofstream("/dev/null");
28    if (argc>1)
29      std::cout << "knn_test -v : for printing extra information\n";
30  }
31  *error << "testing knn" << std::endl;
32  bool ok = true;
33 
34  std::ifstream is("data/sorlie_centroid_data.txt");
35  utility::matrix data(is,'\t');
36  is.close();
37 
38  is.open("data/sorlie_centroid_classes.txt");
39  classifier::Target targets(is);
40  is.close();
41
42  // Generate weight matrix with 0 for missing values and 1 for others.
43  utility::matrix weights(data.rows(),data.columns(),0.0);
44  utility::nan(data,weights);
45
46  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
47  classifier::KNN<statistics::pearson_vector_distance_tag> knn(dataviewweighted,targets);
48  *error << "Training KNN" << std::endl;
49  knn.train();
50 
51  utility::matrix prediction;
52  knn.predict(dataviewweighted,prediction);
53 
54  if(!ok) {
55    *error << "vector_distance_test failed" << std::endl;
56  }
57  if (error!=&std::cerr)
58    delete error;
59  if (ok=true) 
60    return 0;
61  return -1;
62}
63
64
Note: See TracBrowser for help on using the repository browser.