treelite
Public Member Functions | Public Attributes | Friends | List of all members
treelite::Tree< ThresholdType, LeafOutputType > Class Template Reference

in-memory representation of a decision tree More...

#include <tree.h>

Collaboration diagram for treelite::Tree< ThresholdType, LeafOutputType >:
Collaboration graph

Public Member Functions

 Tree ()=default
 
 ~Tree ()=default
 
 Tree (Tree const &)=delete
 
Treeoperator= (Tree const &)=delete
 
 Tree (Tree &&) noexcept=default
 
Treeoperator= (Tree &&) noexcept=default
 
Tree< ThresholdType, LeafOutputType > Clone () const
 
void Init ()
 Initialize the tree with a single root node. More...
 
int AllocNode ()
 Allocate a new node and return the node's ID. More...
 
int LeftChild (int nid) const
 Index of the node's left child. More...
 
int RightChild (int nid) const
 Index of the node's right child. More...
 
int DefaultChild (int nid) const
 Index of the node's "default" child, used when feature is missing. More...
 
std::int32_t SplitIndex (int nid) const
 Feature index of the node's split condition. More...
 
bool DefaultLeft (int nid) const
 Whether to use the left child node, when the feature in the split condition is missing. More...
 
bool IsLeaf (int nid) const
 Whether the node is leaf node. More...
 
LeafOutputType LeafValue (int nid) const
 Get leaf value of the leaf node. More...
 
std::vector< LeafOutputType > LeafVector (int nid) const
 get leaf vector of the leaf node; useful for multi-class random forest classifier More...
 
bool HasLeafVector (int nid) const
 Tests whether the leaf node has a non-empty leaf vector. More...
 
ThresholdType Threshold (int nid) const
 Get threshold of the node. More...
 
Operator ComparisonOp (int nid) const
 Get comparison operator. More...
 
std::vector< std::uint32_t > CategoryList (int nid) const
 Get list of all categories belonging to the left/right child node. See the category_list_right_child_ field of each test node to determine whether this list represents the right child node or the left child node. Categories are integers ranging from 0 to (n-1), where n is the number of categories in that particular feature. This list is assumed to be in ascending order. More...
 
TreeNodeType NodeType (int nid) const
 Get the type of a node. More...
 
bool HasDataCount (int nid) const
 Test whether this node has data count. More...
 
std::uint64_t DataCount (int nid) const
 Get data count. More...
 
bool HasSumHess (int nid) const
 Test whether this node has hessian sum. More...
 
double SumHess (int nid) const
 Get hessian sum. More...
 
bool HasGain (int nid) const
 Test whether this node has gain value. More...
 
double Gain (int nid) const
 Get gain value. More...
 
bool CategoryListRightChild (int nid) const
 Test whether the list given by CategoryList(nid) is associated with the right child node or the left child node. More...
 
bool HasCategoricalSplit () const
 Query whether this tree contains any categorical splits. More...
 
void SetChildren (int nid, int left_child, int right_child)
 Identify two child nodes of the node. More...
 
void SetNumericalTest (int nid, std::int32_t split_index, ThresholdType threshold, bool default_left, Operator cmp)
 Create a numerical test. More...
 
void SetCategoricalTest (int nid, std::int32_t split_index, bool default_left, std::vector< std::uint32_t > const &category_list, bool category_list_right_child)
 Create a categorical test. More...
 
void SetLeaf (int nid, LeafOutputType value)
 Set the leaf value of the node. More...
 
void SetLeafVector (int nid, std::vector< LeafOutputType > const &leaf_vector)
 Set the leaf vector of the node; useful for multi-class random forest classifier. More...
 
void SetSumHess (int nid, double sum_hess)
 Set the hessian sum of the node. More...
 
void SetDataCount (int nid, std::uint64_t data_count)
 Set the data count of the node. More...
 
void SetGain (int nid, double gain)
 Set the gain value of the node. More...
 

Public Attributes

std::int32_t num_nodes {0}
 Number of nodes. More...
 

Friends

template<typename MixIn >
class detail::serializer::Serializer
 
template<typename MixIn >
class detail::serializer::Deserializer
 
template<typename WriterType , typename X , typename Y >
void DumpTreeAsJSON (WriterType &writer, Tree< X, Y > const &tree)
 
template<typename X , typename Y >
PyBufferFrame detail::field_accessor::GetTreeFieldImpl (ModelPreset< X, Y > &, std::uint64_t, std::string const &)
 
template<typename X , typename Y >
void detail::field_accessor::SetTreeFieldImpl (ModelPreset< X, Y > &, std::uint64_t, std::string const &, PyBufferFrame)
 

Detailed Description

template<typename ThresholdType, typename LeafOutputType>
class treelite::Tree< ThresholdType, LeafOutputType >

in-memory representation of a decision tree

Constructor & Destructor Documentation

◆ Tree() [1/3]

template<typename ThresholdType , typename LeafOutputType >
treelite::Tree< ThresholdType, LeafOutputType >::Tree ( )
default

◆ ~Tree()

template<typename ThresholdType , typename LeafOutputType >
treelite::Tree< ThresholdType, LeafOutputType >::~Tree ( )
default

◆ Tree() [2/3]

template<typename ThresholdType , typename LeafOutputType >
treelite::Tree< ThresholdType, LeafOutputType >::Tree ( Tree< ThresholdType, LeafOutputType > const &  )
delete

◆ Tree() [3/3]

template<typename ThresholdType , typename LeafOutputType >
treelite::Tree< ThresholdType, LeafOutputType >::Tree ( Tree< ThresholdType, LeafOutputType > &&  )
defaultnoexcept

Member Function Documentation

◆ AllocNode()

template<typename ThresholdType , typename LeafOutputType >
int treelite::Tree< ThresholdType, LeafOutputType >::AllocNode
inline

Allocate a new node and return the node's ID.

◆ CategoryList()

template<typename ThresholdType , typename LeafOutputType >
std::vector<std::uint32_t> treelite::Tree< ThresholdType, LeafOutputType >::CategoryList ( int  nid) const
inline

Get list of all categories belonging to the left/right child node. See the category_list_right_child_ field of each test node to determine whether this list represents the right child node or the left child node. Categories are integers ranging from 0 to (n-1), where n is the number of categories in that particular feature. This list is assumed to be in ascending order.

Parameters
nidID of node being queried

◆ CategoryListRightChild()

template<typename ThresholdType , typename LeafOutputType >
bool treelite::Tree< ThresholdType, LeafOutputType >::CategoryListRightChild ( int  nid) const
inline

Test whether the list given by CategoryList(nid) is associated with the right child node or the left child node.

Parameters
nidID of node being queried

◆ Clone()

template<typename ThresholdType , typename LeafOutputType >
Tree< ThresholdType, LeafOutputType > treelite::Tree< ThresholdType, LeafOutputType >::Clone
inline

◆ ComparisonOp()

template<typename ThresholdType , typename LeafOutputType >
Operator treelite::Tree< ThresholdType, LeafOutputType >::ComparisonOp ( int  nid) const
inline

Get comparison operator.

Parameters
nidID of node being queried

◆ DataCount()

template<typename ThresholdType , typename LeafOutputType >
std::uint64_t treelite::Tree< ThresholdType, LeafOutputType >::DataCount ( int  nid) const
inline

Get data count.

Parameters
nidID of node being queried

◆ DefaultChild()

template<typename ThresholdType , typename LeafOutputType >
int treelite::Tree< ThresholdType, LeafOutputType >::DefaultChild ( int  nid) const
inline

Index of the node's "default" child, used when feature is missing.

Parameters
nidID of node being queried

◆ DefaultLeft()

template<typename ThresholdType , typename LeafOutputType >
bool treelite::Tree< ThresholdType, LeafOutputType >::DefaultLeft ( int  nid) const
inline

Whether to use the left child node, when the feature in the split condition is missing.

Parameters
nidID of node being queried

◆ Gain()

template<typename ThresholdType , typename LeafOutputType >
double treelite::Tree< ThresholdType, LeafOutputType >::Gain ( int  nid) const
inline

Get gain value.

Parameters
nidID of node being queried

◆ HasCategoricalSplit()

template<typename ThresholdType , typename LeafOutputType >
bool treelite::Tree< ThresholdType, LeafOutputType >::HasCategoricalSplit ( ) const
inline

Query whether this tree contains any categorical splits.

◆ HasDataCount()

template<typename ThresholdType , typename LeafOutputType >
bool treelite::Tree< ThresholdType, LeafOutputType >::HasDataCount ( int  nid) const
inline

Test whether this node has data count.

Parameters
nidID of node being queried

◆ HasGain()

template<typename ThresholdType , typename LeafOutputType >
bool treelite::Tree< ThresholdType, LeafOutputType >::HasGain ( int  nid) const
inline

Test whether this node has gain value.

Parameters
nidID of node being queried

◆ HasLeafVector()

template<typename ThresholdType , typename LeafOutputType >
bool treelite::Tree< ThresholdType, LeafOutputType >::HasLeafVector ( int  nid) const
inline

Tests whether the leaf node has a non-empty leaf vector.

Parameters
nidID of node being queried

◆ HasSumHess()

template<typename ThresholdType , typename LeafOutputType >
bool treelite::Tree< ThresholdType, LeafOutputType >::HasSumHess ( int  nid) const
inline

Test whether this node has hessian sum.

Parameters
nidID of node being queried

◆ Init()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::Init
inline

Initialize the tree with a single root node.

◆ IsLeaf()

template<typename ThresholdType , typename LeafOutputType >
bool treelite::Tree< ThresholdType, LeafOutputType >::IsLeaf ( int  nid) const
inline

Whether the node is leaf node.

Parameters
nidID of node being queried

◆ LeafValue()

template<typename ThresholdType , typename LeafOutputType >
LeafOutputType treelite::Tree< ThresholdType, LeafOutputType >::LeafValue ( int  nid) const
inline

Get leaf value of the leaf node.

Parameters
nidID of node being queried

◆ LeafVector()

template<typename ThresholdType , typename LeafOutputType >
std::vector<LeafOutputType> treelite::Tree< ThresholdType, LeafOutputType >::LeafVector ( int  nid) const
inline

get leaf vector of the leaf node; useful for multi-class random forest classifier

Parameters
nidID of node being queried

◆ LeftChild()

template<typename ThresholdType , typename LeafOutputType >
int treelite::Tree< ThresholdType, LeafOutputType >::LeftChild ( int  nid) const
inline

Index of the node's left child.

Getters

Parameters
nidID of node being queried

◆ NodeType()

template<typename ThresholdType , typename LeafOutputType >
TreeNodeType treelite::Tree< ThresholdType, LeafOutputType >::NodeType ( int  nid) const
inline

Get the type of a node.

Parameters
nidID of node being queried

◆ operator=() [1/2]

template<typename ThresholdType , typename LeafOutputType >
Tree& treelite::Tree< ThresholdType, LeafOutputType >::operator= ( Tree< ThresholdType, LeafOutputType > &&  )
defaultnoexcept

◆ operator=() [2/2]

template<typename ThresholdType , typename LeafOutputType >
Tree& treelite::Tree< ThresholdType, LeafOutputType >::operator= ( Tree< ThresholdType, LeafOutputType > const &  )
delete

◆ RightChild()

template<typename ThresholdType , typename LeafOutputType >
int treelite::Tree< ThresholdType, LeafOutputType >::RightChild ( int  nid) const
inline

Index of the node's right child.

Parameters
nidID of node being queried

◆ SetCategoricalTest()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::SetCategoricalTest ( int  nid,
std::int32_t  split_index,
bool  default_left,
std::vector< std::uint32_t > const &  category_list,
bool  category_list_right_child 
)
inline

Create a categorical test.

Parameters
nidID of node being updated
split_indexFeature index to split
default_leftDefault direction when feature is unknown
category_listList of categories to belong to either the right child node or the left child node. Set categories_list_right_child parameter to indicate which node the category list should represent.
category_list_right_childWhether category_list indicates the list of categories for the right child node (true) or the left child node (false)

◆ SetChildren()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::SetChildren ( int  nid,
int  left_child,
int  right_child 
)
inline

Identify two child nodes of the node.

Setters

Parameters
nidID of node being modified
left_childID of the left child node
right_childID of the right child node

◆ SetDataCount()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::SetDataCount ( int  nid,
std::uint64_t  data_count 
)
inline

Set the data count of the node.

Parameters
nidID of node being updated
data_countData count

◆ SetGain()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::SetGain ( int  nid,
double  gain 
)
inline

Set the gain value of the node.

Parameters
nidID of node being updated
gainGain value

◆ SetLeaf()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::SetLeaf ( int  nid,
LeafOutputType  value 
)
inline

Set the leaf value of the node.

Parameters
nidID of node being updated
valueLeaf value

◆ SetLeafVector()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::SetLeafVector ( int  nid,
std::vector< LeafOutputType > const &  leaf_vector 
)
inline

Set the leaf vector of the node; useful for multi-class random forest classifier.

Parameters
nidID of node being updated
leaf_vectorLeaf vector

◆ SetNumericalTest()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::SetNumericalTest ( int  nid,
std::int32_t  split_index,
ThresholdType  threshold,
bool  default_left,
Operator  cmp 
)
inline

Create a numerical test.

Parameters
nidID of node being updated
split_indexFeature index to split
thresholdThreshold value
default_leftDefault direction when feature is unknown
cmpComparison operator to compare between feature value and threshold

◆ SetSumHess()

template<typename ThresholdType , typename LeafOutputType >
void treelite::Tree< ThresholdType, LeafOutputType >::SetSumHess ( int  nid,
double  sum_hess 
)
inline

Set the hessian sum of the node.

Parameters
nidID of node being updated
sum_hessHessian sum

◆ SplitIndex()

template<typename ThresholdType , typename LeafOutputType >
std::int32_t treelite::Tree< ThresholdType, LeafOutputType >::SplitIndex ( int  nid) const
inline

Feature index of the node's split condition.

Parameters
nidID of node being queried

◆ SumHess()

template<typename ThresholdType , typename LeafOutputType >
double treelite::Tree< ThresholdType, LeafOutputType >::SumHess ( int  nid) const
inline

Get hessian sum.

Parameters
nidID of node being queried

◆ Threshold()

template<typename ThresholdType , typename LeafOutputType >
ThresholdType treelite::Tree< ThresholdType, LeafOutputType >::Threshold ( int  nid) const
inline

Get threshold of the node.

Parameters
nidID of node being queried

Friends And Related Function Documentation

◆ detail::field_accessor::GetTreeFieldImpl

template<typename ThresholdType , typename LeafOutputType >
template<typename X , typename Y >
PyBufferFrame detail::field_accessor::GetTreeFieldImpl ( ModelPreset< X, Y > &  ,
std::uint64_t  ,
std::string const &   
)
friend

◆ detail::field_accessor::SetTreeFieldImpl

template<typename ThresholdType , typename LeafOutputType >
template<typename X , typename Y >
void detail::field_accessor::SetTreeFieldImpl ( ModelPreset< X, Y > &  ,
std::uint64_t  ,
std::string const &  ,
PyBufferFrame   
)
friend

◆ detail::serializer::Deserializer

template<typename ThresholdType , typename LeafOutputType >
template<typename MixIn >
friend class detail::serializer::Deserializer
friend

◆ detail::serializer::Serializer

template<typename ThresholdType , typename LeafOutputType >
template<typename MixIn >
friend class detail::serializer::Serializer
friend

◆ DumpTreeAsJSON

template<typename ThresholdType , typename LeafOutputType >
template<typename WriterType , typename X , typename Y >
void DumpTreeAsJSON ( WriterType &  writer,
Tree< X, Y > const &  tree 
)
friend

Member Data Documentation

◆ num_nodes

template<typename ThresholdType , typename LeafOutputType >
std::int32_t treelite::Tree< ThresholdType, LeafOutputType >::num_nodes {0}

Number of nodes.


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