Add predict_proba to Ld classifiers
This commit is contained in:
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
- Fix the vcpkg configuration in building the library.
|
- Fix the vcpkg configuration in building the library.
|
||||||
- Fix the sample app to use the vcpkg configuration.
|
- Fix the sample app to use the vcpkg configuration.
|
||||||
|
- Add predict_proba method to all Ld classifiers.
|
||||||
|
|
||||||
## [1.1.0] - 2025-04-27
|
## [1.1.0] - 2025-04-27
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
[](https://app.codacy.com/gh/Doctorado-ML/BayesNet/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
[](https://app.codacy.com/gh/Doctorado-ML/BayesNet/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
||||||
[](https://sonarcloud.io/summary/new_code?id=rmontanana_BayesNet)
|
[](https://sonarcloud.io/summary/new_code?id=rmontanana_BayesNet)
|
||||||
[](https://sonarcloud.io/summary/new_code?id=rmontanana_BayesNet)
|
[](https://sonarcloud.io/summary/new_code?id=rmontanana_BayesNet)
|
||||||
|
[](https://deepwiki.com/Doctorado-ML/BayesNet)
|
||||||

|

|
||||||
[](https://gitea.rmontanana.es/rmontanana/BayesNet)
|
[](https://gitea.rmontanana.es/rmontanana/BayesNet)
|
||||||
[](https://doi.org/10.5281/zenodo.14210344)
|
[](https://doi.org/10.5281/zenodo.14210344)
|
||||||
|
@@ -28,6 +28,11 @@ namespace bayesnet {
|
|||||||
auto Xt = prepareX(X);
|
auto Xt = prepareX(X);
|
||||||
return KDB::predict(Xt);
|
return KDB::predict(Xt);
|
||||||
}
|
}
|
||||||
|
torch::Tensor KDBLd::predict_proba(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
auto Xt = prepareX(X);
|
||||||
|
return KDB::predict_proba(Xt);
|
||||||
|
}
|
||||||
std::vector<std::string> KDBLd::graph(const std::string& name) const
|
std::vector<std::string> KDBLd::graph(const std::string& name) const
|
||||||
{
|
{
|
||||||
return KDB::graph(name);
|
return KDB::graph(name);
|
||||||
|
@@ -18,6 +18,7 @@ namespace bayesnet {
|
|||||||
KDBLd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states, const Smoothing_t smoothing) override;
|
KDBLd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states, const Smoothing_t smoothing) override;
|
||||||
std::vector<std::string> graph(const std::string& name = "KDB") const override;
|
std::vector<std::string> graph(const std::string& name = "KDB") const override;
|
||||||
torch::Tensor predict(torch::Tensor& X) override;
|
torch::Tensor predict(torch::Tensor& X) override;
|
||||||
|
torch::Tensor predict_proba(torch::Tensor& X) override;
|
||||||
static inline std::string version() { return "0.0.1"; };
|
static inline std::string version() { return "0.0.1"; };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -43,6 +43,11 @@ namespace bayesnet {
|
|||||||
auto Xt = prepareX(X);
|
auto Xt = prepareX(X);
|
||||||
return SPODE::predict(Xt);
|
return SPODE::predict(Xt);
|
||||||
}
|
}
|
||||||
|
torch::Tensor SPODELd::predict_proba(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
auto Xt = prepareX(X);
|
||||||
|
return SPODE::predict_proba(Xt);
|
||||||
|
}
|
||||||
std::vector<std::string> SPODELd::graph(const std::string& name) const
|
std::vector<std::string> SPODELd::graph(const std::string& name) const
|
||||||
{
|
{
|
||||||
return SPODE::graph(name);
|
return SPODE::graph(name);
|
||||||
|
@@ -19,6 +19,7 @@ namespace bayesnet {
|
|||||||
SPODELd& commonFit(const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states, const Smoothing_t smoothing);
|
SPODELd& commonFit(const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states, const Smoothing_t smoothing);
|
||||||
std::vector<std::string> graph(const std::string& name = "SPODELd") const override;
|
std::vector<std::string> graph(const std::string& name = "SPODELd") const override;
|
||||||
torch::Tensor predict(torch::Tensor& X) override;
|
torch::Tensor predict(torch::Tensor& X) override;
|
||||||
|
torch::Tensor predict_proba(torch::Tensor& X) override;
|
||||||
static inline std::string version() { return "0.0.1"; };
|
static inline std::string version() { return "0.0.1"; };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -29,6 +29,11 @@ namespace bayesnet {
|
|||||||
auto Xt = prepareX(X);
|
auto Xt = prepareX(X);
|
||||||
return TAN::predict(Xt);
|
return TAN::predict(Xt);
|
||||||
}
|
}
|
||||||
|
torch::Tensor TANLd::predict_proba(torch::Tensor& X)
|
||||||
|
{
|
||||||
|
auto Xt = prepareX(X);
|
||||||
|
return TAN::predict_proba(Xt);
|
||||||
|
}
|
||||||
std::vector<std::string> TANLd::graph(const std::string& name) const
|
std::vector<std::string> TANLd::graph(const std::string& name) const
|
||||||
{
|
{
|
||||||
return TAN::graph(name);
|
return TAN::graph(name);
|
||||||
|
@@ -18,6 +18,7 @@ namespace bayesnet {
|
|||||||
TANLd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states, const Smoothing_t smoothing) override;
|
TANLd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states, const Smoothing_t smoothing) override;
|
||||||
std::vector<std::string> graph(const std::string& name = "TANLd") const override;
|
std::vector<std::string> graph(const std::string& name = "TANLd") const override;
|
||||||
torch::Tensor predict(torch::Tensor& X) override;
|
torch::Tensor predict(torch::Tensor& X) override;
|
||||||
|
torch::Tensor predict_proba(torch::Tensor& X) override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif // !TANLD_H
|
#endif // !TANLD_H
|
@@ -10,7 +10,25 @@ find_package(Torch CONFIG REQUIRED)
|
|||||||
find_package(fimdlp CONFIG REQUIRED)
|
find_package(fimdlp CONFIG REQUIRED)
|
||||||
find_package(folding CONFIG REQUIRED)
|
find_package(folding CONFIG REQUIRED)
|
||||||
find_package(arff-files CONFIG REQUIRED)
|
find_package(arff-files CONFIG REQUIRED)
|
||||||
find_package(bayesnet CONFIG REQUIRED)
|
find_package(nlohmann_json CONFIG REQUIRED)
|
||||||
|
|
||||||
|
option(BAYESNET_VCPKG_CONFIG "Use vcpkg config for BayesNet" ON)
|
||||||
|
|
||||||
|
if (BAYESNET_VCPKG_CONFIG)
|
||||||
|
message(STATUS "Using BayesNet vcpkg config")
|
||||||
|
find_package(bayesnet CONFIG REQUIRED)
|
||||||
|
set(BayesNet_LIBRARIES bayesnet::bayesnet)
|
||||||
|
else(BAYESNET_VCPKG_CONFIG)
|
||||||
|
message(STATUS "Using BayesNet local library config")
|
||||||
|
find_library(bayesnet NAMES libbayesnet bayesnet libbayesnet.a PATHS ${Platform_SOURCE_DIR}/../lib/lib REQUIRED)
|
||||||
|
find_path(Bayesnet_INCLUDE_DIRS REQUIRED NAMES bayesnet PATHS ${Platform_SOURCE_DIR}/../lib/include)
|
||||||
|
add_library(bayesnet::bayesnet UNKNOWN IMPORTED)
|
||||||
|
set_target_properties(bayesnet::bayesnet PROPERTIES
|
||||||
|
IMPORTED_LOCATION ${bayesnet}
|
||||||
|
INTERFACE_INCLUDE_DIRECTORIES ${Bayesnet_INCLUDE_DIRS}
|
||||||
|
)
|
||||||
|
endif(BAYESNET_VCPKG_CONFIG)
|
||||||
|
message(STATUS "BayesNet: ${bayesnet}")
|
||||||
|
|
||||||
add_executable(bayesnet_sample sample.cc)
|
add_executable(bayesnet_sample sample.cc)
|
||||||
target_link_libraries(bayesnet_sample PRIVATE
|
target_link_libraries(bayesnet_sample PRIVATE
|
||||||
|
114
sample/sample.cc
114
sample/sample.cc
@@ -4,9 +4,22 @@
|
|||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// ***************************************************************
|
// ***************************************************************
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
#include <ArffFiles/ArffFiles.hpp>
|
#include <ArffFiles/ArffFiles.hpp>
|
||||||
#include <fimdlp/CPPFImdlp.h>
|
#include <fimdlp/CPPFImdlp.h>
|
||||||
#include <bayesnet/ensembles/XBAODE.h>
|
#include <bayesnet/classifiers/TANLd.h>
|
||||||
|
#include <bayesnet/classifiers/KDBLd.h>
|
||||||
|
#include <bayesnet/ensembles/AODELd.h>
|
||||||
|
|
||||||
|
torch::Tensor matrix2tensor(const std::vector<std::vector<float>>& matrix)
|
||||||
|
{
|
||||||
|
auto tensor = torch::empty({ static_cast<int>(matrix.size()), static_cast<int>(matrix[0].size()) }, torch::kFloat32);
|
||||||
|
for (int i = 0; i < matrix.size(); ++i) {
|
||||||
|
tensor.index_put_({ i, "..." }, torch::tensor(matrix[i], torch::kFloat32));
|
||||||
|
}
|
||||||
|
return tensor;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<mdlp::labels_t> discretizeDataset(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y)
|
std::vector<mdlp::labels_t> discretizeDataset(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y)
|
||||||
{
|
{
|
||||||
@@ -19,32 +32,40 @@ std::vector<mdlp::labels_t> discretizeDataset(std::vector<mdlp::samples_t>& X, m
|
|||||||
}
|
}
|
||||||
return Xd;
|
return Xd;
|
||||||
}
|
}
|
||||||
tuple<torch::Tensor, torch::Tensor, std::vector<std::string>, std::string, map<std::string, std::vector<int>>> loadDataset(const std::string& name, bool class_last)
|
std::tuple<torch::Tensor, torch::Tensor, std::vector<std::string>, std::string> loadArff(const std::string& name, bool class_last)
|
||||||
{
|
{
|
||||||
auto handler = ArffFiles();
|
auto handler = ArffFiles();
|
||||||
handler.load(name, class_last);
|
handler.load(name, class_last);
|
||||||
// Get Dataset X, y
|
// Get Dataset X, y
|
||||||
std::vector<mdlp::samples_t>& X = handler.getX();
|
std::vector<mdlp::samples_t> X = handler.getX();
|
||||||
mdlp::labels_t& y = handler.getY();
|
mdlp::labels_t y = handler.getY();
|
||||||
// Get className & Features
|
|
||||||
auto className = handler.getClassName();
|
|
||||||
std::vector<std::string> features;
|
std::vector<std::string> features;
|
||||||
auto attributes = handler.getAttributes();
|
auto attributes = handler.getAttributes();
|
||||||
transform(attributes.begin(), attributes.end(), back_inserter(features), [](const auto& pair) { return pair.first; });
|
transform(attributes.begin(), attributes.end(), back_inserter(features), [](const auto& pair) { return pair.first; });
|
||||||
torch::Tensor Xd;
|
auto Xt = matrix2tensor(X);
|
||||||
auto states = map<std::string, std::vector<int>>();
|
auto yt = torch::tensor(y, torch::kInt32);
|
||||||
auto Xr = discretizeDataset(X, y);
|
return { Xt, yt, features, handler.getClassName() };
|
||||||
Xd = torch::zeros({ static_cast<int>(Xr.size()), static_cast<int>(Xr[0].size()) }, torch::kInt32);
|
|
||||||
for (int i = 0; i < features.size(); ++i) {
|
|
||||||
states[features[i]] = std::vector<int>(*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<int>(*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 };
|
|
||||||
}
|
}
|
||||||
|
// tuple<torch::Tensor, torch::Tensor, std::vector<std::string>, std::string, map<std::string, std::vector<int>>> loadDataset(const std::string& name, bool class_last)
|
||||||
|
// {
|
||||||
|
// auto [X, y, features, className] = loadArff(name, class_last);
|
||||||
|
// // Discretize the dataset
|
||||||
|
// torch::Tensor Xd;
|
||||||
|
// auto states = map<std::string, std::vector<int>>();
|
||||||
|
// // Fill the class states
|
||||||
|
// states[className] = std::vector<int>(*max_element(y.begin(), y.end()) + 1);
|
||||||
|
// iota(begin(states.at(className)), end(states.at(className)), 0);
|
||||||
|
// auto Xr = discretizeDataset(X, y);
|
||||||
|
// Xd = torch::zeros({ static_cast<int>(Xr.size()), static_cast<int>(Xr[0].size()) }, torch::kInt32);
|
||||||
|
// for (int i = 0; i < features.size(); ++i) {
|
||||||
|
// states[features[i]] = std::vector<int>(*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));
|
||||||
|
// }
|
||||||
|
// auto yt = torch::tensor(y, torch::kInt32);
|
||||||
|
// return { Xd, yt, features, className, states };
|
||||||
|
// }
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
@@ -53,29 +74,42 @@ int main(int argc, char* argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::string file_name = argv[1];
|
std::string file_name = argv[1];
|
||||||
torch::Tensor X, y;
|
std::string model_name = argv[2];
|
||||||
std::vector<std::string> features;
|
std::map<std::string, bayesnet::Classifier*> models{ {"TANLd", new bayesnet::TANLd()}, {"KDBLd", new bayesnet::KDBLd(2)}, {"AODELd", new bayesnet::AODELd() }
|
||||||
std::string className;
|
};
|
||||||
map<std::string, std::vector<int>> states;
|
if (models.find(model_name) == models.end()) {
|
||||||
auto clf = bayesnet::XBAODE(); // false for not using voting in predict
|
std::cerr << "Model not found: " << model_name << std::endl;
|
||||||
std::cout << "Library version: " << clf.getVersion() << std::endl;
|
return 1;
|
||||||
tie(X, y, features, className, states) = loadDataset(file_name, true);
|
|
||||||
torch::Tensor weights = torch::full({ X.size(1) }, 15, torch::kDouble);
|
|
||||||
torch::Tensor dataset;
|
|
||||||
try {
|
|
||||||
auto yresized = torch::transpose(y.view({ y.size(0), 1 }), 0, 1);
|
|
||||||
dataset = torch::cat({ X, yresized }, 0);
|
|
||||||
}
|
}
|
||||||
catch (const std::exception& e) {
|
auto clf = models[model_name];
|
||||||
std::stringstream oss;
|
std::cout << "Library version: " << clf->getVersion() << std::endl;
|
||||||
oss << "* Error in X and y dimensions *\n";
|
// auto [X, y, features, className, states] = loadDataset(file_name, true);
|
||||||
oss << "X dimensions: " << dataset.sizes() << "\n";
|
auto [Xt, yt, features, className] = loadArff(file_name, true);
|
||||||
oss << "y dimensions: " << y.sizes();
|
std::map<std::string, std::vector<int>> states;
|
||||||
throw std::runtime_error(oss.str());
|
// int m = Xt.size(1);
|
||||||
|
// auto weights = torch::full({ m }, 1 / m, torch::kDouble);
|
||||||
|
// auto dataset = buildDataset(Xv, yv);
|
||||||
|
// try {
|
||||||
|
// auto yresized = torch::transpose(y.view({ y.size(0), 1 }), 0, 1);
|
||||||
|
// dataset = torch::cat({ X, yresized }, 0);
|
||||||
|
// }
|
||||||
|
// catch (const std::exception& e) {
|
||||||
|
// std::stringstream oss;
|
||||||
|
// oss << "* Error in X and y dimensions *\n";
|
||||||
|
// oss << "X dimensions: " << dataset.sizes() << "\n";
|
||||||
|
// oss << "y dimensions: " << y.sizes();
|
||||||
|
// throw std::runtime_error(oss.str());
|
||||||
|
// }
|
||||||
|
clf->fit(Xt, yt, features, className, states, bayesnet::Smoothing_t::ORIGINAL);
|
||||||
|
auto total = yt.size(0);
|
||||||
|
auto y_proba = clf->predict_proba(Xt);
|
||||||
|
auto y_pred = y_proba.argmax(1);
|
||||||
|
auto accuracy_value = (y_pred == yt).sum().item<float>() / total;
|
||||||
|
auto score = clf->score(Xt, yt);
|
||||||
|
std::cout << "File: " << file_name << " Model: " << model_name << " score: " << score << " Computed accuracy: " << accuracy_value << std::endl;
|
||||||
|
for (const auto clf : models) {
|
||||||
|
delete clf.second;
|
||||||
}
|
}
|
||||||
clf.fit(dataset, features, className, states, weights, bayesnet::Smoothing_t::LAPLACE);
|
|
||||||
auto score = clf.score(X, y);
|
|
||||||
std::cout << "File: " << file_name << " Model: BoostAODE score: " << score << std::endl;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
"fimdlp",
|
"fimdlp",
|
||||||
"libtorch-bin",
|
"libtorch-bin",
|
||||||
"folding",
|
"folding",
|
||||||
"bayesnet"
|
"nlohmann-json"
|
||||||
],
|
],
|
||||||
"overrides": [
|
"overrides": [
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user