diff --git a/Makefile b/Makefile index 836c853..d48d477 100644 --- a/Makefile +++ b/Makefile @@ -99,7 +99,15 @@ sample: ## Build sample @if [ -d ./sample/build ]; then rm -rf ./sample/build; fi @cd sample && cmake -B build -S . && cmake --build build -t bayesnet_sample sample/build/bayesnet_sample $(fname) - @echo ">>> Done"; + @echo ">>> Done"; + +fname = "tests/data/iris.arff" +sample2: ## Build sample2 + @echo ">>> Building Sample..."; + @if [ -d ./sample/build ]; then rm -rf ./sample/build; fi + @cd sample && cmake -B build -S . && cmake --build build -t bayesnet_sample_xspode + sample/build/bayesnet_sample_xspode $(fname) + @echo ">>> Done"; opt = "" test: ## Run tests (opt="-s") to verbose output the tests, (opt="-c='Test Maximum Spanning Tree'") to run only that section diff --git a/sample/CMakeLists.txt b/sample/CMakeLists.txt index d3e79a4..b6f78ac 100644 --- a/sample/CMakeLists.txt +++ b/sample/CMakeLists.txt @@ -22,4 +22,6 @@ include_directories( ) add_executable(bayesnet_sample sample.cc) -target_link_libraries(bayesnet_sample ${FImdlp} "${TORCH_LIBRARIES}" "${BayesNet}") \ No newline at end of file +target_link_libraries(bayesnet_sample ${FImdlp} "${TORCH_LIBRARIES}" "${BayesNet}") +add_executable(bayesnet_sample_xspode sample_xspode.cc) +target_link_libraries(bayesnet_sample_xspode ${FImdlp} "${TORCH_LIBRARIES}" "${BayesNet}") \ No newline at end of file diff --git a/sample/sample_xspode.cc b/sample/sample_xspode.cc new file mode 100644 index 0000000..a071ae3 --- /dev/null +++ b/sample/sample_xspode.cc @@ -0,0 +1,77 @@ +// *************************************************************** +// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez +// SPDX-FileType: SOURCE +// SPDX-License-Identifier: MIT +// *************************************************************** + +#include +#include +#include +#include + +std::vector discretizeDataset(std::vector& X, mdlp::labels_t& y) +{ + std::vector Xd; + 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]); + Xd.push_back(xd); + } + return Xd; +} +tuple>, std::vector, std::vector, std::string, map>> loadDataset(const std::string& name, bool class_last) +{ + auto handler = ArffFiles(); + handler.load(name, class_last); + // Get Dataset X, y + std::vector& X = handler.getX(); + mdlp::labels_t y = handler.getY(); + // Get className & Features + auto className = handler.getClassName(); + std::vector features; + auto attributes = handler.getAttributes(); + transform(attributes.begin(), attributes.end(), back_inserter(features), [](const auto& pair) { return pair.first; }); + torch::Tensor Xd; + auto states = map>(); + auto Xr = discretizeDataset(X, y); + for (int i = 0; i < features.size(); ++i) { + states[features[i]] = std::vector(*max_element(Xr[i].begin(), Xr[i].end()) + 1); + auto item = states.at(features[i]); + iota(begin(item), end(item), 0); + } + states[className] = std::vector(*max_element(y.begin(), y.end()) + 1); + iota(begin(states.at(className)), end(states.at(className)), 0); + return { Xr, y, features, className, states }; + // Xd = torch::zeros({ static_cast(Xr.size()), static_cast(Xr[0].size()) }, torch::kInt32); + // for (int i = 0; i < features.size(); ++i) { + // states[features[i]] = std::vector(*max_element(Xr[i].begin(), Xr[i].end()) + 1); + // auto item = states.at(features[i]); + // iota(begin(item), end(item), 0); + // Xd.index_put_({ i, "..." }, torch::tensor(Xr[i], torch::kInt32)); + // } + // states[className] = std::vector(*max_element(y.begin(), y.end()) + 1); + // iota(begin(states.at(className)), end(states.at(className)), 0); + // return { Xd, torch::tensor(y, torch::kInt32), features, className, states }; +} + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; + } + std::string file_name = argv[1]; + // auto clf = bayesnet::BoostAODE(false); // false for not using voting in predict + bayesnet::BaseClassifier* clf = new bayesnet::XSpode(0); // false for not using voting in predict + std::cout << "Library version: " << clf->getVersion() << std::endl; + auto [X, y, features, className, states] = loadDataset(file_name, true); + torch::Tensor weights = torch::full({ static_cast(X[0].size()) }, 1.0 / X[0].size(), torch::kDouble); + clf->fit(X, y, features, className, states, bayesnet::Smoothing_t::ORIGINAL); + // auto score = clf.score(X, y); + auto score = clf->score(X, y); + std::cout << "File: " << file_name << " Model: XSpode(0) score: " << score << std::endl; + delete clf; + return 0; +} +