37typedef tree<Node>::pre_order_iterator
Iter;
38typedef tree<Node>::post_order_iterator
PostIter;
75 std::optional<std::reference_wrapper<SearchSpace>>
SSref;
81 SSref = std::optional<std::reference_wrapper<SearchSpace>>{
s};
88 SSref = std::optional<std::reference_wrapper<SearchSpace>>{
s};
96 return head.node->get_complexity();
123 return 1+
Tree.max_depth();
145 this->is_fitted_ =
true;
151 template <
typename R,
typename W>
157 return Tree.begin().node->predict<
R>(
d, weights);
162 float const *
wptr = weights.data();
174 template <
typename R = RetType>
187 template <
typename R = RetType>
199 template <
typename R = RetType>
210 template <PT P = PType>
243 template <PT P = PType>
265 const auto&
node =
i.node->data;
266 if (
node.get_is_weighted())
283 const auto&
node =
t.node->data;
284 if (
node.get_is_weighted())
308 auto&
node =
i.node->data;
309 if (
node.get_is_weighted())
348 string out =
"digraph G {\n";
352 auto get_id = [](
const auto&
n){
354 return n->data.get_name(
false);
356 return fmt::format(
"{}",fmt::ptr(
n)).substr(2);
363 const auto& parent =
iter.node;
366 string parent_id =
get_id(parent);
376 if (
i==0 && parent->data.get_is_weighted())
378 out +=
"y [shape=box];\n";
379 out += fmt::format(
"y -> \"{}\" [label=\"{:.2f}\"];\n",
391 node_label = fmt::format(
"{}>{:.2f}?", parent->data.get_feature(), parent->data.W);
396 out += fmt::format(
"\"{}\" [label=\"{}\"];\n", parent_id,
node_label);
399 auto kid =
iter.node->first_child;
400 for (
int j = 0;
j <
iter.number_of_children(); ++
j)
411 if (
kid->data.get_is_weighted()
419 tail_label = fmt::format(
">{:.2f}",parent->data.W);
439 out += fmt::format(
"\"{}\" -> \"{}\" [headlabel=\"{}\",taillabel=\"{}\"];\n",
447 out += fmt::format(
"\"{}\" -> \"{}\" [label=\"{}\"];\n",
459 out += fmt::format(
"\"{}\" -> \"{}\" [label=\"\"];\n",
465 out += fmt::format(
"\"{}\" [label=\"{}\"];\n",
494template<ProgramType PType>
501 WO.update((*
this),
d);
508template<ProgramType PType>
511 j = json{{
"Tree",
p.Tree}, {
"is_fitted_",
p.is_fitted_}};
514template<ProgramType PType>
517 j.at(
"Tree").get_to(
p.Tree);
518 j.at(
"is_fitted_").get_to(
p.is_fitted_);
void bind_engine(py::module &m, string name)
holds variable type data.
#define HANDLE_ERROR_THROW(err)
< nsga2 selection operator for getting the front
Eigen::Array< bool, Eigen::Dynamic, 1 > ArrayXb
void from_json(const json &j, Fitness &f)
tree< Node >::pre_order_iterator Iter
tree< Node >::post_order_iterator PostIter
void to_json(json &j, const Fitness &f)
Eigen::Array< int, Eigen::Dynamic, 1 > ArrayXi
An individual program, a.k.a. model.
Program(const std::reference_wrapper< SearchSpace > s, const tree< Node > t)
TreeType predict_proba(const Dataset &d)
void update_weights(const Dataset &d)
Updates the program's weights using non-linear least squares.
typename std::conditional_t< PType==PT::Regressor, ArrayXf, std::conditional_t< PType==PT::BinaryClassifier, ArrayXb, std::conditional_t< PType==PT::MulticlassClassifier, ArrayXi, std::conditional_t< PType==PT::Representer, ArrayXXf, ArrayXf > > > > RetType
the return type of the tree when calling :func:predict.
std::optional< std::reference_wrapper< SearchSpace > > SSref
reference to search space
RetType predict(const Ref< const ArrayXXf > &X)
Convenience function to call predict directly from X data.
void set_weights(const ArrayXf &weights)
Set the weights in the tree from an array of weights.
bool is_fitted_
whether fit has been called
TreeType predict(const Dataset &d)
the standard predict function. Returns the output of the Tree directly.
static constexpr PT program_type
an enum storing the program type.
R predict_with_weights(const Dataset &d, const W **weights)
std::conditional_t< PType==PT::BinaryClassifier, ArrayXf, std::conditional_t< PType==PT::MulticlassClassifier, ArrayXXf, RetType > > TreeType
the type of output from the tree object
int complexity() const
count the complexity of the program.
int size_at(Iter &top, bool include_weight=true) const
count the size of a given subtree, optionally including the weights in weighted nodes....
ArrayXf get_weights()
Get the weights of the tree as an array.
Program< PType > & fit(const Dataset &d)
int depth() const
count the tree depth of the program. The depth is not influenced by weighted nodes.
auto predict_with_weights(const Dataset &d, const ArrayXf &weights)
int get_n_weights() const
returns the number of weights in the program.
int depth_at(Iter &top) const
count the depth of a given subtree. The depth is not influenced by weighted nodes....
vector< Node > linearize() const
turns program tree into a linear program.
ArrayXi predict(const Dataset &d)
Specialized predict function for multiclass classification.
void set_search_space(const std::reference_wrapper< SearchSpace > s)
ArrayXb predict(const Dataset &d)
Specialized predict function for binary classification.
string get_dot_model(string extras="") const
Get the model as a dot object.
string get_model(string fmt="compact", bool pretty=false) const
Get the model as a string.
TreeType predict_proba(const Ref< const ArrayXXf > &X)
Predict probabilities from X.
int depth_to_reach(Iter &top) const
count the depth until reaching the given subtree. The depth is not influenced by weighted nodes....
Program< PType > & fit(const Ref< const ArrayXXf > &X, const Ref< const ArrayXf > &y)
Convenience function to call fit directly from X,y data.
int size(bool include_weight=true) const
count the tree size of the program, including the weights in weighted nodes.
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...