2023-10-04 21:19:23 +00:00
|
|
|
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <catch2/catch_approx.hpp>
|
|
|
|
#include <catch2/generators/catch_generators.hpp>
|
2024-01-07 18:58:22 +00:00
|
|
|
#include <vector>
|
2023-10-04 21:19:23 +00:00
|
|
|
#include <map>
|
2024-01-07 18:58:22 +00:00
|
|
|
#include <string>
|
2023-10-04 21:19:23 +00:00
|
|
|
#include "KDB.h"
|
|
|
|
#include "TAN.h"
|
|
|
|
#include "SPODE.h"
|
|
|
|
#include "AODE.h"
|
|
|
|
#include "BoostAODE.h"
|
|
|
|
#include "TANLd.h"
|
|
|
|
#include "KDBLd.h"
|
|
|
|
#include "SPODELd.h"
|
|
|
|
#include "AODELd.h"
|
|
|
|
#include "TestUtils.h"
|
|
|
|
|
2024-02-12 09:58:20 +00:00
|
|
|
TEST_CASE("Library check version", "[BayesNet]")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::KDB(2);
|
2024-02-20 09:11:22 +00:00
|
|
|
REQUIRE(clf.getVersion() == "1.0.2");
|
2024-02-12 09:58:20 +00:00
|
|
|
}
|
2023-10-04 21:19:23 +00:00
|
|
|
TEST_CASE("Test Bayesian Classifiers score", "[BayesNet]")
|
|
|
|
{
|
2023-11-08 17:45:35 +00:00
|
|
|
map <pair<std::string, std::string>, float> scores = {
|
2023-10-04 21:19:23 +00:00
|
|
|
// Diabetes
|
|
|
|
{{"diabetes", "AODE"}, 0.811198}, {{"diabetes", "KDB"}, 0.852865}, {{"diabetes", "SPODE"}, 0.802083}, {{"diabetes", "TAN"}, 0.821615},
|
2023-10-04 23:14:16 +00:00
|
|
|
{{"diabetes", "AODELd"}, 0.8138f}, {{"diabetes", "KDBLd"}, 0.80208f}, {{"diabetes", "SPODELd"}, 0.78646f}, {{"diabetes", "TANLd"}, 0.8099f}, {{"diabetes", "BoostAODE"}, 0.83984f},
|
2023-10-04 21:19:23 +00:00
|
|
|
// Ecoli
|
|
|
|
{{"ecoli", "AODE"}, 0.889881}, {{"ecoli", "KDB"}, 0.889881}, {{"ecoli", "SPODE"}, 0.880952}, {{"ecoli", "TAN"}, 0.892857},
|
2023-10-04 23:14:16 +00:00
|
|
|
{{"ecoli", "AODELd"}, 0.8869f}, {{"ecoli", "KDBLd"}, 0.875f}, {{"ecoli", "SPODELd"}, 0.84226f}, {{"ecoli", "TANLd"}, 0.86905f}, {{"ecoli", "BoostAODE"}, 0.89583f},
|
2023-10-04 21:19:23 +00:00
|
|
|
// Glass
|
|
|
|
{{"glass", "AODE"}, 0.78972}, {{"glass", "KDB"}, 0.827103}, {{"glass", "SPODE"}, 0.775701}, {{"glass", "TAN"}, 0.827103},
|
2023-10-04 23:14:16 +00:00
|
|
|
{{"glass", "AODELd"}, 0.79439f}, {{"glass", "KDBLd"}, 0.85047f}, {{"glass", "SPODELd"}, 0.79439f}, {{"glass", "TANLd"}, 0.86449f}, {{"glass", "BoostAODE"}, 0.84579f},
|
2023-10-04 21:19:23 +00:00
|
|
|
// Iris
|
|
|
|
{{"iris", "AODE"}, 0.973333}, {{"iris", "KDB"}, 0.973333}, {{"iris", "SPODE"}, 0.973333}, {{"iris", "TAN"}, 0.973333},
|
2023-10-04 23:14:16 +00:00
|
|
|
{{"iris", "AODELd"}, 0.973333}, {{"iris", "KDBLd"}, 0.973333}, {{"iris", "SPODELd"}, 0.96f}, {{"iris", "TANLd"}, 0.97333f}, {{"iris", "BoostAODE"}, 0.98f}
|
2023-10-04 21:19:23 +00:00
|
|
|
};
|
|
|
|
|
2023-11-08 17:45:35 +00:00
|
|
|
std::string file_name = GENERATE("glass", "iris", "ecoli", "diabetes");
|
2023-10-06 15:08:54 +00:00
|
|
|
auto raw = RawDatasets(file_name, false);
|
2023-10-04 21:19:23 +00:00
|
|
|
|
|
|
|
SECTION("Test TAN classifier (" + file_name + ")")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::TAN();
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
auto score = clf.score(raw.Xv, raw.yv);
|
2023-10-04 21:19:23 +00:00
|
|
|
//scores[{file_name, "TAN"}] = score;
|
2023-10-06 15:08:54 +00:00
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "TAN"}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
SECTION("Test TANLd classifier (" + file_name + ")")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::TANLd();
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
|
|
|
auto score = clf.score(raw.Xt, raw.yt);
|
2023-10-04 21:19:23 +00:00
|
|
|
//scores[{file_name, "TANLd"}] = score;
|
2023-10-06 15:08:54 +00:00
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "TANLd"}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
SECTION("Test KDB classifier (" + file_name + ")")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::KDB(2);
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
auto score = clf.score(raw.Xv, raw.yv);
|
2023-10-04 21:19:23 +00:00
|
|
|
//scores[{file_name, "KDB"}] = score;
|
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "KDB"
|
2023-10-06 15:08:54 +00:00
|
|
|
}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
SECTION("Test KDBLd classifier (" + file_name + ")")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::KDBLd(2);
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
|
|
|
auto score = clf.score(raw.Xt, raw.yt);
|
2023-10-04 21:19:23 +00:00
|
|
|
//scores[{file_name, "KDBLd"}] = score;
|
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "KDBLd"
|
2023-10-06 15:08:54 +00:00
|
|
|
}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
SECTION("Test SPODE classifier (" + file_name + ")")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::SPODE(1);
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
auto score = clf.score(raw.Xv, raw.yv);
|
2023-10-04 21:19:23 +00:00
|
|
|
// scores[{file_name, "SPODE"}] = score;
|
2023-10-06 15:08:54 +00:00
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "SPODE"}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
SECTION("Test SPODELd classifier (" + file_name + ")")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::SPODELd(1);
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
|
|
|
auto score = clf.score(raw.Xt, raw.yt);
|
2023-10-04 21:19:23 +00:00
|
|
|
// scores[{file_name, "SPODELd"}] = score;
|
2023-10-06 15:08:54 +00:00
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "SPODELd"}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
SECTION("Test AODE classifier (" + file_name + ")")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::AODE();
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
auto score = clf.score(raw.Xv, raw.yv);
|
2023-10-04 21:19:23 +00:00
|
|
|
// scores[{file_name, "AODE"}] = score;
|
2023-10-06 15:08:54 +00:00
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "AODE"}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
SECTION("Test AODELd classifier (" + file_name + ")")
|
|
|
|
{
|
2023-10-04 23:14:16 +00:00
|
|
|
auto clf = bayesnet::AODELd();
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
|
|
|
auto score = clf.score(raw.Xt, raw.yt);
|
2023-10-04 21:19:23 +00:00
|
|
|
// scores[{file_name, "AODELd"}] = score;
|
2023-10-06 15:08:54 +00:00
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "AODELd"}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
SECTION("Test BoostAODE classifier (" + file_name + ")")
|
|
|
|
{
|
|
|
|
auto clf = bayesnet::BoostAODE();
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
auto score = clf.score(raw.Xv, raw.yv);
|
2023-10-04 21:19:23 +00:00
|
|
|
// scores[{file_name, "BoostAODE"}] = score;
|
2023-10-06 15:08:54 +00:00
|
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "BoostAODE"}]).epsilon(raw.epsilon));
|
2023-10-04 21:19:23 +00:00
|
|
|
}
|
|
|
|
// for (auto scores : scores) {
|
2023-11-08 17:45:35 +00:00
|
|
|
// std::cout << "{{\"" << scores.first.first << "\", \"" << scores.first.second << "\"}, " << scores.second << "}, ";
|
2023-10-04 21:19:23 +00:00
|
|
|
// }
|
|
|
|
}
|
2023-10-05 13:45:36 +00:00
|
|
|
TEST_CASE("Models features", "[BayesNet]")
|
2023-10-04 21:19:23 +00:00
|
|
|
{
|
2023-11-08 17:45:35 +00:00
|
|
|
auto graph = std::vector<std::string>({ "digraph BayesNet {\nlabel=<BayesNet Test>\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n",
|
2023-10-04 21:19:23 +00:00
|
|
|
"class [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] \n",
|
|
|
|
"class -> sepallength", "class -> sepalwidth", "class -> petallength", "class -> petalwidth", "petallength [shape=circle] \n",
|
|
|
|
"petallength -> sepallength", "petalwidth [shape=circle] \n", "sepallength [shape=circle] \n",
|
|
|
|
"sepallength -> sepalwidth", "sepalwidth [shape=circle] \n", "sepalwidth -> petalwidth", "}\n"
|
|
|
|
}
|
|
|
|
);
|
2023-10-06 15:08:54 +00:00
|
|
|
auto raw = RawDatasets("iris", true);
|
2023-10-04 21:19:23 +00:00
|
|
|
auto clf = bayesnet::TAN();
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
2024-01-07 18:58:22 +00:00
|
|
|
REQUIRE(clf.getNumberOfNodes() == 5);
|
2023-10-04 21:19:23 +00:00
|
|
|
REQUIRE(clf.getNumberOfEdges() == 7);
|
2023-11-08 17:45:35 +00:00
|
|
|
REQUIRE(clf.show() == std::vector<std::string>{"class -> sepallength, sepalwidth, petallength, petalwidth, ", "petallength -> sepallength, ", "petalwidth -> ", "sepallength -> sepalwidth, ", "sepalwidth -> petalwidth, "});
|
2023-10-04 21:19:23 +00:00
|
|
|
REQUIRE(clf.graph("Test") == graph);
|
|
|
|
}
|
2023-10-05 13:45:36 +00:00
|
|
|
TEST_CASE("Get num features & num edges", "[BayesNet]")
|
2023-10-04 21:19:23 +00:00
|
|
|
{
|
2023-10-06 15:08:54 +00:00
|
|
|
auto raw = RawDatasets("iris", true);
|
2023-10-04 21:19:23 +00:00
|
|
|
auto clf = bayesnet::KDB(2);
|
2023-10-06 15:08:54 +00:00
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
2024-01-07 18:58:22 +00:00
|
|
|
REQUIRE(clf.getNumberOfNodes() == 5);
|
2023-10-04 21:19:23 +00:00
|
|
|
REQUIRE(clf.getNumberOfEdges() == 8);
|
2024-02-09 11:06:19 +00:00
|
|
|
}
|
2024-02-12 09:58:20 +00:00
|
|
|
TEST_CASE("BoostAODE feature_select CFS", "[BayesNet]")
|
2024-02-09 11:06:19 +00:00
|
|
|
{
|
|
|
|
auto raw = RawDatasets("glass", true);
|
|
|
|
auto clf = bayesnet::BoostAODE();
|
|
|
|
clf.setHyperparameters({ {"select_features", "CFS"} });
|
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
REQUIRE(clf.getNumberOfNodes() == 90);
|
|
|
|
REQUIRE(clf.getNumberOfEdges() == 153);
|
2024-02-12 09:58:20 +00:00
|
|
|
REQUIRE(clf.getNotes().size() == 2);
|
2024-02-09 11:06:19 +00:00
|
|
|
REQUIRE(clf.getNotes()[0] == "Used features in initialization: 6 of 9 with CFS");
|
2024-02-12 09:58:20 +00:00
|
|
|
REQUIRE(clf.getNotes()[1] == "Number of models: 9");
|
|
|
|
}
|
2024-02-22 17:44:40 +00:00
|
|
|
TEST_CASE("BoostAODE test used features in train note and score", "[BayesNet]")
|
2024-02-12 09:58:20 +00:00
|
|
|
{
|
|
|
|
auto raw = RawDatasets("diabetes", true);
|
|
|
|
auto clf = bayesnet::BoostAODE();
|
|
|
|
clf.setHyperparameters({
|
|
|
|
{"ascending",true},
|
|
|
|
{"convergence", true},
|
|
|
|
{"repeatSparent",true},
|
2024-02-20 09:11:22 +00:00
|
|
|
{"select_features","CFS"},
|
2024-02-12 09:58:20 +00:00
|
|
|
});
|
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
REQUIRE(clf.getNumberOfNodes() == 72);
|
|
|
|
REQUIRE(clf.getNumberOfEdges() == 120);
|
|
|
|
REQUIRE(clf.getNotes().size() == 3);
|
|
|
|
REQUIRE(clf.getNotes()[0] == "Used features in initialization: 6 of 8 with CFS");
|
|
|
|
REQUIRE(clf.getNotes()[1] == "Used features in train: 7 of 8");
|
|
|
|
REQUIRE(clf.getNotes()[2] == "Number of models: 8");
|
2024-02-22 17:44:40 +00:00
|
|
|
auto score = clf.score(raw.Xv, raw.yv);
|
|
|
|
auto scoret = clf.score(raw.Xt, raw.yt);
|
|
|
|
REQUIRE(score == Catch::Approx(0.8138).epsilon(raw.epsilon));
|
|
|
|
REQUIRE(scoret == Catch::Approx(0.8138).epsilon(raw.epsilon));
|
2024-02-12 09:58:20 +00:00
|
|
|
}
|
2024-02-22 10:45:40 +00:00
|
|
|
TEST_CASE("TAN predict_proba", "[BayesNet]")
|
|
|
|
{
|
2024-02-22 17:44:40 +00:00
|
|
|
auto res_prob = std::vector<std::vector<double>>({
|
|
|
|
{ 0.00375671, 0.994457, 0.00178621 },
|
|
|
|
{ 0.00137462, 0.992734, 0.00589123 },
|
|
|
|
{ 0.00137462, 0.992734, 0.00589123 },
|
|
|
|
{ 0.00137462, 0.992734, 0.00589123 },
|
|
|
|
{ 0.00218225, 0.992877, 0.00494094 },
|
|
|
|
{ 0.00494209, 0.0978534, 0.897205 },
|
|
|
|
{ 0.0054192, 0.974275, 0.0203054 },
|
|
|
|
{ 0.00433012, 0.985054, 0.0106159 },
|
|
|
|
{ 0.000860806, 0.996922, 0.00221698 }
|
|
|
|
});
|
|
|
|
int init_index = 78;
|
2024-02-22 10:45:40 +00:00
|
|
|
auto raw = RawDatasets("iris", true);
|
|
|
|
auto clf = bayesnet::TAN();
|
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
auto y_pred_proba = clf.predict_proba(raw.Xv);
|
|
|
|
auto y_pred = clf.predict(raw.Xv);
|
|
|
|
auto yt_pred_proba = clf.predict_proba(raw.Xt);
|
|
|
|
REQUIRE(y_pred.size() == y_pred_proba.size());
|
|
|
|
REQUIRE(y_pred.size() == yt_pred_proba.size(0));
|
|
|
|
REQUIRE(y_pred.size() == raw.yv.size());
|
|
|
|
REQUIRE(y_pred_proba[0].size() == 3);
|
|
|
|
REQUIRE(yt_pred_proba.size(1) == y_pred_proba[0].size());
|
|
|
|
for (int i = 0; i < y_pred_proba.size(); ++i) {
|
|
|
|
auto maxElem = max_element(y_pred_proba[i].begin(), y_pred_proba[i].end());
|
|
|
|
int predictedClass = distance(y_pred_proba[i].begin(), maxElem);
|
|
|
|
REQUIRE(predictedClass == y_pred[i]);
|
2024-02-22 17:44:40 +00:00
|
|
|
// Check predict is coherent with predict_proba
|
2024-02-22 10:45:40 +00:00
|
|
|
REQUIRE(yt_pred_proba[i].argmax().item<int>() == y_pred[i]);
|
|
|
|
}
|
2024-02-22 17:44:40 +00:00
|
|
|
// Check predict_proba values for vectors and tensors
|
|
|
|
for (int i = 0; i < res_prob.size(); i++) {
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
REQUIRE(res_prob[i][j] == Catch::Approx(y_pred_proba[i + init_index][j]).epsilon(raw.epsilon));
|
|
|
|
REQUIRE(res_prob[i][j] == Catch::Approx(yt_pred_proba[i + init_index][j].item<double>()).epsilon(raw.epsilon));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_CASE("BoostAODE predict_proba voting", "[BayesNet]")
|
|
|
|
{
|
|
|
|
// auto res_prob = std::vector<std::vector<double>>({
|
|
|
|
// { 0.00375671, 0.994457, 0.00178621 },
|
|
|
|
// { 0.00137462, 0.992734, 0.00589123 },
|
|
|
|
// { 0.00137462, 0.992734, 0.00589123 },
|
|
|
|
// { 0.00137462, 0.992734, 0.00589123 },
|
|
|
|
// { 0.00218225, 0.992877, 0.00494094 },
|
|
|
|
// { 0.00494209, 0.0978534, 0.897205 },
|
|
|
|
// { 0.0054192, 0.974275, 0.0203054 },
|
|
|
|
// { 0.00433012, 0.985054, 0.0106159 },
|
|
|
|
// { 0.000860806, 0.996922, 0.00221698 }
|
|
|
|
// });
|
|
|
|
// int init_index = 78;
|
|
|
|
auto raw = RawDatasets("iris", true);
|
|
|
|
auto clf = bayesnet::BoostAODE(true);
|
|
|
|
clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv);
|
|
|
|
auto y_pred_proba = clf.predict_proba(raw.Xv);
|
|
|
|
auto y_pred = clf.predict(raw.Xv);
|
|
|
|
auto yt_pred_proba = clf.predict_proba(raw.Xt);
|
|
|
|
// REQUIRE(y_pred.size() == y_pred_proba.size());
|
|
|
|
// REQUIRE(y_pred.size() == yt_pred_proba.size(0));
|
|
|
|
// REQUIRE(y_pred.size() == raw.yv.size());
|
|
|
|
// REQUIRE(y_pred_proba[0].size() == 3);
|
|
|
|
// REQUIRE(yt_pred_proba.size(1) == y_pred_proba[0].size());
|
|
|
|
// for (int i = 0; i < y_pred_proba.size(); ++i) {
|
|
|
|
// auto maxElem = max_element(y_pred_proba[i].begin(), y_pred_proba[i].end());
|
|
|
|
// int predictedClass = distance(y_pred_proba[i].begin(), maxElem);
|
|
|
|
// REQUIRE(predictedClass == y_pred[i]);
|
|
|
|
// // Check predict is coherent with predict_proba
|
|
|
|
// REQUIRE(yt_pred_proba[i].argmax().item<int>() == y_pred[i]);
|
|
|
|
// }
|
|
|
|
// // Check predict_proba values for vectors and tensors
|
|
|
|
// for (int i = 0; i < res_prob.size(); i++) {
|
|
|
|
// for (int j = 0; j < 3; j++) {
|
|
|
|
// REQUIRE(res_prob[i][j] == Catch::Approx(y_pred_proba[i + init_index][j]).epsilon(raw.epsilon));
|
|
|
|
// REQUIRE(res_prob[i][j] == Catch::Approx(yt_pred_proba[i + init_index][j].item<double>()).epsilon(raw.epsilon));
|
|
|
|
// }
|
|
|
|
// }
|
2024-02-22 10:45:40 +00:00
|
|
|
}
|