2023-12-30 00:41:59 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* bernoulli_nb.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* PMLPP Machine Learning Library */
|
|
|
|
/* https://github.com/Relintai/pmlpp */
|
|
|
|
/*************************************************************************/
|
2023-12-30 00:43:39 +01:00
|
|
|
/* Copyright (c) 2023-present Péter Magyar. */
|
2023-12-30 00:41:59 +01:00
|
|
|
/* Copyright (c) 2022-2023 Marc Melikyan */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2023-01-23 21:13:26 +01:00
|
|
|
|
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 "../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++) {
|
2023-04-29 15:07:30 +02:00
|
|
|
X->row_get_into_mlpp_vector(i, x_row_tmp);
|
2023-02-16 21:07:31 +01:00
|
|
|
|
2023-04-29 13:44:18 +02:00
|
|
|
y_hat->element_set(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++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
if (x->element_get(j) == _vocab->element_get(k)) {
|
|
|
|
score_0 *= _theta[0][_vocab->element_get(k)];
|
|
|
|
score_1 *= _theta[1][_vocab->element_get(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++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
if (_vocab->element_get(i) == _vocab->element_get(found_indices[j])) {
|
2023-01-24 19:00:54 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2023-04-29 13:44:18 +02:00
|
|
|
score_0 *= 1 - _theta[0][_vocab->element_get(i)];
|
|
|
|
score_1 *= 1 - _theta[1][_vocab->element_get(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-12-28 12:03:17 +01:00
|
|
|
_vocab.instance();
|
2023-02-16 21:07:31 +01:00
|
|
|
_y_hat.instance();
|
2023-12-28 12:03:17 +01:00
|
|
|
|
2023-02-16 21:07:31 +01:00
|
|
|
_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:21:31 +01:00
|
|
|
MLPPData data;
|
2023-02-16 21:07:31 +01:00
|
|
|
|
2023-04-30 18:46:53 +02:00
|
|
|
_vocab = data.vec_to_setnv(_input_set->flatten());
|
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++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
_theta.write[i][_vocab->element_get(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++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
_theta.write[_output_set->element_get(i)][_input_set->element_get(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++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
if (_output_set->element_get(ii) == 1) {
|
2023-02-16 21:07:31 +01:00
|
|
|
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-12-28 12:03:17 +01:00
|
|
|
for (int j = 0; j < _input_set->size().x; j++) {
|
2023-02-16 21:07:31 +01:00
|
|
|
for (int k = 0; k < _vocab->size(); k++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
if (_input_set->element_get(i, j) == _vocab->element_get(k)) {
|
|
|
|
score_0 += Math::log(static_cast<real_t>(_theta[0][_vocab->element_get(k)]));
|
|
|
|
score_1 += Math::log(static_cast<real_t>(_theta[1][_vocab->element_get(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++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
if (_vocab->element_get(ii) == _vocab->element_get(found_indices[j])) {
|
2023-01-24 19:00:54 +01:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2023-04-29 13:44:18 +02:00
|
|
|
score_0 += Math::log(1.0 - _theta[0][_vocab->element_get(ii)]);
|
|
|
|
score_1 += Math::log(1.0 - _theta[1][_vocab->element_get(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-04-29 13:44:18 +02:00
|
|
|
_y_hat->element_set(i, 0);
|
2023-01-24 19:00:54 +01:00
|
|
|
} else {
|
2023-04-29 13:44:18 +02:00
|
|
|
_y_hat->element_set(i, 1);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 18:26:19 +01:00
|
|
|
|
|
|
|
void MLPPBernoulliNB::_bind_methods() {
|
|
|
|
}
|