Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
Brush::Program< PType > Struct Template Reference

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

#include <program.h>

Public Types

using RetType
 the return type of the tree when calling :func:predict.
 
using TreeType
 the type of output from the tree object
 

Public Member Functions

 Program ()=default
 
 Program (const std::reference_wrapper< SearchSpace > s, const tree< Node > t)
 
Program< PType > copy ()
 
void set_search_space (const std::reference_wrapper< SearchSpace > s)
 
int complexity () const
 count the (recursive) complexity of the program.
 
int linear_complexity () const
 count the linear complexity of the program.
 
int size (bool include_weight=true) const
 count the tree size of the program, including the weights in weighted nodes.
 
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.
 
int depth () const
 count the tree depth of the program. The depth is not influenced by weighted nodes.
 
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.
 
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.
 
Program< PType > & fit (const Dataset &d)
 
Program< PType > & replace_program (const Program< PType > &new_program)
 Replace the current program with a new program, invalidating fitness.
 
Program< PType > & replace_program (const json &j)
 Replace the current program from a JSON representation, invalidating fitness.
 
template<typename R, typename W>
predict_with_weights (const Dataset &d, const W **weights)
 
auto predict_with_weights (const Dataset &d, const ArrayXf &weights)
 
template<typename R = RetType>
requires (is_same_v<R, TreeType>)
TreeType predict (const Dataset &d)
 the standard predict function. Returns the output of the Tree directly.
 
template<typename R = RetType>
requires (is_same_v<R, ArrayXb>)
ArrayXb predict (const Dataset &d)
 Specialized predict function for binary classification.
 
template<typename R = RetType>
requires (is_same_v<R, ArrayXi>)
ArrayXi predict (const Dataset &d)
 Specialized predict function for multiclass classification.
 
template<PT P = PType>
requires ((P == PT::BinaryClassifier) || (P == PT::MulticlassClassifier))
TreeType predict_proba (const Dataset &d)
 
Program< PType > & fit (const Ref< const ArrayXXf > &X, const Ref< const ArrayXf > &y)
 Convenience function to call fit directly from X,y data.
 
RetType predict (const Ref< const ArrayXXf > &X)
 Convenience function to call predict directly from X data.
 
template<PT P = PType>
requires ((P == PT::BinaryClassifier) || (P == PT::MulticlassClassifier))
TreeType predict_proba (const Ref< const ArrayXXf > &X)
 Predict probabilities from X.
 
void update_weights (const Dataset &d)
 Updates the program's weights using non-linear least squares.
 
int get_n_weights () const
 returns the number of weights in the program.
 
ArrayXf get_weights ()
 Get the weights of the tree as an array.
 
void set_weights (const ArrayXf &weights)
 Set the weights in the tree from an array of weights.
 
void lock_nodes (int end_depth=0, bool keep_leaves_unlocked=true, bool keep_current_weights=false)
 Iterates over the program, locking the nodes until it reaches a certain depth. If the node is a SplitBest and leaves are kept, then the split feature is fixed.
 
string get_model (string fmt="compact", bool pretty=false) const
 Get the model as a string.
 
string get_dot_model (string extras="") const
 Get the model as a dot object.
 
vector< Nodelinearize () const
 turns program tree into a linear program.
 

Public Attributes

bool is_fitted_ = false
 whether fit has been called
 
tree< NodeTree
 fitness
 
std::optional< std::reference_wrapper< SearchSpace > > SSref
 reference to search space
 

Static Public Attributes

static constexpr PT program_type = PType
 an enum storing the program type.
 

Detailed Description

template<PT PType>
struct Brush::Program< PType >

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

Template Parameters
PTypeone of the ProgramType enum values.

Definition at line 49 of file program.h.

Member Typedef Documentation

◆ RetType

template<PT PType>
using Brush::Program< PType >::RetType
Initial value:
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
>>>>
Eigen::Array< bool, Eigen::Dynamic, 1 > ArrayXb
Definition types.h:39
Eigen::Array< int, Eigen::Dynamic, 1 > ArrayXi
Definition types.h:40

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

Definition at line 55 of file program.h.

◆ TreeType

template<PT PType>
using Brush::Program< PType >::TreeType
Initial value:
std::conditional_t<PType == PT::BinaryClassifier, ArrayXf,
std::conditional_t<PType == PT::MulticlassClassifier, ArrayXXf,
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.
Definition program.h:55

the type of output from the tree object

Definition at line 62 of file program.h.

Constructor & Destructor Documentation

◆ Program() [1/2]

template<PT PType>
Brush::Program< PType >::Program ( )
default
Here is the caller graph for this function:

◆ Program() [2/2]

template<PT PType>
Brush::Program< PType >::Program ( const std::reference_wrapper< SearchSpace > s,
const tree< Node > t )
inline

Definition at line 79 of file program.h.

Member Function Documentation

◆ complexity()

template<PT PType>
int Brush::Program< PType >::complexity ( ) const
inline

count the (recursive) complexity of the program.

Returns
int complexity.

Definition at line 94 of file program.h.

◆ copy()

template<PT PType>
Program< PType > Brush::Program< PType >::copy ( )
inline

Definition at line 85 of file program.h.

◆ depth()

template<PT PType>
int Brush::Program< PType >::depth ( ) const
inline

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

Returns
int tree depth.

Definition at line 129 of file program.h.

Here is the caller graph for this function:

◆ depth_at()

template<PT PType>
int Brush::Program< PType >::depth_at ( Iter & top) const
inline

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
toproot node of the subtree.
Returns
int tree depth.

Definition at line 139 of file program.h.

◆ depth_to_reach()

template<PT PType>
int Brush::Program< PType >::depth_to_reach ( Iter & top) const
inline

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
toproot node of the subtree.
Returns
int tree depth.

Definition at line 147 of file program.h.

Here is the caller graph for this function:

◆ fit() [1/2]

template<PT PType>
Program< PType > & Brush::Program< PType >::fit ( const Dataset & d)
inline

Definition at line 151 of file program.h.

Here is the caller graph for this function:

◆ fit() [2/2]

template<PT PType>
Program< PType > & Brush::Program< PType >::fit ( const Ref< const ArrayXXf > & X,
const Ref< const ArrayXf > & y )
inline

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

Parameters
X: Input features
y: Labels
Returns
: reference to program

Definition at line 263 of file program.h.

◆ get_dot_model()

template<PT PType>
string Brush::Program< PType >::get_dot_model ( string extras = "") const
inline

Get the model as a dot object.

Parameters
extrasextra code passed to the beginning of the dot code.
Returns
string the model in dot language.

Definition at line 491 of file program.h.

◆ get_model()

template<PT PType>
string Brush::Program< PType >::get_model ( string fmt = "compact",
bool pretty = false ) const
inline

Get the model as a string.

Parameters
fmtone of "compact", "tree", or "dot". Default "compact".
  • 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
prettycurrently unused.
Returns
string the model in string form.

Definition at line 475 of file program.h.

◆ get_n_weights()

template<PT PType>
int Brush::Program< PType >::get_n_weights ( ) const
inline

returns the number of weights in the program.

Definition at line 301 of file program.h.

Here is the caller graph for this function:

◆ get_weights()

template<PT PType>
ArrayXf Brush::Program< PType >::get_weights ( )
inline

Get the weights of the tree as an array.

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

Definition at line 331 of file program.h.

◆ linear_complexity()

template<PT PType>
int Brush::Program< PType >::linear_complexity ( ) const
inline

count the linear complexity of the program.

Returns
int complexity.

Definition at line 102 of file program.h.

◆ linearize()

template<PT PType>
vector< Node > Brush::Program< PType >::linearize ( ) const
inline

turns program tree into a linear program.

Returns
a vector of nodes encoding the program in reverse polish notation

Definition at line 659 of file program.h.

◆ lock_nodes()

template<PT PType>
void Brush::Program< PType >::lock_nodes ( int end_depth = 0,
bool keep_leaves_unlocked = true,
bool keep_current_weights = false )
inline

Iterates over the program, locking the nodes until it reaches a certain depth. If the node is a SplitBest and leaves are kept, then the split feature is fixed.

Parameters
end_depththe depth to stop locking nodes. Default 0.
keep_leaves_unlockedwhether to skip leaves and leave them unlocked.
keep_current_weightswhether to keep current weights at the spot they appear. Default true.

Definition at line 394 of file program.h.

◆ predict() [1/4]

template<PT PType>
template<typename R = RetType>
requires (is_same_v<R, TreeType>)
TreeType Brush::Program< PType >::predict ( const Dataset & d)
inline

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

Template Parameters
Rreturn type, default
Parameters
ddataset
Returns

Definition at line 215 of file program.h.

Here is the caller graph for this function:

◆ predict() [2/4]

template<PT PType>
template<typename R = RetType>
requires (is_same_v<R, ArrayXb>)
ArrayXb Brush::Program< PType >::predict ( const Dataset & d)
inline

Specialized predict function for binary classification.

Template Parameters
Rreturn type, typically left blank
Parameters
d: data
Returns
out: binary labels

Definition at line 228 of file program.h.

◆ predict() [3/4]

template<PT PType>
template<typename R = RetType>
requires (is_same_v<R, ArrayXi>)
ArrayXi Brush::Program< PType >::predict ( const Dataset & d)
inline

Specialized predict function for multiclass classification.

Template Parameters
Rreturn type, typically left blank
Parameters
d: data
Returns
out: integer labels

Definition at line 241 of file program.h.

◆ predict() [4/4]

template<PT PType>
RetType Brush::Program< PType >::predict ( const Ref< const ArrayXXf > & X)
inline

Convenience function to call predict directly from X data.

Parameters
X: Input features
Returns
: predictions

Definition at line 272 of file program.h.

◆ predict_proba() [1/2]

template<PT PType>
template<PT P = PType>
requires ((P == PT::BinaryClassifier) || (P == PT::MulticlassClassifier))
TreeType Brush::Program< PType >::predict_proba ( const Dataset & d)
inline

Definition at line 254 of file program.h.

Here is the caller graph for this function:

◆ predict_proba() [2/2]

template<PT PType>
template<PT P = PType>
requires ((P == PT::BinaryClassifier) || (P == PT::MulticlassClassifier))
TreeType Brush::Program< PType >::predict_proba ( const Ref< const ArrayXXf > & X)
inline

Predict probabilities from X.

Requires a BinaryClassifier or MulticlassClassifier.

Template Parameters
Pparameter for type checking, typically left blank.

Definition at line 287 of file program.h.

◆ predict_with_weights() [1/2]

template<PT PType>
auto Brush::Program< PType >::predict_with_weights ( const Dataset & d,
const ArrayXf & weights )
inline

Definition at line 200 of file program.h.

◆ predict_with_weights() [2/2]

template<PT PType>
template<typename R, typename W>
R Brush::Program< PType >::predict_with_weights ( const Dataset & d,
const W ** weights )
inline

Definition at line 192 of file program.h.

Here is the caller graph for this function:

◆ replace_program() [1/2]

template<PT PType>
Program< PType > & Brush::Program< PType >::replace_program ( const json & j)
inline

Replace the current program from a JSON representation, invalidating fitness.

Parameters
jJSON object containing the serialized program
Returns
reference to this program

Definition at line 185 of file program.h.

◆ replace_program() [2/2]

template<PT PType>
Program< PType > & Brush::Program< PType >::replace_program ( const Program< PType > & new_program)
inline

Replace the current program with a new program, invalidating fitness.

Parameters
new_programThe new program to replace the current one with
Returns
reference to this program

Definition at line 166 of file program.h.

Here is the caller graph for this function:

◆ set_search_space()

template<PT PType>
void Brush::Program< PType >::set_search_space ( const std::reference_wrapper< SearchSpace > s)
inline

Definition at line 87 of file program.h.

Here is the caller graph for this function:

◆ set_weights()

template<PT PType>
void Brush::Program< PType >::set_weights ( const ArrayXf & weights)
inline

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

Parameters
weightsan array of weights. The number of the weights in the tree must match the length of weights.

Definition at line 359 of file program.h.

◆ size()

template<PT PType>
int Brush::Program< PType >::size ( bool include_weight = true) const
inline

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

Parameters
include_weightwhether to include the node's weight in the count.
Returns
int number of nodes.

Definition at line 111 of file program.h.

Here is the caller graph for this function:

◆ size_at()

template<PT PType>
int Brush::Program< PType >::size_at ( Iter & top,
bool include_weight = true ) const
inline

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

Parameters
toproot node of the subtree.
include_weightwhether to include the node's weight in the count.
Returns
int number of nodes.

Definition at line 122 of file program.h.

Here is the caller graph for this function:

◆ update_weights()

template<ProgramType PType>
void Brush::Program< PType >::update_weights ( const Dataset & d)

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

Parameters
dthe dataset

Definition at line 675 of file program.h.

Here is the caller graph for this function:

Member Data Documentation

◆ is_fitted_

template<PT PType>
bool Brush::Program< PType >::is_fitted_ = false

whether fit has been called

Definition at line 67 of file program.h.

◆ program_type

template<PT PType>
PT Brush::Program< PType >::program_type = PType
staticconstexpr

an enum storing the program type.

Definition at line 52 of file program.h.

◆ SSref

template<PT PType>
std::optional<std::reference_wrapper<SearchSpace> > Brush::Program< PType >::SSref

reference to search space

Definition at line 76 of file program.h.

◆ Tree

template<PT PType>
tree<Node> Brush::Program< PType >::Tree

fitness

the underlying tree

Definition at line 73 of file program.h.


The documentation for this struct was generated from the following file: