openGPMP
Open Source Mathematics Package
Functions
bayes.cpp File Reference
#include <iostream>
#include <openGPMP/ml.hpp>
#include <string>
#include <vector>

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

◆ main()

int main ( void  )

Definition at line 6 of file bayes.cpp.

6  {
7  // Example for BayesBernoulli class
8  std::vector<std::vector<size_t>> bernoulli_train_data = {
9  {1, 0, 1},
10  {0, 1, 0},
11  {1, 1, 1},
12  {0, 0, 1},
13  };
14 
15  std::vector<std::string> bernoulli_labels = {"spam", "ham", "spam", "ham"};
16 
17  gpmp::ml::BayesBernoulli bernoulli_clf(1.0);
18  bernoulli_clf.train(bernoulli_train_data, bernoulli_labels);
19 
20  std::vector<size_t> bernoulli_new_data = {1, 0, 0};
21  std::string bernoulli_prediction =
22  bernoulli_clf.predict(bernoulli_new_data);
23 
24  std::cout << "Predicted Class (BayesBernoulli): " << bernoulli_prediction
25  << "\n";
26 
27  // print learned probabilities for BayesBernoulli
28  bernoulli_clf.display();
29 
30  // example for BayesClf class
31  std::vector<std::vector<double>> clf_train_data = {
32  {1.0, 2.0, 3.0},
33  {2.0, 3.0, 4.0},
34  {3.0, 4.0, 5.0},
35  {4.0, 5.0, 6.0},
36  };
37 
38  std::vector<std::string> clf_labels = {"classA",
39  "classB",
40  "classA",
41  "classB"};
42  // example with predefined class priors
43  gpmp::ml::BayesClf bayes_clf(1.0, true, {0.4, 0.6});
44  bayes_clf.train(clf_train_data, clf_labels);
45 
46  std::vector<double> bayes_clf_new_data = {2.0, 3.0, 4.0};
47  std::string bayes_clf_prediction = bayes_clf.predict(bayes_clf_new_data);
48 
49  std::cout << "Predicted Class (BayesClf): " << bayes_clf_prediction << "\n";
50 
51  // print learned probabilities for BayesClf
52  bayes_clf.display();
53 
54  // example for BayesGauss class
55  std::vector<std::vector<double>> gauss_train_data = {
56  {1.2, 2.3, 3.5},
57  {2.5, 3.8, 4.0},
58  {3.0, 4.2, 5.2},
59  {4.1, 5.3, 6.7},
60  };
61 
62  std::vector<std::string> gauss_labels = {"classX",
63  "classY",
64  "classX",
65  "classY"};
66 
67  gpmp::ml::BayesGauss gauss_clf;
68  gauss_clf.train(gauss_train_data, gauss_labels);
69 
70  std::vector<double> gauss_new_data = {2.3, 3.7, 4.5};
71  std::string gauss_prediction = gauss_clf.predict(gauss_new_data);
72 
73  std::cout << "Predicted Class (BayesGauss): " << gauss_prediction << "\n";
74 
75  // print learned probabilities for BayesGauss
76  gauss_clf.display();
77 
78  // example for BayesMultiNom class
79  std::vector<std::vector<size_t>> multinom_train_data = {
80  {1, 2, 3},
81  {2, 3, 4},
82  {3, 4, 5},
83  {4, 5, 6},
84  };
85 
86  std::vector<std::string> multinom_labels = {"classA",
87  "classB",
88  "classA",
89  "classB"};
90 
91  gpmp::ml::BayesMultiNom multinom_clf(1.0, true, {0.4, 0.6});
92  multinom_clf.train(multinom_train_data, multinom_labels);
93 
94  std::vector<size_t> multinom_new_data = {2, 3, 4};
95  std::string multinom_prediction = multinom_clf.predict(multinom_new_data);
96 
97  std::cout << "Predicted Class (BayesMultiNom): " << multinom_prediction
98  << "\n";
99 
100  // print learned probabilities for BayesMultiNom
101  multinom_clf.display();
102 
103  return 0;
104 }
Bayes Classifier Class based on assumptions of independence.
Definition: bayes_clf.hpp:53
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
void display() const
Display the learned probabilities.
Definition: bayes_clf.cpp:323
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
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

References gpmp::ml::BayesBernoulli::display(), gpmp::ml::BayesGauss::display(), gpmp::ml::BayesGauss::predict(), gpmp::ml::BayesBernoulli::predict(), gpmp::ml::BayesClf::train(), gpmp::ml::BayesGauss::train(), gpmp::ml::BayesBernoulli::train(), and gpmp::ml::BayesMultiNom::train().