source: trunk/test/knn_test.cc @ 1050

Last change on this file since 1050 was 1050, checked in by Peter, 15 years ago

Simplifying distance structure

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1// $Id: knn_test.cc 1050 2008-02-07 18:47:34Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson, Markus Ringnér
5
6  This file is part of the yat library, http://trac.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/classifier/KNN.h"
25#include "yat/classifier/MatrixLookupWeighted.h"
26#include "yat/statistics/euclidean_distance.h"
27#include "yat/statistics/pearson_distance.h"
28#include "yat/utility/matrix.h"
29
30
31#include <cassert>
32#include <fstream>
33#include <iostream>
34#include <list>
35#include <string>
36#include <vector>
37
38
39using namespace theplu::yat;
40
41int main(const int argc,const char* argv[])
42
43{ 
44  std::ostream* error;
45  if (argc>1 && argv[1]==std::string("-v"))
46    error = &std::cerr;
47  else {
48    error = new std::ofstream("/dev/null");
49    if (argc>1)
50      std::cout << "knn_test -v : for printing extra information\n";
51  }
52  *error << "testing knn" << std::endl;
53  bool ok = true;
54 
55  std::ifstream is("data/sorlie_centroid_data.txt");
56  utility::matrix data(is,'\t');
57  is.close();
58 
59  is.open("data/sorlie_centroid_classes.txt");
60  classifier::Target targets(is);
61  is.close();
62
63  // Generate weight matrix with 0 for missing values and 1 for others.
64  utility::matrix weights(data.rows(),data.columns(),0.0);
65  utility::nan(data,weights);
66
67  classifier::MatrixLookupWeighted dataviewweighted(data,weights);
68  classifier::KNN<statistics::PearsonDistance> knn(dataviewweighted,targets);
69  *error << "training KNN" << std::endl;
70  knn.train();
71 
72  utility::matrix prediction;
73  knn.predict(dataviewweighted,prediction);
74  *error << prediction << std::endl;
75 
76  if(!ok) {
77    *error << "knn_test failed" << std::endl;
78  }
79  else {
80    *error << "OK" << std::endl;
81  }
82  if (error!=&std::cerr)
83    delete error;
84  if (ok=true) 
85    return 0;
86  return -1;
87}
88
89
Note: See TracBrowser for help on using the repository browser.