2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// GaussianNB.cpp
|
|
|
|
//
|
|
|
|
// Created by Marc Melikyan on 1/17/21.
|
|
|
|
//
|
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "gaussian_nb.h"
|
|
|
|
#include "../lin_alg/lin_alg.h"
|
2023-01-24 19:00:54 +01:00
|
|
|
#include "../stat/stat.h"
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "../utilities/utilities.h"
|
2023-01-23 21:13:26 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
2023-01-24 19:00:54 +01:00
|
|
|
#include <iostream>
|
2023-01-23 21:13:26 +01:00
|
|
|
#include <random>
|
|
|
|
|
2023-02-12 02:52:05 +01:00
|
|
|
/*
|
|
|
|
Ref<MLPPMatrix> MLPPGaussianNB::get_input_set() {
|
|
|
|
return _input_set;
|
|
|
|
}
|
|
|
|
void MLPPGaussianNB::set_input_set(const Ref<MLPPMatrix> &val) {
|
|
|
|
_input_set = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<MLPPVector> MLPPGaussianNB::get_output_set() {
|
|
|
|
return _output_set;
|
|
|
|
}
|
|
|
|
void MLPPGaussianNB::set_output_set(const Ref<MLPPVector> &val) {
|
|
|
|
_output_set = val;
|
|
|
|
}
|
2023-01-24 19:20:18 +01:00
|
|
|
|
2023-02-12 02:52:05 +01:00
|
|
|
int MLPPGaussianNB::get_class_num() {
|
|
|
|
return _class_num;
|
|
|
|
}
|
|
|
|
void MLPPGaussianNB::set_class_num(const int val) {
|
|
|
|
_class_num = val;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-02-12 02:52:05 +01:00
|
|
|
*/
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 02:52:05 +01:00
|
|
|
std::vector<real_t> MLPPGaussianNB::model_set_test(std::vector<std::vector<real_t>> X) {
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> y_hat;
|
2023-02-10 22:02:57 +01:00
|
|
|
for (uint32_t i = 0; i < X.size(); i++) {
|
2023-02-12 02:52:05 +01:00
|
|
|
y_hat.push_back(model_test(X[i]));
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
return y_hat;
|
|
|
|
}
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-02-12 02:52:05 +01:00
|
|
|
real_t MLPPGaussianNB::model_test(std::vector<real_t> x) {
|
|
|
|
real_t score[_class_num];
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t y_hat_i = 1;
|
2023-02-12 02:52:05 +01:00
|
|
|
|
|
|
|
for (int i = _class_num - 1; i >= 0; i--) {
|
|
|
|
y_hat_i += std::log(_priors[i] * (1 / sqrt(2 * M_PI * _sigma[i] * _sigma[i])) * exp(-(x[i] * _mu[i]) * (x[i] * _mu[i]) / (2 * _sigma[i] * _sigma[i])));
|
2023-01-24 19:00:54 +01:00
|
|
|
score[i] = exp(y_hat_i);
|
|
|
|
}
|
2023-02-12 02:52:05 +01:00
|
|
|
|
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-23 21:13:26 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPGaussianNB::score() {
|
2023-02-10 22:02:57 +01:00
|
|
|
MLPPUtilities util;
|
2023-02-12 02:52:05 +01:00
|
|
|
return util.performance(_y_hat, _output_set);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-02-12 02:52:05 +01:00
|
|
|
bool MLPPGaussianNB::is_initialized() {
|
|
|
|
return _initialized;
|
|
|
|
}
|
|
|
|
void MLPPGaussianNB::initialize() {
|
|
|
|
if (_initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
|
|
|
|
|
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MLPPGaussianNB::MLPPGaussianNB(std::vector<std::vector<real_t>> p_input_set, std::vector<real_t> p_output_set, int p_class_num) {
|
|
|
|
_input_set = p_input_set;
|
|
|
|
_output_set = p_output_set;
|
|
|
|
_class_num = p_class_num;
|
|
|
|
|
|
|
|
_y_hat.resize(_output_set.size());
|
|
|
|
|
|
|
|
evaluate();
|
|
|
|
|
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MLPPGaussianNB::MLPPGaussianNB() {
|
|
|
|
_initialized = false;
|
|
|
|
}
|
|
|
|
MLPPGaussianNB::~MLPPGaussianNB() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void MLPPGaussianNB::evaluate() {
|
2023-02-10 22:02:57 +01:00
|
|
|
MLPPStat stat;
|
2023-01-25 00:29:02 +01:00
|
|
|
MLPPLinAlg alg;
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
// Computing mu_k_y and sigma_k_y
|
2023-02-12 02:52:05 +01:00
|
|
|
_mu.resize(_class_num);
|
|
|
|
_sigma.resize(_class_num);
|
|
|
|
|
|
|
|
for (int i = _class_num - 1; i >= 0; i--) {
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> set;
|
2023-02-12 02:52:05 +01:00
|
|
|
for (uint32_t j = 0; j < _input_set.size(); j++) {
|
|
|
|
for (uint32_t k = 0; k < _input_set[j].size(); k++) {
|
|
|
|
if (_output_set[j] == i) {
|
|
|
|
set.push_back(_input_set[j][k]);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 02:52:05 +01:00
|
|
|
|
|
|
|
_mu[i] = stat.mean(set);
|
|
|
|
_sigma[i] = stat.standardDeviation(set);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
// Priors
|
2023-02-12 02:52:05 +01:00
|
|
|
_priors.resize(_class_num);
|
|
|
|
for (uint32_t i = 0; i < _output_set.size(); i++) {
|
|
|
|
_priors[int(_output_set[i])]++;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-02-12 02:52:05 +01:00
|
|
|
_priors = alg.scalarMultiply(real_t(1) / real_t(_output_set.size()), _priors);
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-02-12 02:52:05 +01:00
|
|
|
for (uint32_t i = 0; i < _output_set.size(); i++) {
|
|
|
|
real_t score[_class_num];
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t y_hat_i = 1;
|
2023-02-12 02:52:05 +01:00
|
|
|
|
|
|
|
for (int j = _class_num - 1; j >= 0; j--) {
|
|
|
|
for (uint32_t k = 0; k < _input_set[i].size(); k++) {
|
|
|
|
y_hat_i += std::log(_priors[j] * (1 / sqrt(2 * M_PI * _sigma[j] * _sigma[j])) * exp(-(_input_set[i][k] * _mu[j]) * (_input_set[i][k] * _mu[j]) / (2 * _sigma[j] * _sigma[j])));
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
score[j] = exp(y_hat_i);
|
|
|
|
std::cout << score[j] << std::endl;
|
|
|
|
}
|
2023-02-12 02:52:05 +01:00
|
|
|
|
|
|
|
_y_hat[i] = std::distance(score, std::max_element(score, score + sizeof(score) / sizeof(real_t)));
|
2023-01-27 13:01:16 +01:00
|
|
|
std::cout << std::distance(score, std::max_element(score, score + sizeof(score) / sizeof(real_t))) << std::endl;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-12 02:52:05 +01:00
|
|
|
|
|
|
|
void MLPPGaussianNB::_bind_methods() {
|
|
|
|
/*
|
|
|
|
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPGaussianNB::get_input_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_input_set", "value"), &MLPPGaussianNB::set_input_set);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_input_set", "get_input_set");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_output_set"), &MLPPGaussianNB::get_output_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_output_set", "value"), &MLPPGaussianNB::set_output_set);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_output_set", "get_output_set");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_k"), &MLPPGaussianNB::get_k);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_k", "value"), &MLPPGaussianNB::set_k);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "k"), "set_k", "get_k");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("model_set_test", "X"), &MLPPGaussianNB::model_set_test);
|
|
|
|
ClassDB::bind_method(D_METHOD("model_test", "x"), &MLPPGaussianNB::model_test);
|
|
|
|
ClassDB::bind_method(D_METHOD("score"), &MLPPGaussianNB::score);
|
|
|
|
*/
|
|
|
|
}
|