2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// BernoulliNB.cpp
|
|
|
|
//
|
|
|
|
// Created by Marc Melikyan on 1/17/21.
|
|
|
|
//
|
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "bernoulli_nb.h"
|
|
|
|
#include "../data/data.h"
|
2023-01-24 19:00:54 +01:00
|
|
|
#include "../lin_alg/lin_alg.h"
|
|
|
|
#include "../utilities/utilities.h"
|
2023-01-23 21:13:26 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <random>
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
Ref<MLPPVector> MLPPBernoulliNB::model_set_test(const Ref<MLPPMatrix> &X) {
|
|
|
|
Ref<MLPPVector> y_hat;
|
|
|
|
y_hat.instance();
|
|
|
|
y_hat->resize(X->size().y);
|
|
|
|
|
|
|
|
Ref<MLPPVector> x_row_tmp;
|
|
|
|
x_row_tmp.instance();
|
|
|
|
x_row_tmp->resize(X->size().x);
|
|
|
|
|
|
|
|
for (int i = 0; i < X->size().y; i++) {
|
|
|
|
X->get_row_into_mlpp_vector(i, x_row_tmp);
|
|
|
|
|
|
|
|
y_hat->set_element(i, model_test(x_row_tmp));
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-02-16 21:07:31 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
return y_hat;
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
real_t MLPPBernoulliNB::model_test(const Ref<MLPPVector> &x) {
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t score_0 = 1;
|
|
|
|
real_t score_1 = 1;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
Vector<int> found_indices;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int j = 0; j < x->size(); j++) {
|
|
|
|
for (int k = 0; k < _vocab->size(); k++) {
|
|
|
|
if (x->get_element(j) == _vocab->get_element(k)) {
|
|
|
|
score_0 *= _theta[0][_vocab->get_element(k)];
|
|
|
|
score_1 *= _theta[1][_vocab->get_element(k)];
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
found_indices.push_back(k);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int i = 0; i < _vocab->size(); i++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
bool found = false;
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int j = 0; j < found_indices.size(); j++) {
|
|
|
|
if (_vocab->get_element(i) == _vocab->get_element(found_indices[j])) {
|
2023-01-24 19:00:54 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2023-02-16 21:07:31 +01:00
|
|
|
score_0 *= 1 - _theta[0][_vocab->get_element(i)];
|
|
|
|
score_1 *= 1 - _theta[1][_vocab->get_element(i)];
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 14:12:02 +01:00
|
|
|
score_0 *= _prior_0;
|
|
|
|
score_1 *= _prior_1;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Assigning the traning example to a class
|
|
|
|
|
|
|
|
if (score_0 > score_1) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPBernoulliNB::score() {
|
2023-02-10 22:46:16 +01:00
|
|
|
MLPPUtilities util;
|
2023-02-16 21:07:31 +01:00
|
|
|
|
|
|
|
return util.performance_vec(_y_hat, _output_set);
|
2023-02-12 14:12:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
MLPPBernoulliNB::MLPPBernoulliNB(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPVector> &p_output_set) {
|
2023-02-12 14:12:02 +01:00
|
|
|
_input_set = p_input_set;
|
|
|
|
_output_set = p_output_set;
|
|
|
|
_class_num = 2;
|
|
|
|
|
|
|
|
_prior_1 = 0;
|
|
|
|
_prior_0 = 0;
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
_y_hat.instance();
|
|
|
|
_y_hat->resize(_output_set->size());
|
|
|
|
|
2023-02-12 14:12:02 +01:00
|
|
|
evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
MLPPBernoulliNB::MLPPBernoulliNB() {
|
|
|
|
_prior_1 = 0;
|
|
|
|
_prior_0 = 0;
|
|
|
|
}
|
|
|
|
MLPPBernoulliNB::~MLPPBernoulliNB() {
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 14:12:02 +01:00
|
|
|
void MLPPBernoulliNB::compute_vocab() {
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-25 00:21:31 +01:00
|
|
|
MLPPData data;
|
2023-02-16 21:07:31 +01:00
|
|
|
|
2023-04-22 14:39:13 +02:00
|
|
|
_vocab = data.vec_to_setnv(alg.flattenvvnv(_input_set));
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 14:12:02 +01:00
|
|
|
void MLPPBernoulliNB::compute_theta() {
|
2023-01-24 19:00:54 +01:00
|
|
|
// Resizing theta for the sake of ease & proper access of the elements.
|
2023-02-12 14:12:02 +01:00
|
|
|
_theta.resize(_class_num);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Setting all values in the hasmap by default to 0.
|
2023-02-12 14:12:02 +01:00
|
|
|
for (int i = _class_num - 1; i >= 0; i--) {
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int j = 0; j < _vocab->size(); j++) {
|
|
|
|
_theta.write[i][_vocab->get_element(j)] = 0;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int i = 0; i < _input_set->size().y; i++) {
|
|
|
|
for (int j = 0; j < _input_set->size().x; j++) {
|
|
|
|
_theta.write[_output_set->get_element(i)][_input_set->get_element(i, j)]++;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int i = 0; i < _theta.size(); i++) {
|
2023-02-12 14:12:02 +01:00
|
|
|
for (uint32_t j = 0; j < _theta[i].size(); j++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
if (i == 0) {
|
2023-02-16 21:07:31 +01:00
|
|
|
_theta.write[i][j] /= _prior_0 * _y_hat->size();
|
2023-01-24 19:00:54 +01:00
|
|
|
} else {
|
2023-02-16 21:07:31 +01:00
|
|
|
_theta.write[i][j] /= _prior_1 * _y_hat->size();
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 14:12:02 +01:00
|
|
|
void MLPPBernoulliNB::evaluate() {
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int i = 0; i < _output_set->size(); i++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
// Pr(B | A) * Pr(A)
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t score_0 = 1;
|
|
|
|
real_t score_1 = 1;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t sum = 0;
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int ii = 0; ii < _output_set->size(); ii++) {
|
|
|
|
if (_output_set->get_element(ii) == 1) {
|
|
|
|
sum += 1;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Easy computation of priors, i.e. Pr(C_k)
|
2023-02-16 21:07:31 +01:00
|
|
|
_prior_1 = sum / _y_hat->size();
|
2023-02-12 14:12:02 +01:00
|
|
|
_prior_0 = 1 - _prior_1;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Evaluating Theta...
|
2023-02-12 14:12:02 +01:00
|
|
|
compute_theta();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Evaluating the vocab set...
|
2023-02-12 14:12:02 +01:00
|
|
|
compute_vocab();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
Vector<int> found_indices;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int j = 0; j < _input_set->size().y; j++) {
|
|
|
|
for (int k = 0; k < _vocab->size(); k++) {
|
|
|
|
if (_input_set->get_element(i, j) == _vocab->get_element(k)) {
|
|
|
|
score_0 += Math::log(static_cast<real_t>(_theta[0][_vocab->get_element(k)]));
|
|
|
|
score_1 += Math::log(static_cast<real_t>(_theta[1][_vocab->get_element(k)]));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
found_indices.push_back(k);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int ii = 0; ii < _vocab->size(); ii++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
bool found = false;
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int j = 0; j < found_indices.size(); j++) {
|
|
|
|
if (_vocab->get_element(ii) == _vocab->get_element(found_indices[j])) {
|
2023-01-24 19:00:54 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2023-02-16 21:07:31 +01:00
|
|
|
score_0 += Math::log(1.0 - _theta[0][_vocab->get_element(ii)]);
|
|
|
|
score_1 += Math::log(1.0 - _theta[1][_vocab->get_element(ii)]);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
score_0 += Math::log(_prior_0);
|
|
|
|
score_1 += Math::log(_prior_1);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
score_0 = Math::exp(score_0);
|
|
|
|
score_1 = Math::exp(score_1);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Assigning the traning example to a class
|
|
|
|
|
|
|
|
if (score_0 > score_1) {
|
2023-02-16 21:07:31 +01:00
|
|
|
_y_hat->set_element(i, 0);
|
2023-01-24 19:00:54 +01:00
|
|
|
} else {
|
2023-02-16 21:07:31 +01:00
|
|
|
_y_hat->set_element(i, 1);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 18:26:19 +01:00
|
|
|
|
|
|
|
void MLPPBernoulliNB::_bind_methods() {
|
|
|
|
}
|