#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do #include #include #include #include #include #include #include #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, map> discretize(vector& X, mdlp::labels_t& y, vector features) { vectorXd; map 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 , 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(file_name) + ".arff"); // Get Dataset X, y vector& X = handler.getX(); mdlp::labels_t& y = handler.getY(); // Get className & Features auto className = handler.getClassName(); vector features; for (auto feature : handler.getAttributes()) { features.push_back(feature.first); } // Discretize Dataset vector Xd; map maxes; tie(Xd, maxes) = discretize(X, y, features); maxes[className] = *max_element(y.begin(), y.end()) + 1; map> states; for (auto feature : features) { states[feature] = vector(maxes[feature]); } states[className] = vector(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 << "}, "; // } }