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 | |
---|
18 | using namespace theplu::yat; |
---|
19 | |
---|
20 | int 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 | |
---|