Feat C++ API
A feature engineering automation tool
pybind.cc
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 authors: William La Cava
4 license: GNU/GPL v3
5 */
6 
7 #include <pybind11/pybind11.h>
8 #include <pybind11/eigen.h>
9 #include <pybind11/stl.h>
10 #include <pybind11/iostream.h>
11 // json support
12 #include "pybind11_json/pybind11_json.hpp"
13 #include "nlohmann/json.hpp"
14 
15 // py::dict obj = py::dict("number"_a=1234, "hello"_a="world");
16 // // Automatic py::dict->nl::json conversion
17 // nl::json j = obj;
18 
19 // // Automatic nl::json->py::object conversion
20 // py::object result1 = j;
21 // // Automatic nl::json->py::dict conversion
22 // py::dict result2 = j;
23 
24 #include "feat.h"
25 
26 #define STRINGIFY(x) #x
27 #define MACRO_STRINGIFY(x) STRINGIFY(x)
28 
29 namespace py = pybind11;
30 using namespace FT;
31 namespace nl = nlohmann;
32 using namespace pybind11::literals;
33 
34 PYBIND11_MODULE(_feat, m)
35 {
36  m.doc() = R"pbdoc(
37  Python binding for Feat
38  --------------------------
39 
40  .. autosummary::
41  :toctree: _generate
42 
43  )pbdoc";
44 
45  // #ifdef VERSION_INFO
46  // m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
47  // #else
48  // m.attr("__version__") = "dev";
49  // #endif
50 
51  py::class_<Feat>(m, "cppFeat", py::dynamic_attr())
52  // .def(py::init<>())
53  .def(py::init([]()
54  { Feat est; return est; }))
55  .def_property("stats_", &Feat::get_stats, nullptr)
56  .def_property("pop_size", &Feat::get_pop_size, &Feat::set_pop_size)
57  .def_property("gens", &Feat::get_gens, &Feat::set_gens)
58  .def_property("ml", &Feat::get_ml, &Feat::set_ml)
59  .def_property("classification", &Feat::get_classification, &Feat::set_classification)
60  .def_property("verbosity", &Feat::get_verbosity, &Feat::set_verbosity)
61  .def_property("max_stall", &Feat::get_max_stall, &Feat::set_max_stall)
62  .def_property("sel", &Feat::get_sel, &Feat::set_sel)
63  .def_property("surv", &Feat::get_surv, &Feat::set_surv)
64  .def_property("cross_rate", &Feat::get_cross_rate, &Feat::set_cross_rate)
65  .def_property("root_xo_rate", &Feat::get_root_xo_rate, &Feat::set_root_xo_rate)
66  .def_property("otype", &Feat::get_otype, &Feat::set_otype)
67  .def_property("max_depth", &Feat::get_max_depth, &Feat::set_max_depth)
68  .def_property("max_dim", &Feat::get_max_dim, &Feat::set_max_dim)
69  .def_property("random_state", &Feat::get_random_state, &Feat::set_random_state)
70  .def_property("erc", &Feat::get_erc, &Feat::set_erc)
71  .def_property("objectives", &Feat::get_objectives, &Feat::set_objectives)
72  .def_property("functions", &Feat::get_functions, &Feat::set_functions)
73  .def_property("shuffle", &Feat::get_shuffle, &Feat::set_shuffle)
74  .def_property("split", &Feat::get_split, &Feat::set_split)
75  .def_property("fb", &Feat::get_fb, &Feat::set_fb)
76  .def_property("scorer", &Feat::get_scorer, &Feat::set_scorer)
77  .def_property("feature_names", &Feat::get_feature_names, &Feat::set_feature_names)
78  .def_property("backprop", &Feat::get_backprop, &Feat::set_backprop)
79  .def_property("iters", &Feat::get_iters, &Feat::set_iters)
80  .def_property("lr", &Feat::get_lr, &Feat::set_lr)
81  .def_property("batch_size", &Feat::get_batch_size, &Feat::set_batch_size)
82  .def_property("n_jobs", &Feat::get_n_jobs, &Feat::set_n_jobs)
83  .def_property("hillclimb", &Feat::get_hillclimb, &Feat::set_hillclimb)
84  .def_property("logfile", &Feat::get_logfile, &Feat::set_logfile)
85  .def_property("max_time", &Feat::get_max_time, &Feat::set_max_time)
86  .def_property("residual_xo", &Feat::get_residual_xo, &Feat::set_residual_xo)
87  .def_property("stagewise_xo", &Feat::get_stagewise_xo, &Feat::set_stagewise_xo)
88  .def_property("stagewise_xo_tol", &Feat::get_stagewise_xo_tol,
90  .def_property("softmax_norm", &Feat::get_softmax_norm, &Feat::set_softmax_norm)
91  .def_property("save_pop", &Feat::get_save_pop, &Feat::set_save_pop)
92  .def_property("normalize", &Feat::get_normalize, &Feat::set_normalize)
93  .def_property("val_from_arch", &Feat::get_val_from_arch, &Feat::set_val_from_arch)
94  .def_property("corr_delete_mutate", &Feat::get_corr_delete_mutate,
96  .def_property("simplify", &Feat::get_simplify, &Feat::set_simplify)
97  .def_property("protected_groups",
99  .def_property("tune_initial", &Feat::get_tune_initial, &Feat::set_tune_initial)
100  .def_property("tune_final", &Feat::get_tune_final, &Feat::set_tune_final)
101  .def_property("starting_pop", &Feat::get_starting_pop, &Feat::set_starting_pop)
102  // .def_property("fitted_", &Feat::get_is_fitted, &Feat::set_is_fitted)
103  .def("fit",
104  py::overload_cast<MatrixXf &, VectorXf &>(&Feat::fit),
105  py::call_guard<
106  py::scoped_ostream_redirect,
107  py::scoped_estream_redirect,
108  py::gil_scoped_release>(),
109  "fit from X,y data")
110  .def("fit",
111  py::overload_cast<MatrixXf &, VectorXf &, LongData &>(&Feat::fit),
112  py::call_guard<
113  py::scoped_ostream_redirect,
114  py::scoped_estream_redirect,
115  py::gil_scoped_release>(),
116  "fit from X,y,Z data")
117  .def("transform",
118  py::overload_cast<MatrixXf &>(&Feat::transform),
119  "transform from X data")
120  .def("transform",
121  py::overload_cast<MatrixXf &, LongData &>(&Feat::transform),
122  "transform from X,Z data")
123  .def("predict",
124  py::overload_cast<MatrixXf &>(&Feat::predict),
125  "predict from X data")
126  .def("predict",
127  py::overload_cast<MatrixXf &, LongData &>(&Feat::predict),
128  "predict from X,Z data")
129  .def("predict_proba",
130  py::overload_cast<MatrixXf &>(&Feat::predict_proba),
131  "predict probabilities from X data")
132  .def("predict_proba",
133  py::overload_cast<MatrixXf &, LongData &>(&Feat::predict_proba),
134  "predict probabilities from X data")
135  .def("predict_archive",
136  py::overload_cast<int, MatrixXf &>(&Feat::predict_archive),
137  "predict from individual in archive")
138  .def("predict_archive",
139  py::overload_cast<int, MatrixXf &, LongData &>(&Feat::predict_archive),
140  "predict from individual in archive")
141  .def("predict_proba_archive",
142  py::overload_cast<int, MatrixXf &>(&Feat::predict_proba_archive),
143  "predict from individual in archive")
144  .def("predict_proba_archive",
145  py::overload_cast<int, MatrixXf &, LongData &>(&Feat::predict_proba_archive),
146  "predict from individual in archive")
147  .def("get_archive", &Feat::get_archive, py::arg("front") = false)
148  .def("get_coefs", &Feat::get_coefs)
149  .def("save", &Feat::save)
150  .def("load", &Feat::load)
151  .def("get_representation", &Feat::get_representation)
152  .def("get_n_params", &Feat::get_n_params)
153  .def("get_dim", &Feat::get_dim)
154  .def("get_n_nodes", &Feat::get_n_nodes)
155  .def("get_model", &Feat::get_model, py::arg("sort") = true)
156  .def("get_eqn", &Feat::get_eqn, py::arg("sort") = true)
157  ;
158  // py::add_ostream_redirect(m, "ostream_redirect");
159 }
main class for the Feat learner.
Definition: feat.h:73
void set_backprop(bool bp)
set constant optimization options
Definition: feat.cc:385
int get_dim()
get dimensionality of best
Definition: feat.cc:567
void set_functions(const vector< string > &fns)
sets available functions based on comma-separated list.
Definition: feat.h:163
void set_starting_pop(string sp)
Definition: feat.h:277
void set_normalize(bool in)
Definition: feat.h:280
void set_root_xo_rate(float cross_rate)
set root xo rate in variation
Definition: feat.cc:333
bool get_tune_initial()
Definition: feat.h:289
void set_feature_names(string s)
Definition: feat.h:223
ArrayXXf predict_proba(MatrixXf &X, LongData &Z)
predict probabilities of each class.
Definition: feat.cc:1280
void load(const json &j)
load Feat state from a json string.
Definition: feat.cc:1584
void set_random_state(int random_state)
set dimensionality as multiple of the number of columns
Definition: feat.cc:355
void set_corr_delete_mutate(bool s)
Definition: feat.cc:389
void set_tune_initial(bool in)
Definition: feat.h:290
bool get_normalize()
Definition: feat.h:281
float get_root_xo_rate()
Definition: feat.h:153
string get_model(bool sort=true)
return best model, in tabular form
Definition: feat.cc:527
void set_tune_final(bool in)
Definition: feat.h:293
int get_n_jobs()
Definition: feat.h:250
void set_gens(int gens)
set size of max generations
Definition: feat.cc:298
void set_split(float sp)
set train fraction of dataset
Definition: feat.cc:368
void set_surv(string in)
Definition: feat.h:287
void set_erc(bool erc)
flag to set whether to use variable or constants for terminals
Definition: feat.cc:362
void set_val_from_arch(bool in)
Definition: feat.h:306
void set_classification(bool classification)
set EProblemType for shogun
Definition: feat.cc:304
int get_iters()
Definition: feat.h:240
float get_split()
return fraction of data to use for training
Definition: feat.cc:464
void set_scorer(string s)
set scoring function
Definition: feat.cc:380
int get_max_depth()
return max_depth of programs
Definition: feat.cc:440
void set_fb(float fb)
set feedback
Definition: feat.cc:374
bool get_softmax_norm()
Definition: feat.h:272
void set_max_time(int time)
set max time in seconds for fit method
Definition: feat.cc:406
float get_simplify()
Definition: feat.h:231
json save() const
save and return a json Feat state as string.
Definition: feat.cc:1590
string get_representation()
return best model
Definition: feat.cc:476
ArrayXf get_coefs()
return the coefficients or importance scores of the best model.
Definition: feat.cc:649
int get_n_params()
get number of parameters in best
Definition: feat.cc:564
bool get_stagewise_xo()
Definition: feat.h:265
void set_protected_groups(string pg)
set protected groups for fairness
Definition: feat.cc:410
void set_simplify(float s)
Definition: feat.cc:387
bool get_erc()
return boolean value of erc flag
Definition: feat.cc:452
int get_verbosity()
return current verbosity level set
Definition: feat.cc:437
auto get_objectives()
get objectives for multi-objective search
Definition: feat.h:297
MatrixXf transform(MatrixXf &X)
transform an input matrix using a program.
Definition: feat.cc:1142
char get_otype()
return parameter otype, used to set otypes
Definition: feat.h:158
bool get_classification()
return type of classification flag set
Definition: feat.cc:428
int get_max_stall()
return maximum stall in learning, in generations
Definition: feat.cc:431
VectorXf predict_archive(int id, MatrixXf &X)
predict on unseen data from the whole archive
Definition: feat.cc:1195
nl::json get_stats()
return statistics from the run as a json string
Definition: feat.cc:1566
string get_sel()
Definition: feat.h:283
float get_lr()
Definition: feat.h:243
string get_surv()
Definition: feat.h:286
bool get_corr_delete_mutate()
Definition: feat.h:234
int get_pop_size()
return population size
Definition: feat.cc:419
string get_protected_groups()
Definition: feat.h:301
void set_iters(int iters)
Definition: feat.cc:393
string get_eqn(bool sort=false)
Definition: feat.cc:478
vector< nl::json > get_archive(bool front)
return population as string
Definition: feat.cc:577
void set_pop_size(int pop_size)
set size of population
Definition: feat.cc:295
void set_residual_xo(bool res_xo=true)
use residual crossover
Definition: feat.h:260
void set_n_jobs(unsigned t)
set number of threads
Definition: feat.cc:404
void set_objectives(const vector< string > &obj)
set objectives for multi-objective search
Definition: feat.h:299
int get_max_time()
Definition: feat.h:254
void set_hillclimb(bool hc)
Definition: feat.cc:391
void set_otype(char ot)
set program output type ('f', 'b')
Definition: feat.cc:339
void set_ml(string ml)
set ML algorithm to use
Definition: feat.cc:301
int get_max_dim()
return max dimensionality of programs
Definition: feat.cc:449
void set_stagewise_xo_tol(int tol)
Definition: feat.h:267
void set_lr(float lr)
Definition: feat.cc:395
string get_feature_names()
Definition: feat.h:224
void set_logfile(string s)
set name for files
Definition: feat.cc:377
bool get_residual_xo()
Definition: feat.h:261
int get_gens()
return size of max generations
Definition: feat.cc:422
void set_shuffle(bool sh)
flag to shuffle the input samples for train/test splits
Definition: feat.cc:365
int get_n_nodes()
return the number of nodes in the best model
Definition: feat.cc:574
string get_logfile()
get name
Definition: feat.cc:455
bool get_backprop()
Definition: feat.h:228
bool get_hillclimb()
Definition: feat.h:237
string get_ml()
return ML algorithm string
Definition: feat.cc:425
bool get_tune_final()
Definition: feat.h:292
int get_stagewise_xo_tol()
Definition: feat.h:268
void set_max_dim(unsigned int max_dim)
set maximum dimensionality of programs
Definition: feat.cc:349
void set_sel(string in)
Definition: feat.h:284
void set_max_depth(unsigned int max_depth)
set max depth of programs
Definition: feat.cc:343
void set_softmax_norm(bool sftmx=true)
use softmax
Definition: feat.h:271
vector< string > get_functions()
Definition: feat.h:164
void set_stagewise_xo(bool sem_xo=true)
use stagewise crossover
Definition: feat.h:264
void set_verbosity(int verbosity)
set level of debug info
Definition: feat.cc:310
string get_starting_pop()
Definition: feat.h:278
ArrayXXf predict_proba_archive(int id, MatrixXf &X, LongData &Z)
Definition: feat.cc:1247
void set_cross_rate(float cross_rate)
set cross rate in variation
Definition: feat.cc:326
void set_batch_size(int bs)
Definition: feat.cc:397
int get_save_pop()
Definition: feat.h:275
string get_scorer()
Definition: feat.cc:382
float get_fb()
get feedback setting
Definition: feat.cc:473
bool get_shuffle()
return whether option to shuffle the data is set or not
Definition: feat.cc:461
float get_cross_rate()
return cross rate for variation
Definition: feat.cc:443
VectorXf predict(MatrixXf &X, LongData &Z)
predict on unseen data.
Definition: feat.cc:1184
bool get_val_from_arch()
Definition: feat.h:305
int get_random_state()
Definition: feat.h:181
int get_batch_size()
Definition: feat.h:245
void fit(MatrixXf &X, VectorXf &y)
train a model.
Definition: feat.cc:668
void set_max_stall(int max_stall)
set maximum stall in learning, in generations
Definition: feat.cc:313
void set_save_pop(int pp)
Definition: feat.h:274
main Feat namespace
Definition: data.cc:13
PYBIND11_MODULE(_feat, m)
Definition: pybind.cc:34