Fix fit & predict with discretized datasets
This commit is contained in:
Submodule lib/catch2 updated: cd8f97e6c7...4e8d92bf02
2
lib/json
2
lib/json
Submodule lib/json updated: 377c767aa1...8c391e04fe
2
lib/mdlp
2
lib/mdlp
Submodule lib/mdlp updated: 5708dc3de9...236d1b2f8b
@@ -21,10 +21,22 @@ namespace pywrap {
|
||||
Xn = Xn.transpose();
|
||||
return Xn;
|
||||
}
|
||||
np::ndarray tensorInt2numpy(torch::Tensor& X)
|
||||
{
|
||||
int m = X.size(0);
|
||||
int n = X.size(1);
|
||||
auto Xn = np::from_data(X.data_ptr(), np::dtype::get_builtin<int>(), bp::make_tuple(m, n), bp::make_tuple(sizeof(X.dtype()) * 2 * n, sizeof(X.dtype()) * 2), bp::object());
|
||||
Xn = Xn.transpose();
|
||||
//std::cout << "Transposed array:\n" << boost::python::extract<char const*>(boost::python::str(Xn)) << std::endl;
|
||||
return Xn;
|
||||
}
|
||||
std::pair<np::ndarray, np::ndarray> tensors2numpy(torch::Tensor& X, torch::Tensor& y)
|
||||
{
|
||||
int n = X.size(1);
|
||||
auto yn = np::from_data(y.data_ptr(), np::dtype::get_builtin<int32_t>(), bp::make_tuple(n), bp::make_tuple(sizeof(y.dtype()) * 2), bp::object());
|
||||
if (X.dtype() == torch::kInt32) {
|
||||
return { tensorInt2numpy(X), yn };
|
||||
}
|
||||
return { tensor2numpy(X), yn };
|
||||
}
|
||||
std::string PyClassifier::version()
|
||||
@@ -65,8 +77,14 @@ namespace pywrap {
|
||||
torch::Tensor PyClassifier::predict(torch::Tensor& X)
|
||||
{
|
||||
int dimension = X.size(1);
|
||||
auto Xn = tensor2numpy(X);
|
||||
CPyObject Xp = bp::incref(bp::object(Xn).ptr());
|
||||
CPyObject Xp;
|
||||
if (X.dtype() == torch::kInt32) {
|
||||
auto Xn = tensorInt2numpy(X);
|
||||
Xp = bp::incref(bp::object(Xn).ptr());
|
||||
} else {
|
||||
auto Xn = tensor2numpy(X);
|
||||
Xp = bp::incref(bp::object(Xn).ptr());
|
||||
}
|
||||
PyObject* incoming = pyWrap->predict(id, Xp);
|
||||
bp::handle<> handle(incoming);
|
||||
bp::object object(handle);
|
||||
|
@@ -13,7 +13,7 @@
|
||||
#include "pyclfs/ODTE.h"
|
||||
#include "TestUtils.h"
|
||||
|
||||
const std::string ACTUAL_VERSION = "1.0.4";
|
||||
const std::string ACTUAL_VERSION = "1.0.5";
|
||||
|
||||
TEST_CASE("Test Python Classifiers score", "[PyClassifiers]")
|
||||
{
|
||||
@@ -60,19 +60,27 @@ TEST_CASE("Test Python Classifiers score", "[PyClassifiers]")
|
||||
}
|
||||
TEST_CASE("Classifiers features", "[PyClassifiers]")
|
||||
{
|
||||
auto raw = RawDatasets("iris", true);
|
||||
auto raw = RawDatasets("iris", false);
|
||||
auto clf = pywrap::STree();
|
||||
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
||||
REQUIRE(clf.getNumberOfNodes() == 3);
|
||||
REQUIRE(clf.getNumberOfEdges() == 2);
|
||||
REQUIRE(clf.getNumberOfNodes() == 5);
|
||||
REQUIRE(clf.getNumberOfEdges() == 3);
|
||||
}
|
||||
TEST_CASE("Get num features & num edges", "[PyClassifiers]")
|
||||
{
|
||||
auto raw = RawDatasets("iris", true);
|
||||
auto raw = RawDatasets("iris", false);
|
||||
auto clf = pywrap::ODTE();
|
||||
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
||||
REQUIRE(clf.getNumberOfNodes() == 10);
|
||||
REQUIRE(clf.getNumberOfEdges() == 10);
|
||||
REQUIRE(clf.getNumberOfNodes() == 50);
|
||||
REQUIRE(clf.getNumberOfEdges() == 30);
|
||||
}
|
||||
TEST_CASE("Classifier with discretized dataset", "[PyClassifiers]")
|
||||
{
|
||||
auto raw = RawDatasets("iris", true);
|
||||
auto clf = pywrap::SVC();
|
||||
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
||||
auto score = clf.score(raw.Xt, raw.yt);
|
||||
REQUIRE(score == Catch::Approx(0.96667f).epsilon(raw.epsilon));
|
||||
}
|
||||
// TEST_CASE("XGBoost", "[PyClassifiers]")
|
||||
// {
|
||||
|
Reference in New Issue
Block a user