From 1f46fc6c247e1d619b98746066c9688ed50f31c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Tue, 7 Nov 2023 17:41:37 +0100 Subject: [PATCH] Add XGBoost & RandomForest --- src/CMakeLists.txt | 2 +- src/PyHelper.hpp | 14 -------------- src/PyWrap.cc | 1 + src/RandomForest.cc | 8 ++++++++ src/RandomForest.h | 13 +++++++++++++ src/SVC.cc | 2 +- src/XGBoost.cc | 8 ++++++++ src/XGBoost.h | 13 +++++++++++++ src/main.cc | 22 +++++++++++++++------- 9 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 src/RandomForest.cc create mode 100644 src/RandomForest.h create mode 100644 src/XGBoost.cc create mode 100644 src/XGBoost.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c2a109b..9fd677d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,7 +2,7 @@ include_directories(${PyWrap_SOURCE_DIR}/lib/Files) include_directories(${Python3_INCLUDE_DIRS}) include_directories(${TORCH_INCLUDE_DIRS}) -add_executable(main main.cc STree.cc SVC.cc PyClassifier.cc PyWrap.cc) +add_executable(main main.cc STree.cc SVC.cc RandomForest.cc PyClassifier.cc PyWrap.cc) add_executable(example example.cpp PyWrap.cc) target_link_libraries(main ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::boost Boost::python Boost::numpy ArffFiles) diff --git a/src/PyHelper.hpp b/src/PyHelper.hpp index 2db3262..fba31e7 100644 --- a/src/PyHelper.hpp +++ b/src/PyHelper.hpp @@ -23,8 +23,6 @@ namespace pywrap { Py_Finalize(); } }; - - class CPyObject { private: PyObject* p; @@ -36,23 +34,18 @@ namespace pywrap { CPyObject(PyObject* _p) : p(_p) { } - - ~CPyObject() { Release(); } - PyObject* getObject() { return p; } - PyObject* setObject(PyObject* _p) { return (p = _p); } - PyObject* AddRef() { if (p) { @@ -60,7 +53,6 @@ namespace pywrap { } return p; } - void Release() { if (p) { @@ -69,33 +61,27 @@ namespace pywrap { p = NULL; } - PyObject* operator ->() { return p; } - bool is() { return p ? true : false; } - operator PyObject* () { return p; } - PyObject* operator = (PyObject* pp) { p = pp; return p; } - operator bool() { return p ? true : false; } }; - } /* namespace pywrap */ #endif \ No newline at end of file diff --git a/src/PyWrap.cc b/src/PyWrap.cc index 2def016..0c30326 100644 --- a/src/PyWrap.cc +++ b/src/PyWrap.cc @@ -143,6 +143,7 @@ namespace pywrap { RemoveInstance(); exit(1); } + Py_INCREF(result); return result; // Caller must free this object } double PyWrap::score(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y) diff --git a/src/RandomForest.cc b/src/RandomForest.cc new file mode 100644 index 0000000..196b1a7 --- /dev/null +++ b/src/RandomForest.cc @@ -0,0 +1,8 @@ +#include "RandomForest.h" + +namespace pywrap { + std::string RandomForest::version() + { + return callMethodString("1.0"); + } +} /* namespace pywrap */ \ No newline at end of file diff --git a/src/RandomForest.h b/src/RandomForest.h new file mode 100644 index 0000000..ad906c1 --- /dev/null +++ b/src/RandomForest.h @@ -0,0 +1,13 @@ +#ifndef RANDOMFOREST_H +#define RANDOMFOREST_H +#include "PyClassifier.h" + +namespace pywrap { + class RandomForest : public PyClassifier { + public: + RandomForest() : PyClassifier("sklearn.ensemble", "RandomForestClassifier") {}; + ~RandomForest() = default; + std::string version(); + }; +} /* namespace pywrap */ +#endif /* RANDOMFOREST_H */ \ No newline at end of file diff --git a/src/SVC.cc b/src/SVC.cc index e299a40..9268c8b 100644 --- a/src/SVC.cc +++ b/src/SVC.cc @@ -3,6 +3,6 @@ namespace pywrap { std::string SVC::version() { - return callMethodString("_repr_html_"); + return callMethodString("1.0"); } } /* namespace pywrap */ \ No newline at end of file diff --git a/src/XGBoost.cc b/src/XGBoost.cc new file mode 100644 index 0000000..636680f --- /dev/null +++ b/src/XGBoost.cc @@ -0,0 +1,8 @@ +#include "XGBoost.h" + +namespace pywrap { + std::string XGBoost::version() + { + return callMethodString("1.0"); + } +} /* namespace pywrap */ \ No newline at end of file diff --git a/src/XGBoost.h b/src/XGBoost.h new file mode 100644 index 0000000..aaf996f --- /dev/null +++ b/src/XGBoost.h @@ -0,0 +1,13 @@ +#ifndef XGBOOST_H +#define XGBOOST_H +#include "PyClassifier.h" + +namespace pywrap { + class XGBoost : public PyClassifier { + public: + XGBoost() : PyClassifier("xgboost", "XGBClassifier") {}; + ~XGBoost() = default; + std::string version(); + }; +} /* namespace pywrap */ +#endif /* XGBOOST_H */ \ No newline at end of file diff --git a/src/main.cc b/src/main.cc index dabec35..a7e1f81 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,6 +7,7 @@ #include #include "STree.h" #include "SVC.h" +#include "RandomForest.h" using namespace std; using namespace torch; @@ -44,25 +45,32 @@ int main(int argc, char* argv[]) { cout << "* Begin." << endl; { - auto [X, y, features, className, states] = loadDataset("wine", false); + auto datasetName = "iris"; + bool class_last = true; + auto [X, y, features, className, states] = loadDataset(datasetName, class_last); + cout << "Dataset: " << datasetName << endl; cout << "X: " << X.sizes() << endl; cout << "y: " << y.sizes() << endl; auto clf = pywrap::STree(); cout << "STree Version: " << clf.version() << endl; - if (true) { - auto svc = pywrap::SVC(); - svc.fit(X, y, features, className, states); - cout << "SVC Score: " << svc.score(X, y) << endl; - } + auto svc = pywrap::SVC(); + svc.fit(X, y, features, className, states); cout << "Graph: " << endl << clf.graph() << endl; clf.fit(X, y, features, className, states); - cout << "STree Score: " << clf.score(X, y) << endl; auto prediction = clf.predict(X); cout << "Prediction: " << endl << "{"; for (int i = 0; i < prediction.size(0); ++i) { cout << prediction[i].item() << ", "; } cout << "}" << endl; + auto rf = pywrap::RandomForest(); + rf.fit(X, y, features, className, states); + auto xg = pywrap::RandomForest(); + xg.fit(X, y, features, className, states); + cout << "STree Score ......: " << clf.score(X, y) << endl; + cout << "RandomForest Score: " << rf.score(X, y) << endl; + cout << "SVC Score ........: " << svc.score(X, y) << endl; + cout << "XGBoost Score ....: " << xg.score(X, y) << endl; } cout << "* End." << endl; } \ No newline at end of file