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 <random>
MLPPBernoulliNB::MLPPBernoulliNB(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet) :
inputSet(inputSet), outputSet(outputSet), class_num(2) {
MLPPBernoulliNB::MLPPBernoulliNB(std::vector<std::vector<real_t>> p_inputSet, std::vector<real_t> p_outputSet) {
inputSet = p_inputSet;
outputSet = p_outputSet;
class_num = 2;
y_hat.resize(outputSet.size());
Evaluate();
}
std::vector<real_t> MLPPBernoulliNB::modelSetTest(std::vector<std::vector<real_t>> X) {
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]));
}
return y_hat;
@ -32,8 +35,8 @@ real_t MLPPBernoulliNB::modelTest(std::vector<real_t> x) {
std::vector<int> foundIndices;
for (int j = 0; j < x.size(); j++) {
for (int k = 0; k < vocab.size(); k++) {
for (uint32_t j = 0; j < x.size(); j++) {
for (uint32_t k = 0; k < vocab.size(); k++) {
if (x[j] == vocab[k]) {
score_0 *= theta[0][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;
for (int j = 0; j < foundIndices.size(); j++) {
for (uint32_t j = 0; j < foundIndices.size(); j++) {
if (vocab[i] == vocab[foundIndices[j]]) {
found = true;
}
@ -69,7 +72,7 @@ real_t MLPPBernoulliNB::modelTest(std::vector<real_t> x) {
}
real_t MLPPBernoulliNB::score() {
MLPPUtilities util;
MLPPUtilities util;
return util.performance(y_hat, outputSet);
}
@ -85,19 +88,19 @@ void MLPPBernoulliNB::computeTheta() {
// Setting all values in the hasmap by default to 0.
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;
}
}
for (int i = 0; i < inputSet.size(); i++) {
for (int j = 0; j < inputSet[0].size(); j++) {
for (uint32_t i = 0; i < inputSet.size(); i++) {
for (uint32_t j = 0; j < inputSet[0].size(); j++) {
theta[outputSet[i]][inputSet[i][j]]++;
}
}
for (int i = 0; i < theta.size(); i++) {
for (int j = 0; j < theta[i].size(); j++) {
for (uint32_t i = 0; i < theta.size(); i++) {
for (uint32_t j = 0; j < theta[i].size(); j++) {
if (i == 0) {
theta[i][j] /= prior_0 * y_hat.size();
} else {
@ -108,15 +111,15 @@ void MLPPBernoulliNB::computeTheta() {
}
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)
real_t score_0 = 1;
real_t score_1 = 1;
real_t sum = 0;
for (int i = 0; i < outputSet.size(); i++) {
if (outputSet[i] == 1) {
sum += outputSet[i];
for (uint32_t ii = 0; ii < outputSet.size(); ii++) {
if (outputSet[ii] == 1) {
sum += outputSet[ii];
}
}
@ -132,8 +135,8 @@ void MLPPBernoulliNB::Evaluate() {
std::vector<int> foundIndices;
for (int j = 0; j < inputSet.size(); j++) {
for (int k = 0; k < vocab.size(); k++) {
for (uint32_t j = 0; j < inputSet.size(); j++) {
for (uint32_t k = 0; k < vocab.size(); k++) {
if (inputSet[i][j] == vocab[k]) {
score_0 += std::log(theta[0][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;
for (int j = 0; j < foundIndices.size(); j++) {
if (vocab[i] == vocab[foundIndices[j]]) {
for (uint32_t j = 0; j < foundIndices.size(); j++) {
if (vocab[ii] == vocab[foundIndices[j]]) {
found = true;
}
}
if (!found) {
score_0 += std::log(1 - theta[0][vocab[i]]);
score_1 += std::log(1 - theta[1][vocab[i]]);
score_0 += std::log(1 - theta[0][vocab[ii]]);
score_1 += std::log(1 - theta[1][vocab[ii]]);
}
}