Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
bind_individuals.h
Go to the documentation of this file.
1#include "module.h"
2
3#include "../ind/individual.h"
4
5namespace nl = nlohmann;
6namespace br = Brush;
7
12
13using stream_redirect = py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>;
14
15template <br::ProgramType PT>
16void bind_individual(py::module& m, string name)
17{
18 using Class = br::Pop::Individual<PT>;
19
20 using RetType = std::conditional_t<
21 std::is_same_v<Class,Reg>, ArrayXf,
22 std::conditional_t<std::is_same_v<Class,Cls>, ArrayXb,
23 std::conditional_t<std::is_same_v<Class,MCls>, ArrayXi, ArrayXXf>>>;
24
25 py::class_<Class> ind(m, name.data() );
26 ind.def(py::init<>())
27 .def(py::init([](br::Program<PT>& prg){ Class i(prg);
28 return i; })
29 )
30 .def(py::init([](const json& j){ br::Program<PT> prg = j;
31 Class i(prg);
32 return i; })
33 )
34 .def("init", &Class::init)
35 .def_property("objectives", &Class::get_objectives, &Class::set_objectives)
36 .def_property_readonly("program", &Class::get_program)
37 .def_property_readonly("fitness", &Class::get_fitness)
38 .def("get_model", &Class::get_model,
39 py::arg("fmt") = "compact",
40 py::arg("pretty") = false)
41 .def("get_dot_model", &Class::get_dot_model,
42 py::arg("extras") = "")
43 .def("fit",
44 static_cast<Class &(Class::*)(const Dataset &d)>(&Class::fit),
45 "fit from Dataset object")
46 .def("fit",
47 static_cast<Class &(Class::*)(const Ref<const ArrayXXf> &X, const Ref<const ArrayXf> &y)>(&Class::fit),
48 "fit from X,y data")
49 .def("predict",
50 static_cast<RetType (Class::*)(const Dataset &d)>(&Class::predict),
51 "predict from Dataset object")
52 .def("predict",
53 static_cast<RetType (Class::*)(const Ref<const ArrayXXf> &X)>(&Class::predict),
54 "predict from X data")
55 .def(py::pickle(
56 [](const Class &p) { // __getstate__
57 /* Return a tuple that fully encodes the state of the object */
58 // return py::make_tuple(p.value(), p.extra());
59 nl::json j = p;
60 return j;
61 },
62 [](nl::json j) { // __setstate__
63 Class p = j;
64 return p;
65 }
66 )
67 )
68 ;
69
70 if constexpr (std::is_same_v<Class,Cls>)
71 {
72 ind.def("predict_proba",
73 static_cast<ArrayXf (Class::*)(const Dataset &d)>(&Class::predict_proba),
74 "predict from Dataset object")
75 .def("predict_proba",
76 static_cast<ArrayXf (Class::*)(const Ref<const ArrayXXf> &X)>(&Class::predict_proba),
77 "predict from X data")
78 ;
79 }
80
81}
void bind_engine(py::module &m, string name)
py::call_guard< py::scoped_ostream_redirect, py::scoped_estream_redirect > stream_redirect
void bind_individual(py::module &m, string name)
holds variable type data.
Definition data.h:51
The Engine class represents the core engine of the brush library.
Definition engine.h:43
< nsga2 selection operator for getting the front
Definition data.cpp:12
Eigen::Array< bool, Eigen::Dynamic, 1 > ArrayXb
Definition types.h:39
Pop::Individual< PT::MulticlassClassifier > MulticlassClassifierIndividual
Definition types.h:90
Eigen::Array< int, Eigen::Dynamic, 1 > ArrayXi
Definition types.h:40
Pop::Individual< PT::Representer > RepresenterIndividual
Definition types.h:91
Pop::Individual< PT::BinaryClassifier > ClassifierIndividual
Definition types.h:89
Pop::Individual< PT::Regressor > RegressorIndividual
Definition types.h:88
An individual program, a.k.a. model.
Definition program.h:50