pmlpp/mlpp/multinomial_nb/multinomial_nb.cpp

121 lines
3.0 KiB
C++
Raw Normal View History

//
// MultinomialNB.cpp
//
// Created by Marc Melikyan on 1/17/21.
//
2023-01-24 18:12:23 +01:00
#include "multinomial_nb.h"
#include "../lin_alg/lin_alg.h"
2023-01-24 19:00:54 +01:00
#include "../utilities/utilities.h"
#include <algorithm>
2023-01-24 19:00:54 +01:00
#include <iostream>
#include <random>
2023-02-10 21:35:43 +01:00
MLPPMultinomialNB::MLPPMultinomialNB(std::vector<std::vector<real_t>> pinputSet, std::vector<real_t> poutputSet, int pclass_num) {
inputSet = pinputSet;
outputSet = poutputSet;
class_num = pclass_num;
2023-01-24 19:20:18 +01:00
2023-01-24 19:00:54 +01:00
y_hat.resize(outputSet.size());
Evaluate();
}
2023-01-27 13:01:16 +01:00
std::vector<real_t> MLPPMultinomialNB::modelSetTest(std::vector<std::vector<real_t>> X) {
std::vector<real_t> y_hat;
2023-02-10 21:35:43 +01:00
for (uint32_t i = 0; i < X.size(); i++) {
2023-01-24 19:00:54 +01:00
y_hat.push_back(modelTest(X[i]));
}
return y_hat;
}
2023-01-27 13:01:16 +01:00
real_t MLPPMultinomialNB::modelTest(std::vector<real_t> x) {
real_t score[class_num];
2023-01-24 19:00:54 +01:00
computeTheta();
2023-02-10 21:35:43 +01:00
for (uint32_t j = 0; j < x.size(); j++) {
for (uint32_t k = 0; k < vocab.size(); k++) {
2023-01-24 19:00:54 +01:00
if (x[j] == vocab[k]) {
for (int p = class_num - 1; p >= 0; p--) {
score[p] += std::log(theta[p][vocab[k]]);
}
}
}
}
2023-02-10 21:35:43 +01:00
for (uint32_t i = 0; i < priors.size(); i++) {
2023-01-24 19:00:54 +01:00
score[i] += std::log(priors[i]);
}
2023-01-27 13:01:16 +01:00
return std::distance(score, std::max_element(score, score + sizeof(score) / sizeof(real_t)));
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
real_t MLPPMultinomialNB::score() {
2023-02-10 21:35:43 +01:00
MLPPUtilities util;
2023-01-24 19:00:54 +01:00
return util.performance(y_hat, outputSet);
}
2023-01-25 00:54:50 +01:00
void MLPPMultinomialNB::computeTheta() {
2023-01-24 19:00:54 +01:00
// Resizing theta for the sake of ease & proper access of the elements.
theta.resize(class_num);
// Setting all values in the hasmap by default to 0.
for (int i = class_num - 1; i >= 0; i--) {
2023-02-10 21:35:43 +01:00
for (uint32_t j = 0; j < vocab.size(); j++) {
2023-01-24 19:00:54 +01:00
theta[i][vocab[j]] = 0;
}
}
2023-02-10 21:35:43 +01:00
for (uint32_t i = 0; i < inputSet.size(); i++) {
for (uint32_t j = 0; j < inputSet[0].size(); j++) {
2023-01-24 19:00:54 +01:00
theta[outputSet[i]][inputSet[i][j]]++;
}
}
2023-02-10 21:35:43 +01:00
for (uint32_t i = 0; i < theta.size(); i++) {
for (uint32_t j = 0; j < theta[i].size(); j++) {
2023-01-24 19:00:54 +01:00
theta[i][j] /= priors[i] * y_hat.size();
}
}
}
2023-01-25 00:54:50 +01:00
void MLPPMultinomialNB::Evaluate() {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-02-10 21:35:43 +01:00
for (uint32_t i = 0; i < outputSet.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[class_num];
2023-01-24 19:00:54 +01:00
// Easy computation of priors, i.e. Pr(C_k)
priors.resize(class_num);
2023-02-10 21:35:43 +01:00
for (uint32_t ii = 0; ii < outputSet.size(); ii++) {
priors[int(outputSet[ii])]++;
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
priors = alg.scalarMultiply(real_t(1) / real_t(outputSet.size()), priors);
2023-01-24 19:00:54 +01:00
// Evaluating Theta...
computeTheta();
2023-02-10 21:35:43 +01:00
for (uint32_t j = 0; j < inputSet.size(); j++) {
for (uint32_t k = 0; k < vocab.size(); k++) {
2023-01-24 19:00:54 +01:00
if (inputSet[i][j] == vocab[k]) {
for (int p = class_num - 1; p >= 0; p--) {
score[p] += std::log(theta[i][vocab[k]]);
}
}
}
}
2023-02-10 21:35:43 +01:00
for (uint32_t ii = 0; ii < priors.size(); ii++) {
score[ii] += std::log(priors[ii]);
score[ii] = exp(score[ii]);
2023-01-24 19:00:54 +01:00
}
2023-02-10 21:35:43 +01:00
for (int ii = 0; ii < 2; ii++) {
std::cout << score[ii] << std::endl;
2023-01-24 19:00:54 +01:00
}
// Assigning the traning example's y_hat to a class
2023-01-27 13:01:16 +01:00
y_hat[i] = std::distance(score, std::max_element(score, score + sizeof(score) / sizeof(real_t)));
2023-01-24 19:00:54 +01:00
}
}