4200 |
19 Aug 22 |
peter |
1 |
#ifndef _theplu_yat_classifier_nbc_ |
4200 |
19 Aug 22 |
peter |
2 |
#define _theplu_yat_classifier_nbc_ |
662 |
27 Sep 06 |
peter |
3 |
|
662 |
27 Sep 06 |
peter |
// $Id$ |
662 |
27 Sep 06 |
peter |
5 |
|
675 |
10 Oct 06 |
jari |
6 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér |
662 |
27 Sep 06 |
peter |
10 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
12 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
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 |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
17 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
22 |
|
675 |
10 Oct 06 |
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/>. |
675 |
10 Oct 06 |
jari |
25 |
*/ |
675 |
10 Oct 06 |
jari |
26 |
|
680 |
11 Oct 06 |
jari |
27 |
#include "SupervisedClassifier.h" |
1121 |
22 Feb 08 |
peter |
28 |
#include "yat/utility/Matrix.h" |
675 |
10 Oct 06 |
jari |
29 |
|
662 |
27 Sep 06 |
peter |
30 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
31 |
namespace yat { |
4200 |
19 Aug 22 |
peter |
32 |
namespace classifier { |
662 |
27 Sep 06 |
peter |
33 |
|
662 |
27 Sep 06 |
peter |
34 |
class MatrixLookup; |
662 |
27 Sep 06 |
peter |
35 |
class MatrixLookupWeighted; |
662 |
27 Sep 06 |
peter |
36 |
class Target; |
662 |
27 Sep 06 |
peter |
37 |
|
767 |
22 Feb 07 |
peter |
38 |
/** |
1152 |
25 Feb 08 |
peter |
@brief Naive Bayesian Classifier. |
4200 |
19 Aug 22 |
peter |
40 |
|
767 |
22 Feb 07 |
peter |
Each class is modelled as a multinormal distribution with |
1184 |
28 Feb 08 |
peter |
features being independent: \f$ P(x|c) \propto \prod |
767 |
22 Feb 07 |
peter |
\frac{1}{\sqrt{2\pi\sigma_i^2}} \exp \left( |
1184 |
28 Feb 08 |
peter |
-\frac{(x_i-\mu_i)^2}{2\sigma_i^2)} \right)\f$ |
767 |
22 Feb 07 |
peter |
45 |
*/ |
662 |
27 Sep 06 |
peter |
46 |
class NBC : public SupervisedClassifier |
662 |
27 Sep 06 |
peter |
47 |
{ |
4200 |
19 Aug 22 |
peter |
48 |
|
662 |
27 Sep 06 |
peter |
49 |
public: |
662 |
27 Sep 06 |
peter |
50 |
/// |
4200 |
19 Aug 22 |
peter |
/// @brief Constructor |
662 |
27 Sep 06 |
peter |
52 |
/// |
1157 |
26 Feb 08 |
markus |
53 |
NBC(void); |
1157 |
26 Feb 08 |
markus |
54 |
|
4200 |
19 Aug 22 |
peter |
55 |
|
662 |
27 Sep 06 |
peter |
56 |
/// |
1157 |
26 Feb 08 |
markus |
/// @brief Destructor |
1157 |
26 Feb 08 |
markus |
58 |
/// |
662 |
27 Sep 06 |
peter |
59 |
virtual ~NBC(); |
662 |
27 Sep 06 |
peter |
60 |
|
722 |
27 Dec 06 |
markus |
61 |
|
1157 |
26 Feb 08 |
markus |
62 |
NBC* make_classifier(void) const; |
4200 |
19 Aug 22 |
peter |
63 |
|
662 |
27 Sep 06 |
peter |
64 |
/// |
1200 |
05 Mar 08 |
peter |
/// \brief Train the NBC using training data and targets. |
662 |
27 Sep 06 |
peter |
66 |
/// |
767 |
22 Feb 07 |
peter |
/// For each class mean and variance are estimated for each |
1184 |
28 Feb 08 |
peter |
/// feature (see statistics::Averager for details). |
767 |
22 Feb 07 |
peter |
69 |
/// |
1184 |
28 Feb 08 |
peter |
/// If there is only one (or zero) samples in a class, parameters |
1184 |
28 Feb 08 |
peter |
/// cannot be estimated. In that case, parameters are set to NaN |
1184 |
28 Feb 08 |
peter |
/// for that particular class. |
960 |
10 Oct 07 |
peter |
73 |
/// |
1157 |
26 Feb 08 |
markus |
74 |
void train(const MatrixLookup&, const Target&); |
662 |
27 Sep 06 |
peter |
75 |
|
1157 |
26 Feb 08 |
markus |
76 |
/// |
1200 |
05 Mar 08 |
peter |
/// \brief Train the NBC using weighted training data and |
1184 |
28 Feb 08 |
peter |
/// targets. |
1157 |
26 Feb 08 |
markus |
79 |
/// |
1184 |
28 Feb 08 |
peter |
/// For each class mean and variance are estimated for each |
1184 |
28 Feb 08 |
peter |
/// feature (see statistics::AveragerWeighted for details). |
1184 |
28 Feb 08 |
peter |
82 |
/// |
1184 |
28 Feb 08 |
peter |
/// To estimate the parameters of a class, each feature of the |
1184 |
28 Feb 08 |
peter |
/// class must have at least two non-zero data points. Otherwise |
1184 |
28 Feb 08 |
peter |
/// the parameters are set to NaN and any prediction will result |
1184 |
28 Feb 08 |
peter |
/// in NaN for that particular class. |
1184 |
28 Feb 08 |
peter |
87 |
/// |
1157 |
26 Feb 08 |
markus |
88 |
void train(const MatrixLookupWeighted&, const Target&); |
4200 |
19 Aug 22 |
peter |
89 |
|
808 |
15 Mar 07 |
peter |
90 |
/** |
1184 |
28 Feb 08 |
peter |
\brief Predict samples using unweighted data |
1184 |
28 Feb 08 |
peter |
92 |
|
813 |
16 Mar 07 |
peter |
Each sample (column) in \a data is predicted and predictions |
1184 |
28 Feb 08 |
peter |
are returned in the corresponding column in passed \a |
1184 |
28 Feb 08 |
peter |
result. Each row in \a result corresponds to a class. The |
1184 |
28 Feb 08 |
peter |
prediction is the estimated probability that sample belong to |
1184 |
28 Feb 08 |
peter |
class \f$ j \f$: |
813 |
16 Mar 07 |
peter |
98 |
|
1184 |
28 Feb 08 |
peter |
\f$ P_j = \frac{1}{Z}\prod_i\frac{1}{\sqrt{2\pi\sigma_i^2}} |
1184 |
28 Feb 08 |
peter |
\exp\left(-\frac{(x_i-\mu_i)^2}{2\sigma_i^2}\right)\f$, where \f$ \mu_i |
813 |
16 Mar 07 |
peter |
\f$ and \f$ \sigma_i^2 \f$ are the estimated mean and variance, |
1184 |
28 Feb 08 |
peter |
respectively. Z is chosen such that total probability equals unity, \f$ |
1184 |
28 Feb 08 |
peter |
\sum P_j = 1 \f$. |
1184 |
28 Feb 08 |
peter |
104 |
|
1184 |
28 Feb 08 |
peter |
\note If parameters could not be estimated during training, due |
1184 |
28 Feb 08 |
peter |
to lack of number of sufficient data points, the output for |
1184 |
28 Feb 08 |
peter |
that class is NaN and not included in calculation of |
1184 |
28 Feb 08 |
peter |
normalization factor \f$ Z \f$. |
808 |
15 Mar 07 |
peter |
109 |
*/ |
1184 |
28 Feb 08 |
peter |
110 |
void predict(const MatrixLookup& data, utility::Matrix& result) const; |
662 |
27 Sep 06 |
peter |
111 |
|
1160 |
26 Feb 08 |
markus |
112 |
/** |
1184 |
28 Feb 08 |
peter |
\brief Predict samples using weighted data |
1184 |
28 Feb 08 |
peter |
114 |
|
1169 |
26 Feb 08 |
peter |
Each sample (column) in \a data is predicted and predictions |
1184 |
28 Feb 08 |
peter |
are returned in the corresponding column in passed \a |
1184 |
28 Feb 08 |
peter |
result. Each row in \a result corresponds to a class. The |
1184 |
28 Feb 08 |
peter |
prediction is the estimated probability that sample belong to |
1184 |
28 Feb 08 |
peter |
class \f$ j \f$: |
1182 |
28 Feb 08 |
peter |
120 |
|
1184 |
28 Feb 08 |
peter |
\f$ P_j = \frac{1}{Z} \exp\left(-N\frac{\sum |
1200 |
05 Mar 08 |
peter |
{w_i(x_i-\mu_i)^2}/(2\sigma_i^2)}{\sum w_i}\right) |
1200 |
05 Mar 08 |
peter |
\prod_i\frac{1}{\sqrt{2\pi\sigma_i^2}}\f$, where \f$ \mu_i \f$ |
1200 |
05 Mar 08 |
peter |
and \f$ \sigma_i^2 \f$ are the estimated mean and variance, |
1200 |
05 Mar 08 |
peter |
respectively. Z is chosen such that total probability equals |
1200 |
05 Mar 08 |
peter |
unity, \f$ \sum P_j = 1 \f$. |
1184 |
28 Feb 08 |
peter |
127 |
|
1184 |
28 Feb 08 |
peter |
\note If parameters could not be estimated during training, due |
1184 |
28 Feb 08 |
peter |
to lack of number of sufficient data points, the output for |
1184 |
28 Feb 08 |
peter |
that class is NaN and not included in calculation of |
1184 |
28 Feb 08 |
peter |
normalization factor \f$ Z \f$. |
1160 |
26 Feb 08 |
markus |
132 |
*/ |
1184 |
28 Feb 08 |
peter |
133 |
void predict(const MatrixLookupWeighted& data,utility::Matrix& result) const; |
662 |
27 Sep 06 |
peter |
134 |
|
1160 |
26 Feb 08 |
markus |
135 |
|
662 |
27 Sep 06 |
peter |
136 |
private: |
1160 |
26 Feb 08 |
markus |
137 |
void standardize_lnP(utility::Matrix& prediction) const; |
1160 |
26 Feb 08 |
markus |
138 |
|
1121 |
22 Feb 08 |
peter |
139 |
utility::Matrix centroids_; |
1121 |
22 Feb 08 |
peter |
140 |
utility::Matrix sigma2_; |
662 |
27 Sep 06 |
peter |
141 |
|
959 |
10 Oct 07 |
peter |
142 |
double sum_logsigma(size_t i) const; |
959 |
10 Oct 07 |
peter |
143 |
|
959 |
10 Oct 07 |
peter |
144 |
|
662 |
27 Sep 06 |
peter |
145 |
}; |
4200 |
19 Aug 22 |
peter |
146 |
|
680 |
11 Oct 06 |
jari |
147 |
}}} // of namespace classifier, yat, and theplu |
662 |
27 Sep 06 |
peter |
148 |
|
662 |
27 Sep 06 |
peter |
149 |
#endif |