Program#

template<PT PType>
struct Program#

An individual program, a.k.a. model.

Template Parameters:

PType – one of the ProgramType enum values.

Public Types

using RetType = 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>>>>#

the return type of the tree when calling :func:predict.

using TreeType = std::conditional_t<PType == PT::BinaryClassifier, ArrayXf, std::conditional_t<PType == PT::MulticlassClassifier, ArrayXXf, RetType>>#

the type of output from the tree object

Public Functions

Program() = default#
inline Program(const std::reference_wrapper<SearchSpace> s, const tree<Node> t)#
inline Program<PType> copy()#
inline void set_search_space(const std::reference_wrapper<SearchSpace> s)#
inline int complexity() const#

count the complexity of the program.

Returns:

int complexity.

inline int size(bool include_weight = true) const#

count the tree size of the program, including the weights in weighted nodes.

Parameters:

include_weight – whether to include the node’s weight in the count.

Returns:

int number of nodes.

inline int size_at(Iter &top, bool include_weight = true) const#

count the size of a given subtree, optionally including the weights in weighted nodes. This function is not exposed to the python wrapper.

Parameters:
  • top – root node of the subtree.

  • include_weight – whether to include the node’s weight in the count.

Returns:

int number of nodes.

inline int depth() const#

count the tree depth of the program. The depth is not influenced by weighted nodes.

Returns:

int tree depth.

inline int depth_at(Iter &top) const#

count the depth of a given subtree. The depth is not influenced by weighted nodes. This function is not exposed to the python wrapper.

Parameters:

top – root node of the subtree.

Returns:

int tree depth.

inline int depth_to_reach(Iter &top) const#

count the depth until reaching the given subtree. The depth is not influenced by weighted nodes. This function is not exposed to the python wrapper.

Parameters:

top – root node of the subtree.

Returns:

int tree depth.

inline Program<PType> &fit(const Dataset &d)#
template<typename R, typename W>
inline R predict_with_weights(const Dataset &d, const W **weights)#
inline auto predict_with_weights(const Dataset &d, const ArrayXf &weights)#
template<typename R = RetType>
inline TreeType predict(const Dataset &d)#

the standard predict function. Returns the output of the Tree directly.

Template Parameters:

R – return type, default

Parameters:

d – dataset

Returns:

template<typename R = RetType>
inline ArrayXb predict(const Dataset &d)#

Specialized predict function for binary classification.

Template Parameters:

R – return type, typically left blank

Parameters:

d – : data

Returns:

out: binary labels

template<typename R = RetType>
inline ArrayXi predict(const Dataset &d)#

Specialized predict function for multiclass classification.

Template Parameters:

R – return type, typically left blank

Parameters:

d – : data

Returns:

out: integer labels

template<PT P = PType>
inline TreeType predict_proba(const Dataset &d)#
inline Program<PType> &fit(const Ref<const ArrayXXf> &X, const Ref<const ArrayXf> &y)#

Convenience function to call fit directly from X,y data.

Parameters:
  • X – : Input features

  • y – : Labels

Returns:

: reference to program

inline RetType predict(const Ref<const ArrayXXf> &X)#

Convenience function to call predict directly from X data.

Parameters:

X – : Input features

Returns:

: predictions

template<PT P = PType>
inline TreeType predict_proba(const Ref<const ArrayXXf> &X)#

Predict probabilities from X.

Requires a BinaryClassifier or MulticlassClassifier.

Template Parameters:

P – parameter for type checking, typically left blank.

void update_weights(const Dataset &d)#

Updates the program’s weights using non-linear least squares.

Parameters:

d – the dataset

inline int get_n_weights() const#

returns the number of weights in the program.

inline ArrayXf get_weights()#

Get the weights of the tree as an array.

Returns:

ArrayXf of weights in the program, encoded in post-fix order.

inline void set_weights(const ArrayXf &weights)#

Set the weights in the tree from an array of weights.

Parameters:

weights – an array of weights. The number of the weights in the tree must match the length of weights.

inline string get_model(string fmt = "compact", bool pretty = false) const#

Get the model as a string.

  • compact : the program as an equation.

  • tree : the program as a (small batch, artisinal) tree.

  • dot : the program in the dot language for downstream visualization.

Parameters:
  • fmt – one of “compact”, “tree”, or “dot”. Default “compact”.

  • pretty – currently unused.

Returns:

string the model in string form.

inline string get_dot_model(string extras = "") const#

Get the model as a dot object.

Parameters:

extras – extra code passed to the beginning of the dot code.

Returns:

string the model in dot language.

inline vector<Node> linearize() const#

turns program tree into a linear program.

Returns:

a vector of nodes encoding the program in reverse polish notation

Public Members

bool is_fitted_#

whether fit has been called

tree<Node> Tree#

fitness

the underlying tree

std::optional<std::reference_wrapper<SearchSpace>> SSref#

reference to search space

Public Static Attributes

static constexpr PT program_type = PType#

an enum storing the program type.