openGPMP
Open Source Mathematics Package
bayes_clf.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
39 #ifndef BAYES_CLF_HPP
40 #define BAYES_CLF_HPP
41 #include <map>
42 #include <string>
43 #include <unordered_map>
44 #include <vector>
45 
46 namespace gpmp {
47 
48 namespace ml {
49 
53 class BayesClf {
54  public:
58  double alpha;
59 
63  bool fit_prior;
67  std::unordered_map<std::string, double> class_probs;
71  std::unordered_map<std::string, std::vector<double>> feature_probs;
75  std::vector<double> class_log_prior;
76 
83  BayesClf(double alpha_param = 1.0,
84  bool fit_prior_param = true,
85  const std::vector<double> &class_prior = {});
86 
90  ~BayesClf();
97  void train(const std::vector<std::vector<double>> &data,
98  const std::vector<std::string> &labels);
99 
106  std::string predict(const std::vector<double> &newData) const;
107 
112  void display() const;
113 };
114 
124  public:
125  std::unordered_map<std::string, double> class_probs;
126  std::unordered_map<std::string, std::unordered_map<size_t, double>>
128  double alpha;
129 
134  BayesBernoulli(double alpha_param = 1.0) : alpha(alpha_param) {
135  }
136 
141  }
142 
149  void train(const std::vector<std::vector<size_t>> &data,
150  const std::vector<std::string> &labels);
157  std::string predict(const std::vector<size_t> &newData) const;
158 
163  void display() const;
164 };
165 
172 class BayesGauss {
173  public:
180  std::unordered_map<std::string, double> class_probs;
187  std::unordered_map<std::string, std::vector<double>> mean;
194  std::unordered_map<std::string, std::vector<double>> variance;
195 
199  BayesGauss() = default;
200 
204  ~BayesGauss() = default;
205 
212  void train(const std::vector<std::vector<double>> &data,
213  const std::vector<std::string> &labels);
214 
221  std::string predict(const std::vector<double> &newData) const;
222 
227  void display() const;
228 
229  private:
236  void mean_and_var(const std::vector<std::vector<double>> &data,
237  const std::vector<std::string> &labels);
238 };
239 
246  public:
251  double alpha;
257  bool fit_prior;
264  std::unordered_map<std::string, double> class_probs;
271  std::unordered_map<std::string, std::vector<double>> feature_probs;
277  std::vector<double> class_log_prior;
278 
285  BayesMultiNom(double alpha_param = 1.0,
286  bool fit_prior_param = true,
287  const std::vector<double> &class_prior = {});
288 
292  ~BayesMultiNom();
293 
300  void train(const std::vector<std::vector<size_t>> &data,
301  const std::vector<std::string> &labels);
302 
309  std::string predict(const std::vector<size_t> &new_data) const;
310 
315  void display() const;
316 };
317 
318 } // namespace ml
319 
320 } // namespace gpmp
321 
322 #endif
BayesBernoulli(double alpha_param=1.0)
Constructor for BayesBernoulli class.
Definition: bayes_clf.hpp:134
void train(const std::vector< std::vector< size_t >> &data, const std::vector< std::string > &labels)
Train the classifier with a set of labeled data.
Definition: bayes_clf.cpp:155
void display() const
Display the learned probabilities.
Definition: bayes_clf.cpp:212
std::string predict(const std::vector< size_t > &newData) const
Predict the class of a new data point.
Definition: bayes_clf.cpp:191
std::unordered_map< std::string, double > class_probs
Definition: bayes_clf.hpp:125
~BayesBernoulli()
Destructor for BayesBernoulli class.
Definition: bayes_clf.hpp:140
std::unordered_map< std::string, std::unordered_map< size_t, double > > feat_probs
Definition: bayes_clf.hpp:127
Bayes Classifier Class based on assumptions of independence.
Definition: bayes_clf.hpp:53
~BayesClf()
Destructor for BayesClf class.
Definition: bayes_clf.cpp:49
bool fit_prior
Whether to learn class prior probabilities or not.
Definition: bayes_clf.hpp:63
void train(const std::vector< std::vector< double >> &data, const std::vector< std::string > &labels)
Train the classifier with a set of labeled data.
Definition: bayes_clf.cpp:52
std::vector< double > class_log_prior
Vector of class log priors.
Definition: bayes_clf.hpp:75
void display() const
Display the learned probabilities.
Definition: bayes_clf.cpp:135
std::unordered_map< std::string, double > class_probs
Map of class labels to their probabilities.
Definition: bayes_clf.hpp:67
std::unordered_map< std::string, std::vector< double > > feature_probs
Map of class labels to their feature probabilities.
Definition: bayes_clf.hpp:71
std::string predict(const std::vector< double > &newData) const
Predict the class of a new data point.
Definition: bayes_clf.cpp:114
BayesClf(double alpha_param=1.0, bool fit_prior_param=true, const std::vector< double > &class_prior={})
Constructor for BayesClf class.
Definition: bayes_clf.cpp:42
double alpha
Additive smoothing parameter.
Definition: bayes_clf.hpp:58
BayesGauss()=default
Constructor for BayesGauss class.
std::unordered_map< std::string, std::vector< double > > mean
Map storing the mean values for each feature in each class.
Definition: bayes_clf.hpp:187
void display() const
Display the learned probabilities.
Definition: bayes_clf.cpp:323
void mean_and_var(const std::vector< std::vector< double >> &data, const std::vector< std::string > &labels)
Calculate the mean and variance for each class.
Definition: bayes_clf.cpp:245
std::string predict(const std::vector< double > &newData) const
Predict the class of a new data point.
Definition: bayes_clf.cpp:299
void train(const std::vector< std::vector< double >> &data, const std::vector< std::string > &labels)
Train the classifier with a set of labeled data.
Definition: bayes_clf.cpp:228
std::unordered_map< std::string, double > class_probs
Map storing the probabilities of each class.
Definition: bayes_clf.hpp:180
~BayesGauss()=default
Destructor for BayesGauss class.
std::unordered_map< std::string, std::vector< double > > variance
Map storing the variance values for each feature in each class.
Definition: bayes_clf.hpp:194
void train(const std::vector< std::vector< size_t >> &data, const std::vector< std::string > &labels)
Train the classifier with a set of labeled data.
Definition: bayes_clf.cpp:354
std::vector< double > class_log_prior
Vector storing the logarithm of the class prior probabilities.
Definition: bayes_clf.hpp:277
std::unordered_map< std::string, std::vector< double > > feature_probs
Map storing the probabilities of features for each class.
Definition: bayes_clf.hpp:271
std::string predict(const std::vector< size_t > &new_data) const
Predict the class of a new data point.
Definition: bayes_clf.cpp:420
bool fit_prior
Flag indicating whether to learn class prior probabilities during training.
Definition: bayes_clf.hpp:257
std::unordered_map< std::string, double > class_probs
Map storing the probabilities of each class.
Definition: bayes_clf.hpp:264
~BayesMultiNom()
Destructor for BayesMultiNom class.
Definition: bayes_clf.cpp:351
void display() const
Display the learned probabilities.
Definition: bayes_clf.cpp:441
BayesMultiNom(double alpha_param=1.0, bool fit_prior_param=true, const std::vector< double > &class_prior={})
Constructor for BayesMultiNom class.
Definition: bayes_clf.cpp:344
double alpha
Additive smoothing parameter for the Multinomial distribution.
Definition: bayes_clf.hpp:251
The source C++ openGPMP namespace.