Fixed warnings in MLPPBernoulliNB.

This commit is contained in:
Relintai 2023-02-10 22:46:16 +01:00
parent c37237aef8
commit 7e738f79ee

View File

@ -12,15 +12,18 @@
#include <iostream> #include <iostream>
#include <random> #include <random>
MLPPBernoulliNB::MLPPBernoulliNB(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet) : MLPPBernoulliNB::MLPPBernoulliNB(std::vector<std::vector<real_t>> p_inputSet, std::vector<real_t> p_outputSet) {
inputSet(inputSet), outputSet(outputSet), class_num(2) { inputSet = p_inputSet;
outputSet = p_outputSet;
class_num = 2;
y_hat.resize(outputSet.size()); y_hat.resize(outputSet.size());
Evaluate(); Evaluate();
} }
std::vector<real_t> MLPPBernoulliNB::modelSetTest(std::vector<std::vector<real_t>> X) { std::vector<real_t> MLPPBernoulliNB::modelSetTest(std::vector<std::vector<real_t>> X) {
std::vector<real_t> y_hat; std::vector<real_t> y_hat;
for (int i = 0; i < X.size(); i++) { for (uint32_t i = 0; i < X.size(); i++) {
y_hat.push_back(modelTest(X[i])); y_hat.push_back(modelTest(X[i]));
} }
return y_hat; return y_hat;
@ -32,8 +35,8 @@ real_t MLPPBernoulliNB::modelTest(std::vector<real_t> x) {
std::vector<int> foundIndices; std::vector<int> foundIndices;
for (int j = 0; j < x.size(); j++) { for (uint32_t j = 0; j < x.size(); j++) {
for (int k = 0; k < vocab.size(); k++) { for (uint32_t k = 0; k < vocab.size(); k++) {
if (x[j] == vocab[k]) { if (x[j] == vocab[k]) {
score_0 *= theta[0][vocab[k]]; score_0 *= theta[0][vocab[k]];
score_1 *= theta[1][vocab[k]]; score_1 *= theta[1][vocab[k]];
@ -43,9 +46,9 @@ real_t MLPPBernoulliNB::modelTest(std::vector<real_t> x) {
} }
} }
for (int i = 0; i < vocab.size(); i++) { for (uint32_t i = 0; i < vocab.size(); i++) {
bool found = false; bool found = false;
for (int j = 0; j < foundIndices.size(); j++) { for (uint32_t j = 0; j < foundIndices.size(); j++) {
if (vocab[i] == vocab[foundIndices[j]]) { if (vocab[i] == vocab[foundIndices[j]]) {
found = true; found = true;
} }
@ -85,19 +88,19 @@ void MLPPBernoulliNB::computeTheta() {
// Setting all values in the hasmap by default to 0. // Setting all values in the hasmap by default to 0.
for (int i = class_num - 1; i >= 0; i--) { for (int i = class_num - 1; i >= 0; i--) {
for (int j = 0; j < vocab.size(); j++) { for (uint32_t j = 0; j < vocab.size(); j++) {
theta[i][vocab[j]] = 0; theta[i][vocab[j]] = 0;
} }
} }
for (int i = 0; i < inputSet.size(); i++) { for (uint32_t i = 0; i < inputSet.size(); i++) {
for (int j = 0; j < inputSet[0].size(); j++) { for (uint32_t j = 0; j < inputSet[0].size(); j++) {
theta[outputSet[i]][inputSet[i][j]]++; theta[outputSet[i]][inputSet[i][j]]++;
} }
} }
for (int i = 0; i < theta.size(); i++) { for (uint32_t i = 0; i < theta.size(); i++) {
for (int j = 0; j < theta[i].size(); j++) { for (uint32_t j = 0; j < theta[i].size(); j++) {
if (i == 0) { if (i == 0) {
theta[i][j] /= prior_0 * y_hat.size(); theta[i][j] /= prior_0 * y_hat.size();
} else { } else {
@ -108,15 +111,15 @@ void MLPPBernoulliNB::computeTheta() {
} }
void MLPPBernoulliNB::Evaluate() { void MLPPBernoulliNB::Evaluate() {
for (int i = 0; i < outputSet.size(); i++) { for (uint32_t i = 0; i < outputSet.size(); i++) {
// Pr(B | A) * Pr(A) // Pr(B | A) * Pr(A)
real_t score_0 = 1; real_t score_0 = 1;
real_t score_1 = 1; real_t score_1 = 1;
real_t sum = 0; real_t sum = 0;
for (int i = 0; i < outputSet.size(); i++) { for (uint32_t ii = 0; ii < outputSet.size(); ii++) {
if (outputSet[i] == 1) { if (outputSet[ii] == 1) {
sum += outputSet[i]; sum += outputSet[ii];
} }
} }
@ -132,8 +135,8 @@ void MLPPBernoulliNB::Evaluate() {
std::vector<int> foundIndices; std::vector<int> foundIndices;
for (int j = 0; j < inputSet.size(); j++) { for (uint32_t j = 0; j < inputSet.size(); j++) {
for (int k = 0; k < vocab.size(); k++) { for (uint32_t k = 0; k < vocab.size(); k++) {
if (inputSet[i][j] == vocab[k]) { if (inputSet[i][j] == vocab[k]) {
score_0 += std::log(theta[0][vocab[k]]); score_0 += std::log(theta[0][vocab[k]]);
score_1 += std::log(theta[1][vocab[k]]); score_1 += std::log(theta[1][vocab[k]]);
@ -143,16 +146,16 @@ void MLPPBernoulliNB::Evaluate() {
} }
} }
for (int i = 0; i < vocab.size(); i++) { for (uint32_t ii = 0; ii < vocab.size(); ii++) {
bool found = false; bool found = false;
for (int j = 0; j < foundIndices.size(); j++) { for (uint32_t j = 0; j < foundIndices.size(); j++) {
if (vocab[i] == vocab[foundIndices[j]]) { if (vocab[ii] == vocab[foundIndices[j]]) {
found = true; found = true;
} }
} }
if (!found) { if (!found) {
score_0 += std::log(1 - theta[0][vocab[i]]); score_0 += std::log(1 - theta[0][vocab[ii]]);
score_1 += std::log(1 - theta[1][vocab[i]]); score_1 += std::log(1 - theta[1][vocab[ii]]);
} }
} }