903 |
27 Sep 07 |
markus |
// $Id$ |
903 |
27 Sep 07 |
markus |
2 |
|
999 |
23 Dec 07 |
jari |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2010, 2012 Peter Johansson |
999 |
23 Dec 07 |
jari |
6 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
999 |
23 Dec 07 |
jari |
8 |
|
999 |
23 Dec 07 |
jari |
The yat library is free software; you can redistribute it and/or |
999 |
23 Dec 07 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
999 |
23 Dec 07 |
jari |
License, or (at your option) any later version. |
999 |
23 Dec 07 |
jari |
13 |
|
999 |
23 Dec 07 |
jari |
The yat library is distributed in the hope that it will be useful, |
999 |
23 Dec 07 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
999 |
23 Dec 07 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
999 |
23 Dec 07 |
jari |
General Public License for more details. |
999 |
23 Dec 07 |
jari |
18 |
|
999 |
23 Dec 07 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
999 |
23 Dec 07 |
jari |
21 |
*/ |
999 |
23 Dec 07 |
jari |
22 |
|
2881 |
18 Nov 12 |
peter |
23 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
24 |
|
1239 |
16 Mar 08 |
peter |
25 |
#include "Suite.h" |
1239 |
16 Mar 08 |
peter |
26 |
|
903 |
27 Sep 07 |
markus |
27 |
#include "yat/classifier/KNN.h" |
1112 |
21 Feb 08 |
markus |
28 |
#include "yat/classifier/KNN_ReciprocalDistance.h" |
1112 |
21 Feb 08 |
markus |
29 |
#include "yat/classifier/KNN_ReciprocalRank.h" |
1107 |
19 Feb 08 |
markus |
30 |
#include "yat/classifier/MatrixLookup.h" |
903 |
27 Sep 07 |
markus |
31 |
#include "yat/classifier/MatrixLookupWeighted.h" |
1052 |
07 Feb 08 |
peter |
32 |
#include "yat/statistics/EuclideanDistance.h" |
1586 |
16 Oct 08 |
peter |
33 |
#include "yat/utility/DataIterator.h" |
1121 |
22 Feb 08 |
peter |
34 |
#include "yat/utility/Matrix.h" |
1586 |
16 Oct 08 |
peter |
35 |
#include "yat/utility/MatrixWeighted.h" |
903 |
27 Sep 07 |
markus |
36 |
|
903 |
27 Sep 07 |
markus |
37 |
|
903 |
27 Sep 07 |
markus |
38 |
#include <cassert> |
903 |
27 Sep 07 |
markus |
39 |
#include <fstream> |
903 |
27 Sep 07 |
markus |
40 |
#include <iostream> |
903 |
27 Sep 07 |
markus |
41 |
#include <list> |
903 |
27 Sep 07 |
markus |
42 |
#include <string> |
903 |
27 Sep 07 |
markus |
43 |
#include <vector> |
903 |
27 Sep 07 |
markus |
44 |
|
903 |
27 Sep 07 |
markus |
45 |
|
903 |
27 Sep 07 |
markus |
46 |
using namespace theplu::yat; |
903 |
27 Sep 07 |
markus |
47 |
|
2338 |
16 Oct 10 |
peter |
48 |
void compile_test(test::Suite&); |
1586 |
16 Oct 08 |
peter |
49 |
utility::Matrix data(void); |
1586 |
16 Oct 08 |
peter |
50 |
utility::MatrixWeighted data_weighted(void); |
1586 |
16 Oct 08 |
peter |
51 |
double deviation(const utility::Matrix& a, const utility::Matrix& b); |
1586 |
16 Oct 08 |
peter |
52 |
void test_unweighted(test::Suite&); |
1586 |
16 Oct 08 |
peter |
53 |
void test_unweighted_weighted(test::Suite&); |
1586 |
16 Oct 08 |
peter |
54 |
void test_weighted(test::Suite&); |
1586 |
16 Oct 08 |
peter |
55 |
void test_reciprocal_ranks(test::Suite&); |
1586 |
16 Oct 08 |
peter |
56 |
void test_reciprocal_distance(test::Suite&); |
1586 |
16 Oct 08 |
peter |
57 |
void test_no_samples(test::Suite&); |
1586 |
16 Oct 08 |
peter |
58 |
void test_no_features(test::Suite&); |
1586 |
16 Oct 08 |
peter |
59 |
std::vector<std::string> vec_target(void); |
1586 |
16 Oct 08 |
peter |
60 |
|
2338 |
16 Oct 10 |
peter |
61 |
|
1586 |
16 Oct 08 |
peter |
62 |
int main(int argc, char* argv[]) |
4200 |
19 Aug 22 |
peter |
63 |
{ |
1586 |
16 Oct 08 |
peter |
64 |
test::Suite suite(argc, argv); |
1586 |
16 Oct 08 |
peter |
65 |
suite.err() << "testing knn" << std::endl; |
1586 |
16 Oct 08 |
peter |
66 |
test_unweighted(suite); |
1586 |
16 Oct 08 |
peter |
67 |
test_unweighted_weighted(suite); |
1586 |
16 Oct 08 |
peter |
68 |
test_weighted(suite); |
1586 |
16 Oct 08 |
peter |
69 |
test_reciprocal_ranks(suite); |
1586 |
16 Oct 08 |
peter |
70 |
test_reciprocal_distance(suite); |
1586 |
16 Oct 08 |
peter |
71 |
test_no_samples(suite); |
1586 |
16 Oct 08 |
peter |
72 |
test_no_features(suite); |
2338 |
16 Oct 10 |
peter |
73 |
compile_test(suite); |
1586 |
16 Oct 08 |
peter |
74 |
return suite.return_value(); |
4200 |
19 Aug 22 |
peter |
75 |
} |
1586 |
16 Oct 08 |
peter |
76 |
|
1586 |
16 Oct 08 |
peter |
77 |
|
2338 |
16 Oct 10 |
peter |
78 |
void compile_test(test::Suite& suite) |
2338 |
16 Oct 10 |
peter |
79 |
{ |
2338 |
16 Oct 10 |
peter |
80 |
if (false) { |
2338 |
16 Oct 10 |
peter |
81 |
boost::detail::dummy_constructor dummy; |
2338 |
16 Oct 10 |
peter |
82 |
test::distance_archetype distance(dummy); |
2339 |
16 Oct 10 |
peter |
83 |
classifier::KNN<test::distance_archetype |
2339 |
16 Oct 10 |
peter |
84 |
, test::neighbor_weighting_archetype> knn(distance); |
2338 |
16 Oct 10 |
peter |
85 |
knn.k(3); |
2338 |
16 Oct 10 |
peter |
86 |
knn.k(); |
2338 |
16 Oct 10 |
peter |
87 |
classifier::SupervisedClassifier* knn2 = knn.make_classifier(); |
2338 |
16 Oct 10 |
peter |
88 |
delete knn2; |
2338 |
16 Oct 10 |
peter |
89 |
utility::Matrix result; |
2338 |
16 Oct 10 |
peter |
90 |
knn.train(classifier::MatrixLookup(data()), classifier::Target()); |
4200 |
19 Aug 22 |
peter |
91 |
knn.train(classifier::MatrixLookupWeighted(data_weighted()), |
2338 |
16 Oct 10 |
peter |
92 |
classifier::Target()); |
2338 |
16 Oct 10 |
peter |
93 |
knn.predict(classifier::MatrixLookup(data()), result); |
2338 |
16 Oct 10 |
peter |
94 |
knn.predict(classifier::MatrixLookupWeighted(data_weighted()), result); |
2338 |
16 Oct 10 |
peter |
95 |
} |
2338 |
16 Oct 10 |
peter |
96 |
} |
2338 |
16 Oct 10 |
peter |
97 |
|
2338 |
16 Oct 10 |
peter |
98 |
|
1586 |
16 Oct 08 |
peter |
99 |
utility::Matrix data(void) |
1586 |
16 Oct 08 |
peter |
100 |
{ |
1586 |
16 Oct 08 |
peter |
101 |
utility::Matrix data1(3,4); |
1586 |
16 Oct 08 |
peter |
102 |
for(size_t i=0;i<3;i++) { |
1586 |
16 Oct 08 |
peter |
103 |
data1(i,0)=3-i; |
1586 |
16 Oct 08 |
peter |
104 |
data1(i,1)=5-i; |
1586 |
16 Oct 08 |
peter |
105 |
data1(i,2)=i+1; |
1586 |
16 Oct 08 |
peter |
106 |
data1(i,3)=i+3; |
1586 |
16 Oct 08 |
peter |
107 |
} |
1586 |
16 Oct 08 |
peter |
108 |
return data1; |
1586 |
16 Oct 08 |
peter |
109 |
} |
1586 |
16 Oct 08 |
peter |
110 |
|
1586 |
16 Oct 08 |
peter |
111 |
|
1586 |
16 Oct 08 |
peter |
112 |
utility::MatrixWeighted data_weighted(void) |
1586 |
16 Oct 08 |
peter |
113 |
{ |
1586 |
16 Oct 08 |
peter |
114 |
utility::Matrix x = data(); |
1586 |
16 Oct 08 |
peter |
115 |
utility::MatrixWeighted result(x.rows(), x.columns()); |
1586 |
16 Oct 08 |
peter |
116 |
std::copy(x.begin(), x.end(), utility::data_iterator(result.begin())); |
1586 |
16 Oct 08 |
peter |
117 |
return result; |
1586 |
16 Oct 08 |
peter |
118 |
} |
1586 |
16 Oct 08 |
peter |
119 |
|
1586 |
16 Oct 08 |
peter |
120 |
|
1121 |
22 Feb 08 |
peter |
121 |
double deviation(const utility::Matrix& a, const utility::Matrix& b) { |
1586 |
16 Oct 08 |
peter |
122 |
assert(a.rows()==b.rows()); |
1586 |
16 Oct 08 |
peter |
123 |
assert(b.columns()==b.columns()); |
1107 |
19 Feb 08 |
markus |
124 |
double sl=0; |
1107 |
19 Feb 08 |
markus |
125 |
for (size_t i=0; i<a.rows(); i++){ |
1107 |
19 Feb 08 |
markus |
126 |
for (size_t j=0; j<a.columns(); j++){ |
1210 |
06 Mar 08 |
peter |
127 |
sl += std::abs(a(i,j)-b(i,j)); |
1107 |
19 Feb 08 |
markus |
128 |
} |
1107 |
19 Feb 08 |
markus |
129 |
} |
1107 |
19 Feb 08 |
markus |
130 |
sl /= (a.columns()*a.rows()); |
1107 |
19 Feb 08 |
markus |
131 |
return sl; |
1107 |
19 Feb 08 |
markus |
132 |
} |
1107 |
19 Feb 08 |
markus |
133 |
|
1586 |
16 Oct 08 |
peter |
134 |
void test_unweighted(test::Suite& suite) |
1586 |
16 Oct 08 |
peter |
135 |
{ |
1107 |
19 Feb 08 |
markus |
136 |
//////////////////////////////////////////////////////////////// |
1107 |
19 Feb 08 |
markus |
// A test of training and predictions using unweighted data |
1107 |
19 Feb 08 |
markus |
138 |
//////////////////////////////////////////////////////////////// |
4200 |
19 Aug 22 |
peter |
139 |
suite.err() << "test of predictions using unweighted training " |
1239 |
16 Mar 08 |
peter |
140 |
<< "and test data\n"; |
1586 |
16 Oct 08 |
peter |
141 |
utility::Matrix data1 = data(); |
1107 |
19 Feb 08 |
markus |
142 |
classifier::MatrixLookup ml1(data1); |
1586 |
16 Oct 08 |
peter |
143 |
classifier::Target target1(vec_target()); |
4200 |
19 Aug 22 |
peter |
144 |
|
1157 |
26 Feb 08 |
markus |
145 |
classifier::KNN<statistics::EuclideanDistance> knn1; |
1107 |
19 Feb 08 |
markus |
146 |
knn1.k(3); |
1157 |
26 Feb 08 |
markus |
147 |
knn1.train(ml1,target1); |
1121 |
22 Feb 08 |
peter |
148 |
utility::Matrix prediction1; |
1107 |
19 Feb 08 |
markus |
149 |
knn1.predict(ml1,prediction1); |
1121 |
22 Feb 08 |
peter |
150 |
utility::Matrix result1(2,4); |
1112 |
21 Feb 08 |
markus |
151 |
result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=2.0; |
1112 |
21 Feb 08 |
markus |
152 |
result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=1.0; |
4200 |
19 Aug 22 |
peter |
153 |
suite.add(suite.equal_range(result1.begin(), result1.end(), |
1239 |
16 Mar 08 |
peter |
154 |
prediction1.begin(), 1)); |
1586 |
16 Oct 08 |
peter |
155 |
} |
1586 |
16 Oct 08 |
peter |
156 |
|
1586 |
16 Oct 08 |
peter |
157 |
void test_unweighted_weighted(test::Suite& suite) |
1586 |
16 Oct 08 |
peter |
158 |
{ |
1586 |
16 Oct 08 |
peter |
159 |
suite.err() << "test of predictions using unweighted training " |
1586 |
16 Oct 08 |
peter |
160 |
<< "and weighted test data\n"; |
1586 |
16 Oct 08 |
peter |
161 |
utility::MatrixWeighted xw = data_weighted(); |
1586 |
16 Oct 08 |
peter |
162 |
xw(2,0).weight()=0; |
4200 |
19 Aug 22 |
peter |
163 |
|
1586 |
16 Oct 08 |
peter |
164 |
classifier::MatrixLookupWeighted mlw1(xw); |
1586 |
16 Oct 08 |
peter |
165 |
classifier::KNN<statistics::EuclideanDistance> knn1; |
1586 |
16 Oct 08 |
peter |
166 |
knn1.k(3); |
1586 |
16 Oct 08 |
peter |
167 |
utility::Matrix data1 = data(); |
1586 |
16 Oct 08 |
peter |
168 |
classifier::MatrixLookup ml1(data1); |
1586 |
16 Oct 08 |
peter |
169 |
classifier::Target target1(vec_target()); |
1586 |
16 Oct 08 |
peter |
170 |
knn1.train(ml1,target1); |
1586 |
16 Oct 08 |
peter |
171 |
utility::Matrix prediction1; |
4200 |
19 Aug 22 |
peter |
172 |
knn1.predict(mlw1,prediction1); |
1586 |
16 Oct 08 |
peter |
173 |
utility::Matrix result1(2,4); |
1586 |
16 Oct 08 |
peter |
174 |
result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=2.0; |
1586 |
16 Oct 08 |
peter |
175 |
result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=1.0; |
1112 |
21 Feb 08 |
markus |
176 |
result1(0,0)=1.0; |
1112 |
21 Feb 08 |
markus |
177 |
result1(1,0)=2.0; |
4200 |
19 Aug 22 |
peter |
178 |
suite.add(suite.equal_range(result1.begin(), result1.end(), |
1239 |
16 Mar 08 |
peter |
179 |
prediction1.begin(), 1)); |
1586 |
16 Oct 08 |
peter |
180 |
} |
916 |
30 Sep 07 |
peter |
181 |
|
1586 |
16 Oct 08 |
peter |
182 |
void test_weighted(test::Suite& suite) |
1586 |
16 Oct 08 |
peter |
183 |
{ |
1107 |
19 Feb 08 |
markus |
184 |
//////////////////////////////////////////////////////////////// |
4200 |
19 Aug 22 |
peter |
// A test of training and test both weighted |
1107 |
19 Feb 08 |
markus |
186 |
//////////////////////////////////////////////////////////////// |
1239 |
16 Mar 08 |
peter |
187 |
suite.err() << "test of predictions using weighted training and test data\n"; |
1239 |
16 Mar 08 |
peter |
188 |
suite.err() << "... uniform neighbor weighting" << std::endl; |
1586 |
16 Oct 08 |
peter |
189 |
utility::MatrixWeighted xw = data_weighted(); |
1586 |
16 Oct 08 |
peter |
190 |
xw(2,0).weight()=0; |
1586 |
16 Oct 08 |
peter |
191 |
xw(0,1).weight()=0; |
1586 |
16 Oct 08 |
peter |
192 |
classifier::MatrixLookupWeighted mlw1(xw); |
4200 |
19 Aug 22 |
peter |
193 |
|
1586 |
16 Oct 08 |
peter |
194 |
utility::MatrixWeighted xw2 = data_weighted(); |
1586 |
16 Oct 08 |
peter |
195 |
xw2(2,3).weight()=0; |
1586 |
16 Oct 08 |
peter |
196 |
classifier::MatrixLookupWeighted mlw2(xw2); |
1157 |
26 Feb 08 |
markus |
197 |
classifier::KNN<statistics::EuclideanDistance> knn2; |
1107 |
19 Feb 08 |
markus |
198 |
knn2.k(3); |
1586 |
16 Oct 08 |
peter |
199 |
classifier::Target target1(vec_target()); |
1157 |
26 Feb 08 |
markus |
200 |
knn2.train(mlw2,target1); |
1586 |
16 Oct 08 |
peter |
201 |
utility::Matrix prediction1; |
4200 |
19 Aug 22 |
peter |
202 |
knn2.predict(mlw1,prediction1); |
1586 |
16 Oct 08 |
peter |
203 |
utility::Matrix result1(2,4); |
1586 |
16 Oct 08 |
peter |
204 |
result1(0,0)=result1(0,1)=result1(1,2)=result1(1,3)=2.0; |
1586 |
16 Oct 08 |
peter |
205 |
result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=1.0; |
1586 |
16 Oct 08 |
peter |
206 |
result1(0,0)=1.0; |
1586 |
16 Oct 08 |
peter |
207 |
result1(1,0)=2.0; |
1112 |
21 Feb 08 |
markus |
208 |
result1(0,1)=1.0; |
1112 |
21 Feb 08 |
markus |
209 |
result1(1,1)=2.0; |
4200 |
19 Aug 22 |
peter |
210 |
suite.add(suite.equal_range(result1.begin(), result1.end(), |
1239 |
16 Mar 08 |
peter |
211 |
prediction1.begin(), 1)); |
1586 |
16 Oct 08 |
peter |
212 |
} |
1107 |
19 Feb 08 |
markus |
213 |
|
1107 |
19 Feb 08 |
markus |
214 |
|
1586 |
16 Oct 08 |
peter |
215 |
void test_reciprocal_ranks(test::Suite& suite) |
1586 |
16 Oct 08 |
peter |
216 |
{ |
1112 |
21 Feb 08 |
markus |
217 |
//////////////////////////////////////////////////////////////// |
4200 |
19 Aug 22 |
peter |
// A test of reciprocal ranks weighting with training and test both weighted |
1112 |
21 Feb 08 |
markus |
219 |
//////////////////////////////////////////////////////////////// |
1239 |
16 Mar 08 |
peter |
220 |
suite.err() << "... reciprokal rank neighbor weighting" << std::endl; |
1586 |
16 Oct 08 |
peter |
221 |
utility::MatrixWeighted xw2 = data_weighted(); |
1586 |
16 Oct 08 |
peter |
222 |
xw2(2,3).weight()=0; |
1586 |
16 Oct 08 |
peter |
223 |
classifier::MatrixLookupWeighted mlw2(xw2); |
1586 |
16 Oct 08 |
peter |
224 |
utility::MatrixWeighted xw3 = data_weighted(); |
1586 |
16 Oct 08 |
peter |
225 |
xw3(1,3).data()=7; |
1586 |
16 Oct 08 |
peter |
226 |
xw3(2,3).weight()=0; |
1586 |
16 Oct 08 |
peter |
227 |
classifier::MatrixLookupWeighted mlw3(xw3); |
1586 |
16 Oct 08 |
peter |
228 |
classifier::KNN<statistics::EuclideanDistance |
1586 |
16 Oct 08 |
peter |
229 |
,classifier::KNN_ReciprocalRank> knn3; |
1112 |
21 Feb 08 |
markus |
230 |
knn3.k(3); |
1586 |
16 Oct 08 |
peter |
231 |
classifier::Target target1(vec_target()); |
1157 |
26 Feb 08 |
markus |
232 |
knn3.train(mlw2,target1); |
1586 |
16 Oct 08 |
peter |
233 |
utility::Matrix prediction1; |
4200 |
19 Aug 22 |
peter |
234 |
knn3.predict(mlw3,prediction1); |
1586 |
16 Oct 08 |
peter |
235 |
utility::Matrix result1(2,4); |
1112 |
21 Feb 08 |
markus |
236 |
result1(0,0)=result1(1,3)=1.0; |
1112 |
21 Feb 08 |
markus |
237 |
result1(0,3)=result1(1,0)=5.0/6.0; |
1112 |
21 Feb 08 |
markus |
238 |
result1(0,2)=result1(1,1)=1.0/2.0; |
1112 |
21 Feb 08 |
markus |
239 |
result1(0,1)=result1(1,2)=4.0/3.0; |
4200 |
19 Aug 22 |
peter |
240 |
suite.add(suite.equal_range(result1.begin(), result1.end(), |
1239 |
16 Mar 08 |
peter |
241 |
prediction1.begin(), 1)); |
1586 |
16 Oct 08 |
peter |
242 |
} |
1112 |
21 Feb 08 |
markus |
243 |
|
1586 |
16 Oct 08 |
peter |
244 |
void test_reciprocal_distance(test::Suite& suite) |
1586 |
16 Oct 08 |
peter |
245 |
{ |
1112 |
21 Feb 08 |
markus |
246 |
//////////////////////////////////////////////////////////////// |
4200 |
19 Aug 22 |
peter |
// A test of reciprocal distance weighting with training and test both weighted |
1112 |
21 Feb 08 |
markus |
248 |
//////////////////////////////////////////////////////////////// |
1239 |
16 Mar 08 |
peter |
249 |
suite.err() << "... reciprocal distance neighbor weighting" << std::endl; |
4200 |
19 Aug 22 |
peter |
250 |
classifier::KNN<statistics::EuclideanDistance,classifier::KNN_ReciprocalDistance> |
1157 |
26 Feb 08 |
markus |
251 |
knn4; |
1112 |
21 Feb 08 |
markus |
252 |
knn4.k(3); |
1586 |
16 Oct 08 |
peter |
253 |
utility::MatrixWeighted xw2 = data_weighted(); |
1586 |
16 Oct 08 |
peter |
254 |
xw2(2,3).weight()=0; |
1586 |
16 Oct 08 |
peter |
255 |
classifier::MatrixLookupWeighted mlw2(xw2); |
1586 |
16 Oct 08 |
peter |
256 |
utility::MatrixWeighted xw3 = data_weighted(); |
1586 |
16 Oct 08 |
peter |
257 |
xw3(1,3).data()=7; |
1586 |
16 Oct 08 |
peter |
258 |
xw3(2,3).weight()=0; |
1586 |
16 Oct 08 |
peter |
259 |
classifier::MatrixLookupWeighted mlw3(xw3); |
1586 |
16 Oct 08 |
peter |
260 |
classifier::Target target1(vec_target()); |
1157 |
26 Feb 08 |
markus |
261 |
knn4.train(mlw2,target1); |
1586 |
16 Oct 08 |
peter |
262 |
utility::Matrix prediction1; |
4200 |
19 Aug 22 |
peter |
263 |
knn4.predict(mlw3,prediction1); |
4200 |
19 Aug 22 |
peter |
264 |
if (!(std::isinf(prediction1(0,0)) && std::isinf(prediction1(0,1)) && |
4200 |
19 Aug 22 |
peter |
265 |
std::isinf(prediction1(1,2)) && |
1667 |
20 Dec 08 |
peter |
266 |
suite.equal_fix(prediction1(1,3), 1.0/3.6742346141747673, 1e-16) && |
4200 |
19 Aug 22 |
peter |
267 |
suite.equal_fix(prediction1(1,0), |
1667 |
20 Dec 08 |
peter |
268 |
1.0/2.82842712475+1.0/2.4494897427831779, 1e-11) |
1239 |
16 Mar 08 |
peter |
269 |
)){ |
1239 |
16 Mar 08 |
peter |
270 |
suite.err() << "Difference to expected prediction too large\n"; |
1239 |
16 Mar 08 |
peter |
271 |
suite.add(false); |
1112 |
21 Feb 08 |
markus |
272 |
} |
1586 |
16 Oct 08 |
peter |
273 |
} |
1112 |
21 Feb 08 |
markus |
274 |
|
1142 |
25 Feb 08 |
markus |
275 |
|
1586 |
16 Oct 08 |
peter |
276 |
void test_no_samples(test::Suite& suite) |
1586 |
16 Oct 08 |
peter |
277 |
{ |
1142 |
25 Feb 08 |
markus |
278 |
//////////////////////////////////////////////////////////////// |
1142 |
25 Feb 08 |
markus |
// A test of when a class has no training samples, should give nan |
1142 |
25 Feb 08 |
markus |
// in predictions. Also tests that k is reduced if not enough |
1142 |
25 Feb 08 |
markus |
// training samples. |
1142 |
25 Feb 08 |
markus |
282 |
//////////////////////////////////////////////////////////////// |
1142 |
25 Feb 08 |
markus |
//Keep only the second class in the training samples |
1142 |
25 Feb 08 |
markus |
284 |
std::vector<size_t> ind(2,2); |
1142 |
25 Feb 08 |
markus |
285 |
ind[1]=3; |
1586 |
16 Oct 08 |
peter |
286 |
classifier::Target target1(vec_target()); |
1142 |
25 Feb 08 |
markus |
287 |
classifier::Target target2(target1,utility::Index(ind)); |
1586 |
16 Oct 08 |
peter |
288 |
|
1586 |
16 Oct 08 |
peter |
289 |
utility::MatrixWeighted xw = data_weighted(); |
1586 |
16 Oct 08 |
peter |
290 |
xw(2,3).weight()=0.0; |
1586 |
16 Oct 08 |
peter |
291 |
|
1586 |
16 Oct 08 |
peter |
292 |
classifier::MatrixLookupWeighted mlw4(xw, utility::Index(xw.rows()), |
1484 |
09 Sep 08 |
peter |
293 |
utility::Index(ind)); |
1157 |
26 Feb 08 |
markus |
294 |
classifier::KNN<statistics::EuclideanDistance> knn5; |
1142 |
25 Feb 08 |
markus |
295 |
knn5.k(3); |
1157 |
26 Feb 08 |
markus |
296 |
knn5.train(mlw4,target2); |
1586 |
16 Oct 08 |
peter |
297 |
utility::MatrixWeighted xw3 = data_weighted(); |
1586 |
16 Oct 08 |
peter |
298 |
xw3(1,3).data()=7; |
1586 |
16 Oct 08 |
peter |
299 |
xw3(2,3).weight()=0; |
1586 |
16 Oct 08 |
peter |
300 |
classifier::MatrixLookupWeighted mlw3(xw3); |
1586 |
16 Oct 08 |
peter |
301 |
utility::Matrix prediction1; |
4200 |
19 Aug 22 |
peter |
302 |
knn5.predict(mlw3,prediction1); |
4200 |
19 Aug 22 |
peter |
303 |
if (!(std::isnan(prediction1(0,0)) && std::isnan(prediction1(0,1)) && |
1142 |
25 Feb 08 |
markus |
304 |
std::isnan(prediction1(0,2)) && std::isnan(prediction1(0,3)) && |
1239 |
16 Mar 08 |
peter |
305 |
suite.equal(prediction1(1,0),2.0) && |
1239 |
16 Mar 08 |
peter |
306 |
suite.equal(prediction1(1,1),2.0) && |
1239 |
16 Mar 08 |
peter |
307 |
suite.equal(prediction1(1,2),2.0) && |
1239 |
16 Mar 08 |
peter |
308 |
suite.equal(prediction1(1,3),2.0) )) { |
1239 |
16 Mar 08 |
peter |
309 |
suite.err() << "Difference to expected prediction too large\n"; |
1239 |
16 Mar 08 |
peter |
310 |
suite.add(false); |
1142 |
25 Feb 08 |
markus |
311 |
} |
1586 |
16 Oct 08 |
peter |
312 |
} |
1142 |
25 Feb 08 |
markus |
313 |
|
1586 |
16 Oct 08 |
peter |
314 |
void test_no_features(test::Suite& suite) |
1586 |
16 Oct 08 |
peter |
315 |
{ |
1142 |
25 Feb 08 |
markus |
316 |
//////////////////////////////////////////////////////////////// |
1142 |
25 Feb 08 |
markus |
// A test of when a test sample has no variables with non-zero |
1155 |
26 Feb 08 |
markus |
// weights in common with training samples: should not vote |
1142 |
25 Feb 08 |
markus |
319 |
//////////////////////////////////////////////////////////////// |
1239 |
16 Mar 08 |
peter |
320 |
suite.err() << "test of predictions with nan distances (set to infinity in KNN)\n"; |
1586 |
16 Oct 08 |
peter |
321 |
utility::MatrixWeighted xw1 = data_weighted(); |
1586 |
16 Oct 08 |
peter |
322 |
xw1(1,0).weight()=xw1(1,1).weight()=xw1(2,0).weight()=xw1(2,1).weight()=0.0; |
1586 |
16 Oct 08 |
peter |
323 |
classifier::MatrixLookupWeighted mlw1(xw1); |
1586 |
16 Oct 08 |
peter |
324 |
|
1157 |
26 Feb 08 |
markus |
325 |
classifier::KNN<statistics::EuclideanDistance> knn6; |
1142 |
25 Feb 08 |
markus |
326 |
knn6.k(3); |
1586 |
16 Oct 08 |
peter |
327 |
classifier::Target target1(vec_target()); |
1157 |
26 Feb 08 |
markus |
328 |
knn6.train(mlw1,target1); |
1586 |
16 Oct 08 |
peter |
329 |
|
1586 |
16 Oct 08 |
peter |
330 |
utility::MatrixWeighted xw3 = data_weighted(); |
1586 |
16 Oct 08 |
peter |
331 |
xw3(1,3).data()=7; |
1586 |
16 Oct 08 |
peter |
332 |
xw3(0,0).weight()=0; |
1586 |
16 Oct 08 |
peter |
333 |
classifier::MatrixLookupWeighted mlw3(xw3); |
1586 |
16 Oct 08 |
peter |
334 |
utility::Matrix prediction1; |
4200 |
19 Aug 22 |
peter |
335 |
knn6.predict(mlw3,prediction1); |
1586 |
16 Oct 08 |
peter |
336 |
utility::Matrix result1(2,4); |
1155 |
26 Feb 08 |
markus |
337 |
result1(0,0)=0; |
1155 |
26 Feb 08 |
markus |
338 |
result1(0,2)=result1(1,1)=result1(1,3)=1.0; |
1155 |
26 Feb 08 |
markus |
339 |
result1(0,1)=result1(0,3)=result1(1,0)=result1(1,2)=2.0; |
4200 |
19 Aug 22 |
peter |
340 |
suite.add(suite.equal_range(result1.begin(), result1.end(), |
1239 |
16 Mar 08 |
peter |
341 |
prediction1.begin(), 1)); |
903 |
27 Sep 07 |
markus |
342 |
} |
903 |
27 Sep 07 |
markus |
343 |
|
1586 |
16 Oct 08 |
peter |
344 |
std::vector<std::string> vec_target(void) |
1586 |
16 Oct 08 |
peter |
345 |
{ |
1586 |
16 Oct 08 |
peter |
346 |
std::vector<std::string> vec1(4, "pos"); |
1586 |
16 Oct 08 |
peter |
347 |
vec1[0]="neg"; |
1586 |
16 Oct 08 |
peter |
348 |
vec1[1]="neg"; |
1586 |
16 Oct 08 |
peter |
349 |
return vec1; |
1586 |
16 Oct 08 |
peter |
350 |
} |