refactor folders

This commit is contained in:
2023-11-11 10:52:35 +01:00
parent 74fb0968c7
commit b6a3a05020
9 changed files with 119 additions and 105 deletions

8
example/CMakeLists.txt Normal file
View File

@@ -0,0 +1,8 @@
include_directories(${PyWrap_SOURCE_DIR}/lib/Files)
include_directories(${PyWrap_SOURCE_DIR}/lib/json/include)
include_directories(${Python3_INCLUDE_DIRS})
include_directories(${PyWrap_SOURCE_DIR}/src)
include_directories(${TORCH_INCLUDE_DIRS})
add_executable(example example.cc)
target_link_libraries(example PyWrap)

97
example/example.cc Normal file
View File

@@ -0,0 +1,97 @@
#include <torch/torch.h>
#include "ArffFiles.h"
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <tuple>
#include "STree.h"
#include "SVC.h"
#include "RandomForest.h"
#include "XGBoost.h"
using namespace std;
using namespace torch;
class Paths {
public:
static string datasets()
{
return "../discretizbench/datasets/";
}
};
tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadDataset(const string& name, bool class_last)
{
auto handler = ArffFiles();
handler.load(Paths::datasets() + static_cast<string>(name) + ".arff", class_last);
// Get Dataset X, y
vector<vector<float>> X = handler.getX();
vector<int> y = handler.getY();
// Get className & Features
auto className = handler.getClassName();
vector<string> features;
auto attributes = handler.getAttributes();
transform(attributes.begin(), attributes.end(), back_inserter(features), [](const auto& pair) { return pair.first; });
Tensor Xd;
auto states = map<string, vector<int>>();
Xd = torch::zeros({ static_cast<int>(X.size()), static_cast<int>(X[0].size()) }, torch::kFloat32);
for (int i = 0; i < features.size(); ++i) {
Xd.index_put_({ i, "..." }, torch::tensor(X[i], torch::kFloat32));
}
return { Xd, torch::tensor(y, torch::kInt32), features, className, states };
}
int main(int argc, char* argv[])
{
using json = nlohmann::json;
cout << "* Begin." << endl;
{
using namespace torch::indexing;
auto datasetName = "iris";
bool class_last = true;
auto [X, y, features, className, states] = loadDataset(datasetName, class_last);
auto m = y.size(0);
int train_split = m * .7;
auto Xtrain = X.index({ "...", Slice(0, train_split) });
auto ytrain = y.index({ Slice(0, train_split) });
auto Xtest = X.index({ "...", Slice(train_split, m) });
auto ytest = y.index({ Slice(train_split, m) });
cout << "Dataset: " << datasetName << endl;
cout << "X: " << X.sizes() << endl;
cout << "y: " << y.sizes() << endl;
// auto clf = pywrap::STree();
// auto stree = pywrap::STree();
// auto hyperparameters = json::parse("{\"C\": 0.7, \"max_iter\": 10000, \"kernel\": \"rbf\", \"random_state\": 17}");
// stree.setHyperparameters(hyperparameters);
// cout << "STree Version: " << clf.version() << endl;
// auto svc = pywrap::SVC();
// cout << "SVC with hyperparameters" << endl;
// svc.fit(X, y, features, className, states);
// cout << "Graph: " << endl << clf.graph() << endl;
// double clf_score = clf.fit(X, y, features, className, states).score(X, y);
// double stree_score = stree.fit(X, y, features, className, states).score(X, y);
// auto prediction = clf.predict(X);
// cout << "Prediction: " << endl << "{";
// for (int i = 0; i < prediction.size(0); ++i) {
// cout << prediction[i].item<int>() << ", ";
// }
// cout << "}" << endl;
// cout << "Building Random Forest" << endl;
// auto rf = pywrap::RandomForest();
// rf.fit(X, y, features, className, states);
cout << "Building XGBoost" << endl;
auto xg = pywrap::XGBoost();
cout << "Fitting XGBoost" << endl;
xg.fit(Xtrain, ytrain, features, className, states);
cout << "Scoring dataset" << endl;
double xg_score = xg.score(Xtest, ytest);
// cout << "Scores:" << endl;
// cout << "STree Score ......: " << clf_score << endl;
// cout << "STree hyper score : " << stree_score << endl;
// cout << "RandomForest Score: " << rf.score(X, y) << endl;
// cout << "SVC Score ........: " << svc.score(X, y) << endl;
cout << "XGBoost Score ....: " << xg_score << endl;
}
cout << "* End." << endl;
}