1 | #ifndef _theplu_yat_classifier_nbc_ |
---|
2 | #define _theplu_yat_classifier_nbc_ |
---|
3 | |
---|
4 | // $Id: NBC.h 1437 2008-08-25 17:55:00Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
8 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
9 | Copyright (C) 2008 Peter Johansson, Markus Ringnér |
---|
10 | |
---|
11 | This file is part of the yat library, http://dev.thep.lu.se/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 "SupervisedClassifier.h" |
---|
30 | #include "yat/utility/Matrix.h" |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace classifier { |
---|
35 | |
---|
36 | class MatrixLookup; |
---|
37 | class MatrixLookupWeighted; |
---|
38 | class Target; |
---|
39 | |
---|
40 | /** |
---|
41 | @brief Naive Bayesian Classifier. |
---|
42 | |
---|
43 | Each class is modelled as a multinormal distribution with |
---|
44 | features being independent: \f$ P(x|c) \propto \prod |
---|
45 | \frac{1}{\sqrt{2\pi\sigma_i^2}} \exp \left( |
---|
46 | -\frac{(x_i-\mu_i)^2}{2\sigma_i^2)} \right)\f$ |
---|
47 | */ |
---|
48 | class NBC : public SupervisedClassifier |
---|
49 | { |
---|
50 | |
---|
51 | public: |
---|
52 | /// |
---|
53 | /// @brief Constructor |
---|
54 | /// |
---|
55 | NBC(void); |
---|
56 | |
---|
57 | |
---|
58 | /// |
---|
59 | /// @brief Destructor |
---|
60 | /// |
---|
61 | virtual ~NBC(); |
---|
62 | |
---|
63 | |
---|
64 | NBC* make_classifier(void) const; |
---|
65 | |
---|
66 | /// |
---|
67 | /// \brief Train the NBC using training data and targets. |
---|
68 | /// |
---|
69 | /// For each class mean and variance are estimated for each |
---|
70 | /// feature (see statistics::Averager for details). |
---|
71 | /// |
---|
72 | /// If there is only one (or zero) samples in a class, parameters |
---|
73 | /// cannot be estimated. In that case, parameters are set to NaN |
---|
74 | /// for that particular class. |
---|
75 | /// |
---|
76 | void train(const MatrixLookup&, const Target&); |
---|
77 | |
---|
78 | /// |
---|
79 | /// \brief Train the NBC using weighted training data and |
---|
80 | /// targets. |
---|
81 | /// |
---|
82 | /// For each class mean and variance are estimated for each |
---|
83 | /// feature (see statistics::AveragerWeighted for details). |
---|
84 | /// |
---|
85 | /// To estimate the parameters of a class, each feature of the |
---|
86 | /// class must have at least two non-zero data points. Otherwise |
---|
87 | /// the parameters are set to NaN and any prediction will result |
---|
88 | /// in NaN for that particular class. |
---|
89 | /// |
---|
90 | void train(const MatrixLookupWeighted&, const Target&); |
---|
91 | |
---|
92 | /** |
---|
93 | \brief Predict samples using unweighted data |
---|
94 | |
---|
95 | Each sample (column) in \a data is predicted and predictions |
---|
96 | are returned in the corresponding column in passed \a |
---|
97 | result. Each row in \a result corresponds to a class. The |
---|
98 | prediction is the estimated probability that sample belong to |
---|
99 | class \f$ j \f$: |
---|
100 | |
---|
101 | \f$ P_j = \frac{1}{Z}\prod_i\frac{1}{\sqrt{2\pi\sigma_i^2}} |
---|
102 | \exp\left(-\frac{(x_i-\mu_i)^2}{2\sigma_i^2}\right)\f$, where \f$ \mu_i |
---|
103 | \f$ and \f$ \sigma_i^2 \f$ are the estimated mean and variance, |
---|
104 | respectively. Z is chosen such that total probability equals unity, \f$ |
---|
105 | \sum P_j = 1 \f$. |
---|
106 | |
---|
107 | \note If parameters could not be estimated during training, due |
---|
108 | to lack of number of sufficient data points, the output for |
---|
109 | that class is NaN and not included in calculation of |
---|
110 | normalization factor \f$ Z \f$. |
---|
111 | */ |
---|
112 | void predict(const MatrixLookup& data, utility::Matrix& result) const; |
---|
113 | |
---|
114 | /** |
---|
115 | \brief Predict samples using weighted data |
---|
116 | |
---|
117 | Each sample (column) in \a data is predicted and predictions |
---|
118 | are returned in the corresponding column in passed \a |
---|
119 | result. Each row in \a result corresponds to a class. The |
---|
120 | prediction is the estimated probability that sample belong to |
---|
121 | class \f$ j \f$: |
---|
122 | |
---|
123 | \f$ P_j = \frac{1}{Z} \exp\left(-N\frac{\sum |
---|
124 | {w_i(x_i-\mu_i)^2}/(2\sigma_i^2)}{\sum w_i}\right) |
---|
125 | \prod_i\frac{1}{\sqrt{2\pi\sigma_i^2}}\f$, where \f$ \mu_i \f$ |
---|
126 | and \f$ \sigma_i^2 \f$ are the estimated mean and variance, |
---|
127 | respectively. Z is chosen such that total probability equals |
---|
128 | unity, \f$ \sum P_j = 1 \f$. |
---|
129 | |
---|
130 | \note If parameters could not be estimated during training, due |
---|
131 | to lack of number of sufficient data points, the output for |
---|
132 | that class is NaN and not included in calculation of |
---|
133 | normalization factor \f$ Z \f$. |
---|
134 | */ |
---|
135 | void predict(const MatrixLookupWeighted& data,utility::Matrix& result) const; |
---|
136 | |
---|
137 | |
---|
138 | private: |
---|
139 | void standardize_lnP(utility::Matrix& prediction) const; |
---|
140 | |
---|
141 | utility::Matrix centroids_; |
---|
142 | utility::Matrix sigma2_; |
---|
143 | |
---|
144 | double sum_logsigma(size_t i) const; |
---|
145 | |
---|
146 | |
---|
147 | }; |
---|
148 | |
---|
149 | }}} // of namespace classifier, yat, and theplu |
---|
150 | |
---|
151 | #endif |
---|