102 lines
3.9 KiB
C++
102 lines
3.9 KiB
C++
#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>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <string>
|
|
#include <torch/torch.h>
|
|
#include "../sample/ArffFiles.h"
|
|
#include "../sample/CPPFImdlp.h"
|
|
#include "../src/KDB.h"
|
|
#include "../src/TAN.h"
|
|
#include "../src/SPODE.h"
|
|
#include "../src/AODE.h"
|
|
|
|
const string PATH = "data/";
|
|
using namespace std;
|
|
|
|
pair<vector<mdlp::labels_t>, map<string, int>> discretize(vector<mdlp::samples_t>& X, mdlp::labels_t& y, vector<string> features)
|
|
{
|
|
vector<mdlp::labels_t>Xd;
|
|
map<string, int> maxes;
|
|
|
|
auto fimdlp = mdlp::CPPFImdlp();
|
|
for (int i = 0; i < X.size(); i++) {
|
|
fimdlp.fit(X[i], y);
|
|
mdlp::labels_t& xd = fimdlp.transform(X[i]);
|
|
maxes[features[i]] = *max_element(xd.begin(), xd.end()) + 1;
|
|
Xd.push_back(xd);
|
|
}
|
|
return { Xd, maxes };
|
|
}
|
|
|
|
TEST_CASE("Test Bayesian Classifiers score", "[BayesNet]")
|
|
{
|
|
auto path = "../../data/";
|
|
map <pair<string, string>, float> scores = {
|
|
{{"diabetes", "AODE"}, 0.811198}, {{"diabetes", "KDB"}, 0.852865}, {{"diabetes", "SPODE"}, 0.802083}, {{"diabetes", "TAN"}, 0.821615},
|
|
{{"ecoli", "AODE"}, 0.889881}, {{"ecoli", "KDB"}, 0.889881}, {{"ecoli", "SPODE"}, 0.880952}, {{"ecoli", "TAN"}, 0.892857},
|
|
{{"glass", "AODE"}, 0.78972}, {{"glass", "KDB"}, 0.827103}, {{"glass", "SPODE"}, 0.775701}, {{"glass", "TAN"}, 0.827103},
|
|
{{"iris", "AODE"}, 0.973333}, {{"iris", "KDB"}, 0.973333}, {{"iris", "SPODE"}, 0.973333}, {{"iris", "TAN"}, 0.973333}
|
|
};
|
|
|
|
string file_name = GENERATE("glass", "iris", "ecoli", "diabetes");
|
|
auto handler = ArffFiles();
|
|
handler.load(path + static_cast<string>(file_name) + ".arff");
|
|
// Get Dataset X, y
|
|
vector<mdlp::samples_t>& X = handler.getX();
|
|
mdlp::labels_t& y = handler.getY();
|
|
// Get className & Features
|
|
auto className = handler.getClassName();
|
|
vector<string> features;
|
|
for (auto feature : handler.getAttributes()) {
|
|
features.push_back(feature.first);
|
|
}
|
|
// Discretize Dataset
|
|
vector<mdlp::labels_t> Xd;
|
|
map<string, int> maxes;
|
|
tie(Xd, maxes) = discretize(X, y, features);
|
|
maxes[className] = *max_element(y.begin(), y.end()) + 1;
|
|
map<string, vector<int>> states;
|
|
for (auto feature : features) {
|
|
states[feature] = vector<int>(maxes[feature]);
|
|
}
|
|
states[className] = vector<int>(maxes[className]);
|
|
SECTION("Test TAN classifier (" + file_name + ")")
|
|
{
|
|
auto clf = bayesnet::TAN();
|
|
clf.fit(Xd, y, features, className, states);
|
|
auto score = clf.score(Xd, y);
|
|
//scores[{file_name, "TAN"}] = score;
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "TAN"}]).epsilon(1e-6));
|
|
}
|
|
SECTION("Test KDB classifier (" + file_name + ")")
|
|
{
|
|
auto clf = bayesnet::KDB(2);
|
|
clf.fit(Xd, y, features, className, states);
|
|
auto score = clf.score(Xd, y);
|
|
//scores[{file_name, "KDB"}] = score;
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "KDB"
|
|
}]).epsilon(1e-6));
|
|
}
|
|
SECTION("Test SPODE classifier (" + file_name + ")")
|
|
{
|
|
auto clf = bayesnet::SPODE(1);
|
|
clf.fit(Xd, y, features, className, states);
|
|
auto score = clf.score(Xd, y);
|
|
// scores[{file_name, "SPODE"}] = score;
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "SPODE"}]).epsilon(1e-6));
|
|
}
|
|
SECTION("Test AODE classifier (" + file_name + ")")
|
|
{
|
|
auto clf = bayesnet::AODE();
|
|
clf.fit(Xd, y, features, className, states);
|
|
auto score = clf.score(Xd, y);
|
|
// scores[{file_name, "AODE"}] = score;
|
|
REQUIRE(score == Catch::Approx(scores[{file_name, "AODE"}]).epsilon(1e-6));
|
|
}
|
|
// for (auto scores : scores) {
|
|
// cout << "{{\"" << scores.first.first << "\", \"" << scores.first.second << "\"}, " << scores.second << "}, ";
|
|
// }
|
|
} |