1 | #ifndef _theplu_yat_classifier_ncc_ |
---|
2 | #define _theplu_yat_classifier_ncc_ |
---|
3 | |
---|
4 | // $Id$ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Markus Ringnér, Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
9 | Copyright (C) 2007 Peter Johansson |
---|
10 | |
---|
11 | This file is part of the yat library, http://trac.thep.lu.se/trac/yat |
---|
12 | |
---|
13 | The yat library is free software; you can redistribute it and/or |
---|
14 | modify it under the terms of the GNU General Public License as |
---|
15 | published by the Free Software Foundation; either version 2 of the |
---|
16 | License, or (at your option) any later version. |
---|
17 | |
---|
18 | The yat library is distributed in the hope that it will be useful, |
---|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
21 | General Public License for more details. |
---|
22 | |
---|
23 | You should have received a copy of the GNU General Public License |
---|
24 | along with this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
26 | 02111-1307, USA. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "yat/utility/matrix.h" |
---|
30 | #include "SupervisedClassifier.h" |
---|
31 | |
---|
32 | #include <map> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | |
---|
37 | namespace utlitity { |
---|
38 | class vector; |
---|
39 | } |
---|
40 | |
---|
41 | namespace statistics { |
---|
42 | class Distance; |
---|
43 | } |
---|
44 | |
---|
45 | namespace classifier { |
---|
46 | |
---|
47 | class DataLookup1D; |
---|
48 | class DataLookup2D; |
---|
49 | class MatrixLookup; |
---|
50 | class MatrixLookupWeighted; |
---|
51 | class Target; |
---|
52 | |
---|
53 | /// |
---|
54 | /// @brief Class for Nearest Centroid Classification. |
---|
55 | /// |
---|
56 | |
---|
57 | class NCC : public SupervisedClassifier |
---|
58 | { |
---|
59 | |
---|
60 | public: |
---|
61 | /// |
---|
62 | /// Constructor taking the training data, the target vector, and |
---|
63 | /// the distance measure as input. |
---|
64 | /// |
---|
65 | NCC(const MatrixLookup&, const Target&, const statistics::Distance&); |
---|
66 | |
---|
67 | /// |
---|
68 | /// Constructor taking the training data with weights, the target |
---|
69 | /// vector, the distance measure, and a weight matrix for the |
---|
70 | /// training data as input. |
---|
71 | /// |
---|
72 | NCC(const MatrixLookupWeighted&, const Target&, const statistics::Distance&); |
---|
73 | |
---|
74 | virtual ~NCC(); |
---|
75 | |
---|
76 | /// |
---|
77 | /// @return the centroids for each class as columns in a matrix. |
---|
78 | /// |
---|
79 | const utility::matrix& centroids(void) const; |
---|
80 | |
---|
81 | const DataLookup2D& data(void) const; |
---|
82 | |
---|
83 | SupervisedClassifier* make_classifier(const DataLookup2D&, |
---|
84 | const Target&) const; |
---|
85 | |
---|
86 | /// |
---|
87 | /// Train the classifier using the training data. Centroids are |
---|
88 | /// calculated for each class. |
---|
89 | /// |
---|
90 | /// @return true if training succedeed. |
---|
91 | /// |
---|
92 | bool train(); |
---|
93 | |
---|
94 | |
---|
95 | /// |
---|
96 | /// Calculate the distance to each centroid for test samples |
---|
97 | /// |
---|
98 | void predict(const DataLookup2D&, utility::matrix&) const; |
---|
99 | |
---|
100 | |
---|
101 | private: |
---|
102 | utility::matrix centroids_; |
---|
103 | const statistics::Distance& distance_; |
---|
104 | |
---|
105 | // data_ has to be of type DataLookup2D to accomodate both |
---|
106 | // MatrixLookup and MatrixLookupWeighted |
---|
107 | const DataLookup2D& data_; |
---|
108 | |
---|
109 | /// |
---|
110 | /// Calculate the distance to each centroid for a test sample |
---|
111 | /// |
---|
112 | void predict(const utility::vector&, const utility::vector&, |
---|
113 | utility::vector&) const; |
---|
114 | |
---|
115 | }; |
---|
116 | |
---|
117 | /// |
---|
118 | /// The output operator for the NCC class. |
---|
119 | /// |
---|
120 | // std::ostream& operator<< (std::ostream&, const NCC&); |
---|
121 | |
---|
122 | |
---|
123 | }}} // of namespace classifier, yat, and theplu |
---|
124 | |
---|
125 | #endif |
---|