1 | #ifndef _theplu_yat_classifier_nbc_ |
---|
2 | #define _theplu_yat_classifier_nbc_ |
---|
3 | |
---|
4 | // $Id: NBC.h 831 2007-03-27 13:22:09Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson |
---|
8 | Copyright (C) 2007 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 2 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with this program; if not, write to the Free Software |
---|
24 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
25 | 02111-1307, USA. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "SupervisedClassifier.h" |
---|
29 | #include "yat/utility/matrix.h" |
---|
30 | |
---|
31 | namespace theplu { |
---|
32 | namespace yat { |
---|
33 | namespace classifier { |
---|
34 | |
---|
35 | class DataLookup1D; |
---|
36 | class DataLookup2D; |
---|
37 | class MatrixLookup; |
---|
38 | class MatrixLookupWeighted; |
---|
39 | class Target; |
---|
40 | |
---|
41 | /** |
---|
42 | @brief Naive Bayesian Classification. |
---|
43 | |
---|
44 | Each class is modelled as a multinormal distribution with |
---|
45 | features being independent: \f$ p(x|c) = \prod |
---|
46 | \frac{1}{\sqrt{2\pi\sigma_i^2}} \exp \left( |
---|
47 | \frac{(x_i-m_i)^2}{2\sigma_i^2)} \right)\f$ |
---|
48 | */ |
---|
49 | class NBC : public SupervisedClassifier |
---|
50 | { |
---|
51 | |
---|
52 | public: |
---|
53 | /// |
---|
54 | /// Constructor taking the training data, the target vector, and |
---|
55 | /// the distance measure as input. |
---|
56 | /// |
---|
57 | NBC(const MatrixLookup&, const Target&); |
---|
58 | |
---|
59 | /// |
---|
60 | /// Constructor taking the training data with weights, the target |
---|
61 | /// vector, the distance measure, and a weight matrix for the |
---|
62 | /// training data as input. |
---|
63 | /// |
---|
64 | NBC(const MatrixLookupWeighted&, const Target&); |
---|
65 | |
---|
66 | virtual ~NBC(); |
---|
67 | |
---|
68 | const DataLookup2D& data(void) const; |
---|
69 | |
---|
70 | |
---|
71 | SupervisedClassifier* make_classifier(const DataLookup2D&, |
---|
72 | const Target&) const; |
---|
73 | |
---|
74 | /// |
---|
75 | /// Train the classifier using the training data. |
---|
76 | /// |
---|
77 | /// For each class mean and variance are estimated for each |
---|
78 | /// feature (see Averager and AveragerWeighted for details). |
---|
79 | /// |
---|
80 | /// @return true if training succedeed. |
---|
81 | /// |
---|
82 | bool train(); |
---|
83 | |
---|
84 | |
---|
85 | /** |
---|
86 | Each sample (column) in \a data is predicted and predictions |
---|
87 | are returned in the corresponding column in passed \a res. Each |
---|
88 | row in \a res corresponds to a class. The prediction is the |
---|
89 | estimated probability that sample belong to class \f$ j \f$ |
---|
90 | |
---|
91 | \f$ P_j = \frac{1}{Z}\prod_i{\frac{1}{\sigma_i}} |
---|
92 | \exp(\frac{w_i(x_i-\mu_i)^2}{\sigma_i^2})\f$, where \f$ \mu_i |
---|
93 | \f$ and \f$ \sigma_i^2 \f$ are the estimated mean and variance, |
---|
94 | respectively. If \a data is a MatrixLookup is equivalent to |
---|
95 | using all weight equal to unity. |
---|
96 | */ |
---|
97 | void predict(const DataLookup2D& data, utility::matrix& res) const; |
---|
98 | |
---|
99 | |
---|
100 | private: |
---|
101 | utility::matrix centroids_; |
---|
102 | utility::matrix sigma2_; |
---|
103 | const DataLookup2D& data_; |
---|
104 | |
---|
105 | }; |
---|
106 | |
---|
107 | }}} // of namespace classifier, yat, and theplu |
---|
108 | |
---|
109 | #endif |
---|