BayesNet/main.cc

58 lines
1.8 KiB
C++
Raw Normal View History

2023-06-29 20:00:41 +00:00
#include <iostream>
#include <string>
#include <torch/torch.h>
#include "ArffFiles.h"
#include "Network.h"
2023-06-30 00:46:06 +00:00
#include "CPPFImdlp.h"
2023-06-29 20:00:41 +00:00
using namespace std;
2023-06-30 00:46:06 +00:00
vector<mdlp::labels_t> discretize(vector<mdlp::samples_t>& X, mdlp::labels_t& y)
{
vector<mdlp::labels_t>Xd;
auto fimdlp = mdlp::CPPFImdlp();
for (int i = 0; i < X.size(); i++) {
fimdlp.fit(X[i], y);
Xd.push_back(fimdlp.transform(X[i]));
}
return Xd;
}
2023-06-29 20:00:41 +00:00
int main()
{
auto handler = ArffFiles();
handler.load("iris.arff");
2023-06-30 00:46:06 +00:00
// Get Dataset X, y
vector<mdlp::samples_t>& X = handler.getX();
mdlp::labels_t& y = handler.getY();
// Get className & Features
2023-06-29 20:00:41 +00:00
auto className = handler.getClassName();
2023-06-30 00:46:06 +00:00
vector<string> features;
2023-06-29 20:00:41 +00:00
for (auto feature : handler.getAttributes()) {
2023-06-30 00:46:06 +00:00
features.push_back(feature.first);
2023-06-29 20:00:41 +00:00
}
2023-06-30 00:46:06 +00:00
// Discretize Dataset
vector<mdlp::labels_t> Xd = discretize(X, y);;
// Build Network
auto network = bayesnet::Network();
network.fit(Xd, y, features, className);
2023-06-29 20:00:41 +00:00
cout << "Hello, Bayesian Networks!" << endl;
cout << "Nodes:" << endl;
for (auto [name, item] : network.getNodes()) {
2023-06-30 00:46:06 +00:00
cout << "*" << item->getName() << " -> " << item->getNumStates() << endl;
2023-06-29 20:00:41 +00:00
cout << "-Parents:" << endl;
for (auto parent : item->getParents()) {
cout << " " << parent->getName() << endl;
}
cout << "-Children:" << endl;
for (auto child : item->getChildren()) {
cout << " " << child->getName() << endl;
}
}
2023-06-29 21:53:33 +00:00
cout << "Root: " << network.getRoot()->getName() << endl;
network.setRoot(className);
cout << "Now Root should be class: " << network.getRoot()->getName() << endl;
2023-06-30 00:46:06 +00:00
cout << "PyTorch version: " << TORCH_VERSION << endl;
2023-06-29 20:00:41 +00:00
return 0;
}