source: trunk/test/knn_test.cc @ 1121

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

fixes #308

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1// $Id: knn_test.cc 1121 2008-02-22 15:29:56Z 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/KNN_ReciprocalDistance.h"
26#include "yat/classifier/KNN_ReciprocalRank.h"
27#include "yat/classifier/MatrixLookup.h"
28#include "yat/classifier/MatrixLookupWeighted.h"
29#include "yat/statistics/EuclideanDistance.h"
30#include "yat/utility/Matrix.h"
31
32
33#include <cassert>
34#include <fstream>
35#include <iostream>
36#include <list>
37#include <string>
38#include <vector>
39
40
41using namespace theplu::yat;
42
43double deviation(const utility::Matrix& a, const utility::Matrix& b) {
44  double sl=0;
45  for (size_t i=0; i<a.rows(); i++){
46    for (size_t j=0; j<a.columns(); j++){
47      sl += fabs(a(i,j)-b(i,j));
48    }
49  }
50  sl /= (a.columns()*a.rows());
51  return sl;
52}
53
54int main(const int argc,const char* argv[])
55
56{ 
57  std::ostream* error;
58  if (argc>1 && argv[1]==std::string("-v"))
59    error = &std::cerr;
60  else {
61    error = new std::ofstream("/dev/null");
62    if (argc>1)
63      std::cout << "knn_test -v : for printing extra information\n";
64  }
65  *error << "testing knn" << std::endl;
66  bool ok = true;
67
68  ////////////////////////////////////////////////////////////////
69  // A test of training and predictions using unweighted data
70  ////////////////////////////////////////////////////////////////
71  *error << "test of predictions using unweighted training and test data\n";
72  utility::Matrix data1(3,4);
73  for(size_t i=0;i<3;i++) {
74    data1(i,0)=3-i;
75    data1(i,1)=5-i;
76    data1(i,2)=i+1;
77    data1(i,3)=i+3;
78  }
79  std::vector<std::string> vec1(4, "pos");
80  vec1[0]="neg";
81  vec1[1]="neg";
82 
83  classifier::MatrixLookup ml1(data1);
84  classifier::Target target1(vec1);
85 
86  classifier::KNN<statistics::EuclideanDistance> knn1(ml1,target1);
87  knn1.k(3);
88  knn1.train();
89  utility::Matrix prediction1;
90  knn1.predict(ml1,prediction1);
91  double slack_bound=2e-7;
92  utility::Matrix result1(2,4);
93  result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=2.0;
94  result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=1.0;
95  double slack = deviation(prediction1,result1); 
96  if (slack > slack_bound || std::isnan(slack)){
97    *error << "Difference to expected prediction too large\n";
98    *error << "slack: " << slack << std::endl;
99    *error << "expected less than " << slack_bound << std::endl;
100    ok = false;
101  }
102 
103
104  ////////////////////////////////////////////////////////////////
105  // A test of training unweighted and test weighted
106  ////////////////////////////////////////////////////////////////
107  *error << "test of predictions using unweighted training and weighted test data\n";
108  utility::Matrix weights1(3,4,1.0);
109  weights1(2,0)=0;
110  classifier::MatrixLookupWeighted mlw1(data1,weights1);
111  knn1.predict(mlw1,prediction1); 
112  result1(0,0)=1.0;
113  result1(1,0)=2.0;
114  slack = deviation(prediction1,result1);
115  if (slack > slack_bound || std::isnan(slack)){
116    *error << "Difference to expected prediction too large\n";
117    *error << "slack: " << slack << std::endl;
118    *error << "expected less than " << slack_bound << std::endl;
119    ok = false;
120  } 
121
122  ////////////////////////////////////////////////////////////////
123  // A test of training and test both weighted
124  ////////////////////////////////////////////////////////////////
125  *error << "test of predictions using weighted training and test data\n";
126  weights1(0,1)=0;
127  utility::Matrix weights2(3,4,1.0);
128  weights2(2,3)=0;
129  classifier::MatrixLookupWeighted mlw2(data1,weights2);
130  classifier::KNN<statistics::EuclideanDistance> knn2(mlw2,target1);
131  knn2.k(3);
132  knn2.train();
133  knn2.predict(mlw1,prediction1); 
134  result1(0,1)=1.0;
135  result1(1,1)=2.0;
136  slack = deviation(prediction1,result1);
137  if (slack > slack_bound || std::isnan(slack)){
138    *error << "Difference to expected prediction too large\n";
139    *error << "slack: " << slack << std::endl;
140    *error << "expected less than " << slack_bound << std::endl;
141    ok = false;
142  } 
143
144
145  ////////////////////////////////////////////////////////////////
146  // A test of reciprocal ranks weighting with training and test both weighted
147  ////////////////////////////////////////////////////////////////
148  utility::Matrix data2(data1);
149  data2(1,3)=7;
150  classifier::MatrixLookupWeighted mlw3(data2,weights2);
151  classifier::KNN<statistics::EuclideanDistance,classifier::KNN_ReciprocalRank> 
152    knn3(mlw2,target1);
153  knn3.k(3);
154  knn3.train();
155  knn3.predict(mlw3,prediction1); 
156  result1(0,0)=result1(1,3)=1.0;
157  result1(0,3)=result1(1,0)=5.0/6.0;
158  result1(0,2)=result1(1,1)=1.0/2.0;
159  result1(0,1)=result1(1,2)=4.0/3.0;
160  slack = deviation(prediction1,result1); 
161  if (slack > slack_bound || std::isnan(slack)){
162    *error << "Difference to expected prediction too large\n";
163    *error << "slack: " << slack << std::endl;
164    *error << "expected less than " << slack_bound << std::endl;
165    ok = false;
166  }
167
168
169  ////////////////////////////////////////////////////////////////
170  // A test of reciprocal distance weighting with training and test both weighted
171  ////////////////////////////////////////////////////////////////
172  classifier::KNN<statistics::EuclideanDistance,classifier::KNN_ReciprocalDistance> 
173    knn4(mlw2,target1);
174  knn4.k(3);
175  knn4.train();
176  knn4.predict(mlw3,prediction1); 
177  slack = deviation(prediction1,result1); 
178  if (!std::isinf(prediction1(0,0)) && !std::isinf(prediction1(0,1)) && 
179      !std::isinf(prediction1(1,2)) && 
180      fabs(prediction1(1,3)-(1.0/3.67423461417))>slack_bound &&
181      fabs(prediction1(1,0)-(1.0/2.82842712475+1.0/2.44948974278))>slack_bound){
182    *error << "Difference to expected prediction too large\n";
183    ok = false;
184  }
185
186  if(!ok) {
187    *error << "knn_test failed" << std::endl;
188  }
189  else {
190    *error << "OK" << std::endl;
191  }
192  if (error!=&std::cerr)
193    delete error;
194  if (ok=true) 
195    return 0;
196  return -1;
197}
198
199
Note: See TracBrowser for help on using the repository browser.