pmlpp/mlpp/multinomial_nb/multinomial_nb.cpp

119 lines
2.8 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-24 19:00:54 +01:00
MultinomialNB::MultinomialNB(std::vector<std::vector<double>> inputSet, std::vector<double> outputSet, int class_num) :
inputSet(inputSet), outputSet(outputSet), class_num(class_num) {
y_hat.resize(outputSet.size());
Evaluate();
}
std::vector<double> MultinomialNB::modelSetTest(std::vector<std::vector<double>> X) {
std::vector<double> y_hat;
for (int i = 0; i < X.size(); i++) {
y_hat.push_back(modelTest(X[i]));
}
return y_hat;
}
double MultinomialNB::modelTest(std::vector<double> x) {
double score[class_num];
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]);
}
return std::distance(score, std::max_element(score, score + sizeof(score) / sizeof(double)));
}
double MultinomialNB::score() {
Utilities util;
return util.performance(y_hat, outputSet);
}
void MultinomialNB::computeTheta() {
// 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();
}
}
}
void MultinomialNB::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)
double score[class_num];
// 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])]++;
}
priors = alg.scalarMultiply(double(1) / double(outputSet.size()), priors);
// 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
y_hat[i] = std::distance(score, std::max_element(score, score + sizeof(score) / sizeof(double)));
}
}