pmlpp/mlpp/multinomial_nb/multinomial_nb.cpp

119 lines
2.9 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-01-24 19:20:18 +01:00
2023-01-27 13:01:16 +01:00
MLPPMultinomialNB::MLPPMultinomialNB(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, int class_num) :
2023-01-24 19:00:54 +01:00
inputSet(inputSet), outputSet(outputSet), class_num(class_num) {
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-01-24 19:00:54 +01:00
for (int i = 0; i < X.size(); i++) {
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();
for (int j = 0; j < x.size(); j++) {
for (int k = 0; k < vocab.size(); k++) {
if (x[j] == vocab[k]) {
for (int p = class_num - 1; p >= 0; p--) {
score[p] += std::log(theta[p][vocab[k]]);
}
}
}
}
for (int i = 0; i < priors.size(); i++) {
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() {
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--) {
for (int 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++) {
theta[outputSet[i]][inputSet[i][j]]++;
}
}
for (int i = 0; i < theta.size(); i++) {
for (int j = 0; j < theta[i].size(); j++) {
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-01-24 19:00:54 +01:00
for (int i = 0; i < outputSet.size(); i++) {
// 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);
for (int i = 0; i < outputSet.size(); i++) {
priors[int(outputSet[i])]++;
}
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();
for (int j = 0; j < inputSet.size(); j++) {
for (int k = 0; k < vocab.size(); k++) {
if (inputSet[i][j] == vocab[k]) {
for (int p = class_num - 1; p >= 0; p--) {
score[p] += std::log(theta[i][vocab[k]]);
}
}
}
}
for (int i = 0; i < priors.size(); i++) {
score[i] += std::log(priors[i]);
score[i] = exp(score[i]);
}
for (int i = 0; i < 2; i++) {
std::cout << score[i] << std::endl;
}
// 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
}
}