source: trunk/yat/classifier/NBC.h @ 1169

Last change on this file since 1169 was 1169, checked in by Peter, 14 years ago

refs #343 moving data to inherited classes and using SmartPtr?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1#ifndef _theplu_yat_classifier_nbc_
2#define _theplu_yat_classifier_nbc_
3
4// $Id: NBC.h 1169 2008-02-26 22:09:04Z 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://trac.thep.lu.se/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
31namespace theplu {
32namespace yat {
33namespace classifier { 
34
35  class MatrixLookup;
36  class MatrixLookupWeighted;
37  class Target;
38
39  /**
40     @brief Naive Bayesian Classifier.
41 
42     Each class is modelled as a multinormal distribution with
43     features being independent: \f$ p(x|c) = \prod
44     \frac{1}{\sqrt{2\pi\sigma_i^2}} \exp \left(
45     \frac{(x_i-m_i)^2}{2\sigma_i^2)} \right)\f$
46  */
47  class NBC : public SupervisedClassifier
48  {
49 
50  public:
51    ///
52    /// @brief Constructor
53    ///
54    NBC(void);
55   
56
57    ///
58    /// @brief Destructor
59    ///
60    virtual ~NBC();
61
62
63    NBC* make_classifier(void) const;
64   
65    ///
66    /// Train the classifier using training data and targets.
67    ///
68    /// For each class mean and variance are estimated for each
69    /// feature (see Averager and AveragerWeighted for details).
70    ///
71    /// If variance can not be estimated (only one valid data point)
72    /// for a feature and label, then that feature is ignored for that
73    /// specific label.
74    ///
75    void train(const MatrixLookup&, const Target&);
76
77    ///
78    /// Train the classifier using weighted training data and targets.
79    ///
80    void train(const MatrixLookupWeighted&, const Target&);
81
82
83   
84    /**
85       Each sample (column) in \a data is predicted and predictions
86       are returned in the corresponding column in passed \a res. Each
87       row in \a res corresponds to a class. The prediction is the
88       estimated probability that sample belong to class \f$ j \f$
89
90       \f$ P_j = \frac{1}{Z}\prod_i{\frac{1}{\sqrt{2\pi\sigma_i^2}}}
91       \exp(\frac{(x_i-\mu_i)^2}{\sigma_i^2})\f$, where \f$ \mu_i
92       \f$ and \f$ \sigma_i^2 \f$ are the estimated mean and variance,
93       respectively. If a \f$ \sigma_i \f$ could not be estimated
94       during training, corresponding factor is set to unity, in other
95       words, that feature is ignored for the prediction of that
96       particular class. Z is chosen such that total probability, \f$
97       \sum P_j \f$, equals unity.
98    */
99    void predict(const MatrixLookup& data, utility::Matrix& res) const;
100
101    /**
102       Each sample (column) in \a data is predicted and predictions
103       are returned in the corresponding column in passed \a res. Each
104       row in \a res corresponds to a class. The prediction is the
105       estimated probability that sample belong to class \f$ j \f$
106     */
107    void predict(const MatrixLookupWeighted& data, utility::Matrix& res) const;
108
109
110  private:
111    void standardize_lnP(utility::Matrix& prediction) const;
112
113    utility::Matrix centroids_;
114    utility::Matrix sigma2_;
115
116    double sum_logsigma(size_t i) const;
117
118
119  };
120 
121}}} // of namespace classifier, yat, and theplu
122
123#endif
Note: See TracBrowser for help on using the repository browser.