From 7876d1a37065ad7000b20b8d259cbe4d44a0c3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Wed, 12 Mar 2025 16:27:19 +0100 Subject: [PATCH] Add test --- tests/TestXBAODE.cc | 129 +++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 68 deletions(-) diff --git a/tests/TestXBAODE.cc b/tests/TestXBAODE.cc index abff33f..a79f10f 100644 --- a/tests/TestXBAODE.cc +++ b/tests/TestXBAODE.cc @@ -4,87 +4,80 @@ // SPDX-License-Identifier: MIT // *************************************************************** +#include "TestUtils.h" +#include "bayesnet/ensembles/XBAODE.h" #include #include #include #include -#include "TestUtils.h" -#include "bayesnet/ensembles/XBAODE.h" TEST_CASE("Normal test", "[XBAODE]") { - auto raw = RawDatasets("iris", true); - auto clf = bayesnet::XBAODE(); - clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, - raw.smoothing); - REQUIRE(clf.getNumberOfNodes() == 20); - REQUIRE(clf.getNumberOfEdges() == 36); - REQUIRE(clf.getNotes().size() == 1); - REQUIRE(clf.getVersion() == "0.9.7"); - REQUIRE(clf.getNotes()[0] == "Number of models: 4"); - REQUIRE(clf.getNumberOfStates() == 256); - REQUIRE(clf.score(raw.X_test, raw.y_test) == Catch::Approx(0.933333)); + auto raw = RawDatasets("iris", true); + auto clf = bayesnet::XBAODE(); + clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); + REQUIRE(clf.getNumberOfNodes() == 20); + REQUIRE(clf.getNumberOfEdges() == 36); + REQUIRE(clf.getNotes().size() == 1); + REQUIRE(clf.getVersion() == "0.9.7"); + REQUIRE(clf.getNotes()[0] == "Number of models: 4"); + REQUIRE(clf.getNumberOfStates() == 256); + REQUIRE(clf.score(raw.X_test, raw.y_test) == Catch::Approx(0.933333)); } TEST_CASE("Feature_select CFS", "[XBAODE]") { - auto raw = RawDatasets("glass", true); - auto clf = bayesnet::XBAODE(); - clf.setHyperparameters({{"select_features", "CFS"}}); - clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, - raw.smoothing); - REQUIRE(clf.getNumberOfNodes() == 90); - REQUIRE(clf.getNumberOfEdges() == 171); - REQUIRE(clf.getNotes().size() == 2); - REQUIRE(clf.getNotes()[0] == - "Used features in initialization: 6 of 9 with CFS"); - REQUIRE(clf.getNotes()[1] == "Number of models: 9"); - REQUIRE(clf.score(raw.X_test, raw.y_test) == Catch::Approx(0.720930219)); + auto raw = RawDatasets("glass", true); + auto clf = bayesnet::XBAODE(); + clf.setHyperparameters({{"select_features", "CFS"}}); + clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); + REQUIRE(clf.getNumberOfNodes() == 90); + REQUIRE(clf.getNumberOfEdges() == 171); + REQUIRE(clf.getNotes().size() == 2); + REQUIRE(clf.getNotes()[0] == "Used features in initialization: 6 of 9 with CFS"); + REQUIRE(clf.getNotes()[1] == "Number of models: 9"); + REQUIRE(clf.score(raw.X_test, raw.y_test) == Catch::Approx(0.720930219)); } TEST_CASE("Feature_select IWSS", "[XBAODE]") { - auto raw = RawDatasets("glass", true); - auto clf = bayesnet::XBAODE(); - clf.setHyperparameters({{"select_features", "IWSS"}, {"threshold", 0.5}}); - clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, - raw.smoothing); - REQUIRE(clf.getNumberOfNodes() == 90); - REQUIRE(clf.getNumberOfEdges() == 171); - REQUIRE(clf.getNotes().size() == 2); - REQUIRE(clf.getNotes()[0] == - "Used features in initialization: 4 of 9 with IWSS"); - REQUIRE(clf.getNotes()[1] == "Number of models: 9"); - REQUIRE(clf.score(raw.X_test, raw.y_test) == Catch::Approx(0.697674394)); + auto raw = RawDatasets("glass", true); + auto clf = bayesnet::XBAODE(); + clf.setHyperparameters({{"select_features", "IWSS"}, {"threshold", 0.5}}); + clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); + REQUIRE(clf.getNumberOfNodes() == 90); + REQUIRE(clf.getNumberOfEdges() == 171); + REQUIRE(clf.getNotes().size() == 2); + REQUIRE(clf.getNotes()[0] == "Used features in initialization: 4 of 9 with IWSS"); + REQUIRE(clf.getNotes()[1] == "Number of models: 9"); + REQUIRE(clf.score(raw.X_test, raw.y_test) == Catch::Approx(0.697674394)); } TEST_CASE("Feature_select FCBF", "[XBAODE]") { - auto raw = RawDatasets("glass", true); - auto clf = bayesnet::XBAODE(); - clf.setHyperparameters({{"select_features", "FCBF"}, {"threshold", 1e-7}}); - clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, - raw.smoothing); - REQUIRE(clf.getNumberOfNodes() == 90); - REQUIRE(clf.getNumberOfEdges() == 171); - REQUIRE(clf.getNotes().size() == 2); - REQUIRE(clf.getNotes()[0] == - "Used features in initialization: 4 of 9 with FCBF"); - REQUIRE(clf.getNotes()[1] == "Number of models: 9"); - REQUIRE(clf.score(raw.X_test, raw.y_test) == Catch::Approx(0.720930219)); + auto raw = RawDatasets("glass", true); + auto clf = bayesnet::XBAODE(); + clf.setHyperparameters({{"select_features", "FCBF"}, {"threshold", 1e-7}}); + clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); + REQUIRE(clf.getNumberOfNodes() == 90); + REQUIRE(clf.getNumberOfEdges() == 171); + REQUIRE(clf.getNotes().size() == 2); + REQUIRE(clf.getNotes()[0] == "Used features in initialization: 4 of 9 with FCBF"); + REQUIRE(clf.getNotes()[1] == "Number of models: 9"); + REQUIRE(clf.score(raw.X_test, raw.y_test) == Catch::Approx(0.720930219)); +} +TEST_CASE("Test used features in train note and score", "[XBAODE]") { + auto raw = RawDatasets("diabetes", true); + auto clf = bayesnet::XBAODE(); + clf.setHyperparameters({ + {"order", "asc"}, + {"convergence", true}, + {"select_features", "CFS"}, + }); + clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); + REQUIRE(clf.getNumberOfNodes() == 72); + REQUIRE(clf.getNumberOfEdges() == 136); + REQUIRE(clf.getNotes().size() == 2); + REQUIRE(clf.getNotes()[0] == "Used features in initialization: 6 of 8 with CFS"); + REQUIRE(clf.getNotes()[1] == "Number of models: 8"); + auto score = clf.score(raw.Xv, raw.yv); + auto scoret = clf.score(raw.Xt, raw.yt); + REQUIRE(score == Catch::Approx(0.819010437f).epsilon(raw.epsilon)); + REQUIRE(scoret == Catch::Approx(0.819010437f).epsilon(raw.epsilon)); } - TEST_CASE("Test used features in train note and score", "[XBAODE]") - { - auto raw = RawDatasets("diabetes", true); - auto clf = bayesnet::XBAODE(); - clf.setHyperparameters({ - {"order", "asc"}, - {"convergence", true}, - {"select_features","CFS"}, - }); - clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, - raw.smoothing); REQUIRE(clf.getNumberOfNodes() == 72); - REQUIRE(clf.getNumberOfEdges() == 136); - REQUIRE(clf.getNotes().size() == 2); - REQUIRE(clf.getNotes()[0] == "Used features in initialization: 6 of 8 with CFS"); - REQUIRE(clf.getNotes()[1] == "Number of models: 8"); - auto score = clf.score(raw.Xv, raw.yv); auto scoret = clf.score(raw.Xt, raw.yt); - REQUIRE(score == Catch::Approx(0.819010437f).epsilon(raw.epsilon)); - REQUIRE(scoret == Catch::Approx(0.819010437f).epsilon(raw.epsilon)); - } // TEST_CASE("Voting vs proba", "[XBAODE]") // { // auto raw = RawDatasets("iris", true);