Fixed warnings in MLPPGaussianNB.

This commit is contained in:
Relintai 2023-02-10 22:02:57 +01:00
parent 97ac09d0d9
commit e22f26d074
2 changed files with 12 additions and 15 deletions

View File

@ -13,26 +13,24 @@
#include <iostream> #include <iostream>
#include <random> #include <random>
MLPPGaussianNB::MLPPGaussianNB(std::vector<std::vector<real_t>> p_inputSet, std::vector<real_t> p_outputSet, int p_class_num) {
inputSet = p_inputSet;
outputSet = p_outputSet;
class_num = p_class_num;
MLPPGaussianNB::MLPPGaussianNB(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, int class_num) :
inputSet(inputSet), outputSet(outputSet), class_num(class_num) {
y_hat.resize(outputSet.size()); y_hat.resize(outputSet.size());
Evaluate(); Evaluate();
MLPPLinAlg alg;
} }
std::vector<real_t> MLPPGaussianNB::modelSetTest(std::vector<std::vector<real_t>> X) { std::vector<real_t> MLPPGaussianNB::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;
} }
real_t MLPPGaussianNB::modelTest(std::vector<real_t> x) { real_t MLPPGaussianNB::modelTest(std::vector<real_t> x) {
MLPPStat stat;
MLPPLinAlg alg;
real_t score[class_num]; real_t score[class_num];
real_t y_hat_i = 1; real_t y_hat_i = 1;
for (int i = class_num - 1; i >= 0; i--) { for (int i = class_num - 1; i >= 0; i--) {
@ -43,12 +41,12 @@ real_t MLPPGaussianNB::modelTest(std::vector<real_t> x) {
} }
real_t MLPPGaussianNB::score() { real_t MLPPGaussianNB::score() {
MLPPUtilities util; MLPPUtilities util;
return util.performance(y_hat, outputSet); return util.performance(y_hat, outputSet);
} }
void MLPPGaussianNB::Evaluate() { void MLPPGaussianNB::Evaluate() {
MLPPStat stat; MLPPStat stat;
MLPPLinAlg alg; MLPPLinAlg alg;
// Computing mu_k_y and sigma_k_y // Computing mu_k_y and sigma_k_y
@ -56,8 +54,8 @@ void MLPPGaussianNB::Evaluate() {
sigma.resize(class_num); sigma.resize(class_num);
for (int i = class_num - 1; i >= 0; i--) { for (int i = class_num - 1; i >= 0; i--) {
std::vector<real_t> set; std::vector<real_t> set;
for (int j = 0; j < inputSet.size(); j++) { for (uint32_t j = 0; j < inputSet.size(); j++) {
for (int k = 0; k < inputSet[j].size(); k++) { for (uint32_t k = 0; k < inputSet[j].size(); k++) {
if (outputSet[j] == i) { if (outputSet[j] == i) {
set.push_back(inputSet[j][k]); set.push_back(inputSet[j][k]);
} }
@ -69,16 +67,16 @@ void MLPPGaussianNB::Evaluate() {
// Priors // Priors
priors.resize(class_num); priors.resize(class_num);
for (int i = 0; i < outputSet.size(); i++) { for (uint32_t i = 0; i < outputSet.size(); i++) {
priors[int(outputSet[i])]++; priors[int(outputSet[i])]++;
} }
priors = alg.scalarMultiply(real_t(1) / real_t(outputSet.size()), priors); priors = alg.scalarMultiply(real_t(1) / real_t(outputSet.size()), priors);
for (int i = 0; i < outputSet.size(); i++) { for (uint32_t i = 0; i < outputSet.size(); i++) {
real_t score[class_num]; real_t score[class_num];
real_t y_hat_i = 1; real_t y_hat_i = 1;
for (int j = class_num - 1; j >= 0; j--) { for (int j = class_num - 1; j >= 0; j--) {
for (int k = 0; k < inputSet[i].size(); k++) { for (uint32_t k = 0; k < inputSet[i].size(); k++) {
y_hat_i += std::log(priors[j] * (1 / sqrt(2 * M_PI * sigma[j] * sigma[j])) * exp(-(inputSet[i][k] * mu[j]) * (inputSet[i][k] * mu[j]) / (2 * sigma[j] * sigma[j]))); y_hat_i += std::log(priors[j] * (1 / sqrt(2 * M_PI * sigma[j] * sigma[j])) * exp(-(inputSet[i][k] * mu[j]) * (inputSet[i][k] * mu[j]) / (2 * sigma[j] * sigma[j])));
} }
score[j] = exp(y_hat_i); score[j] = exp(y_hat_i);

View File

@ -12,7 +12,6 @@
#include <vector> #include <vector>
class MLPPGaussianNB { class MLPPGaussianNB {
public: public:
MLPPGaussianNB(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, int class_num); MLPPGaussianNB(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, int class_num);