shark::CARTClassifier< LabelType > Class Template Reference

CART Classifier. More...

#include <shark/Models/Trees/CARTClassifier.h>

+ Inheritance diagram for shark::CARTClassifier< LabelType >:

Classes

struct  NodeInfo
 

Public Types

typedef base_type::BatchInputType BatchInputType
 
typedef base_type::BatchOutputType BatchOutputType
 
typedef std::vector< NodeInfoTreeType
 
- Public Types inherited from shark::AbstractModel< RealVector, LabelType >
enum  Feature
 
typedef RealVector InputType
 Defines the input type of the model. More...
 
typedef LabelType OutputType
 Defines the output type of the model. More...
 
typedef Batch< InputType >::type BatchInputType
 defines the batch type of the input type. More...
 
typedef Batch< OutputType >::type BatchOutputType
 defines the batch type of the output type More...
 
typedef TypedFlags< FeatureFeatures
 
typedef TypedFeatureNotAvailableException< FeatureFeatureNotAvailableException
 

Public Member Functions

 CARTClassifier ()
 Constructor. More...
 
 CARTClassifier (TreeType const &tree)
 Constructor taking the tree as argument. More...
 
 CARTClassifier (TreeType &&tree)
 
 CARTClassifier (TreeType const &tree, bool optimize)
 Constructor taking the tree as argument and optimize it if requested. More...
 
 CARTClassifier (TreeType const &tree, std::size_t d)
 Constructor taking the tree as argument as well as maximum number of attributes. More...
 
 CARTClassifier (TreeType &&tree, std::size_t d) BOOST_NOEXCEPT_IF((std
 
std::string name () const
 From INameable: return the class name. More...
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &patterns, BatchOutputType &outputs) const
 Evaluate the Tree on a batch of patterns. More...
 
void eval (BatchInputType const &patterns, BatchOutputType &outputs, State &state) const
 Standard interface for evaluating the response of the model to a batch of patterns. More...
 
void eval (RealVector const &pattern, LabelType &output)
 Evaluate the Tree on a single pattern. More...
 
void setTree (TreeType const &tree)
 Set the model tree. More...
 
TreeType getTree () const
 Get the model tree. More...
 
std::size_t numberOfParameters () const
 The model does not have any parameters. More...
 
RealVector parameterVector () const
 The model does not have any parameters. More...
 
void setParameterVector (RealVector const &param)
 The model does not have any parameters. More...
 
void read (InArchive &archive)
 from ISerializable, reads a model from an archive More...
 
void write (OutArchive &archive) const
 from ISerializable, writes a model to an archive More...
 
UIntVector countAttributes () const
 
std::size_t inputSize () const
 Return input dimension. More...
 
void setInputDimension (std::size_t d)
 
void computeOOBerror (const ClassificationDataset &dataOOB)
 Compute oob error, given an oob dataset (Classification) More...
 
void computeOOBerror (RegressionDataset const &dataOOB)
 Compute oob error, given an oob dataset (Regression) More...
 
double OOBerror () const
 Return OOB error. More...
 
RealVector const & featureImportances () const
 Return feature importances. More...
 
void computeFeatureImportances (ClassificationDataset const &dataOOB, random::rng_type &rng)
 Compute feature importances, given an oob dataset (Classification) More...
 
void computeFeatureImportances (RegressionDataset const &dataOOB, random::rng_type &rng)
 Compute feature importances, given an oob dataset (Regression) More...
 
- Public Member Functions inherited from shark::AbstractModel< RealVector, LabelType >
 AbstractModel ()
 
virtual ~AbstractModel ()
 
const Featuresfeatures () const
 
virtual void updateFeatures ()
 
bool hasFirstParameterDerivative () const
 Returns true when the first parameter derivative is implemented. More...
 
bool hasSecondParameterDerivative () const
 Returns true when the second parameter derivative is implemented. More...
 
bool hasFirstInputDerivative () const
 Returns true when the first input derivative is implemented. More...
 
bool hasSecondInputDerivative () const
 Returns true when the second parameter derivative is implemented. More...
 
bool isSequential () const
 
virtual void eval (InputType const &pattern, OutputType &output) const
 Standard interface for evaluating the response of the model to a single pattern. More...
 
Data< OutputTypeoperator() (Data< InputType > const &patterns) const
 Model evaluation as an operator for a whole dataset. This is a convenience function. More...
 
OutputType operator() (InputType const &pattern) const
 Model evaluation as an operator for a single pattern. This is a convenience function. More...
 
BatchOutputType operator() (BatchInputType const &patterns) const
 Model evaluation as an operator for a single pattern. This is a convenience function. More...
 
virtual void weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, State const &state, RealVector &derivative) const
 calculates the weighted sum of derivatives w.r.t the parameters. More...
 
virtual void weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, Batch< RealMatrix >::type const &errorHessian, State const &state, RealVector &derivative, RealMatrix &hessian) const
 calculates the weighted sum of derivatives w.r.t the parameters More...
 
virtual void weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, State const &state, BatchInputType &derivative) const
 calculates the weighted sum of derivatives w.r.t the inputs More...
 
virtual void weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, typename Batch< RealMatrix >::type const &errorHessian, State const &state, RealMatrix &derivative, Batch< RealMatrix >::type &hessian) const
 calculates the weighted sum of derivatives w.r.t the inputs More...
 
virtual void weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &coefficients, State const &state, RealVector &parameterDerivative, BatchInputType &inputDerivative) const
 calculates weighted input and parameter derivative at the same time More...
 
- Public Member Functions inherited from shark::IParameterizable
virtual ~IParameterizable ()
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor. More...
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...). More...
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...). More...
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Member Functions

std::size_t findNode (std::size_t nodeId) const
 Finds the index of the node with a certain nodeID in an unoptimized tree. More...
 
void optimizeTree (TreeType &tree) const
 
template<class Vector >
LabelType const & evalPattern (Vector const &pattern) const
 Evaluate the CART tree on a single sample. More...
 

Protected Attributes

TreeType m_tree
 tree of the model More...
 
std::size_t m_inputDimension
 Number of attributes (set by trainer) More...
 
RealVector m_featureImportances
 
double m_OOBerror
 
- Protected Attributes inherited from shark::AbstractModel< RealVector, LabelType >
Features m_features
 

Detailed Description

template<class LabelType>
class shark::CARTClassifier< LabelType >

CART Classifier.

The CARTClassifier predicts a class label using the CART algorithm.
It is a decision tree algorithm.

Definition at line 58 of file CARTClassifier.h.

Member Typedef Documentation

◆ BatchInputType

template<class LabelType>
typedef base_type::BatchInputType shark::CARTClassifier< LabelType >::BatchInputType

Definition at line 63 of file CARTClassifier.h.

◆ BatchOutputType

template<class LabelType>
typedef base_type::BatchOutputType shark::CARTClassifier< LabelType >::BatchOutputType

Definition at line 64 of file CARTClassifier.h.

◆ TreeType

template<class LabelType>
typedef std::vector<NodeInfo> shark::CARTClassifier< LabelType >::TreeType

Vector of structs that contains the splitting information and the labels. The class label is a normalized histogram in the classification case. In the regression case, the label is the regression value.

Definition at line 121 of file CARTClassifier.h.

Constructor & Destructor Documentation

◆ CARTClassifier() [1/6]

template<class LabelType>
shark::CARTClassifier< LabelType >::CARTClassifier ( )
inline

Constructor.

Definition at line 124 of file CARTClassifier.h.

◆ CARTClassifier() [2/6]

template<class LabelType>
shark::CARTClassifier< LabelType >::CARTClassifier ( TreeType const &  tree)
inlineexplicit

Constructor taking the tree as argument.

Definition at line 128 of file CARTClassifier.h.

◆ CARTClassifier() [3/6]

template<class LabelType>
shark::CARTClassifier< LabelType >::CARTClassifier ( TreeType &&  tree)
inlineexplicit

Definition at line 131 of file CARTClassifier.h.

◆ CARTClassifier() [4/6]

template<class LabelType>
shark::CARTClassifier< LabelType >::CARTClassifier ( TreeType const &  tree,
bool  optimize 
)
inline

Constructor taking the tree as argument and optimize it if requested.

Definition at line 136 of file CARTClassifier.h.

◆ CARTClassifier() [5/6]

template<class LabelType>
shark::CARTClassifier< LabelType >::CARTClassifier ( TreeType const &  tree,
std::size_t  d 
)
inline

Constructor taking the tree as argument as well as maximum number of attributes.

Definition at line 145 of file CARTClassifier.h.

◆ CARTClassifier() [6/6]

template<class LabelType>
shark::CARTClassifier< LabelType >::CARTClassifier ( TreeType &&  tree,
std::size_t  d 
)
inline

Definition at line 151 of file CARTClassifier.h.

Member Function Documentation

◆ computeFeatureImportances() [1/2]

template<class LabelType>
void shark::CARTClassifier< LabelType >::computeFeatureImportances ( ClassificationDataset const &  dataOOB,
random::rng_type &  rng 
)
inline

Compute feature importances, given an oob dataset (Classification)

Definition at line 284 of file CARTClassifier.h.

◆ computeFeatureImportances() [2/2]

template<class LabelType>
void shark::CARTClassifier< LabelType >::computeFeatureImportances ( RegressionDataset const &  dataOOB,
random::rng_type &  rng 
)
inline

Compute feature importances, given an oob dataset (Regression)

Definition at line 319 of file CARTClassifier.h.

◆ computeOOBerror() [1/2]

template<class LabelType>
void shark::CARTClassifier< LabelType >::computeOOBerror ( const ClassificationDataset dataOOB)
inline

Compute oob error, given an oob dataset (Classification)

Definition at line 250 of file CARTClassifier.h.

Referenced by shark::CARTClassifier< RealVector >::computeFeatureImportances().

◆ computeOOBerror() [2/2]

template<class LabelType>
void shark::CARTClassifier< LabelType >::computeOOBerror ( RegressionDataset const &  dataOOB)
inline

Compute oob error, given an oob dataset (Regression)

Definition at line 262 of file CARTClassifier.h.

◆ countAttributes()

template<class LabelType>
UIntVector shark::CARTClassifier< LabelType >::countAttributes ( ) const
inline

Definition at line 226 of file CARTClassifier.h.

◆ createState()

template<class LabelType>
boost::shared_ptr<State> shark::CARTClassifier< LabelType >::createState ( ) const
inlinevirtual

Creates an internal state of the model.

The state is needed when the derivatives are to be calculated. Eval can store a state which is then reused to speed up the calculations of the derivatives. This also allows eval to be evaluated in parallel!

Reimplemented from shark::AbstractModel< RealVector, LabelType >.

Definition at line 161 of file CARTClassifier.h.

◆ eval() [1/3]

template<class LabelType>
void shark::CARTClassifier< LabelType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs 
) const
inlinevirtual

Evaluate the Tree on a batch of patterns.

Reimplemented from shark::AbstractModel< RealVector, LabelType >.

Definition at line 167 of file CARTClassifier.h.

Referenced by shark::CARTClassifier< RealVector >::eval().

◆ eval() [2/3]

template<class LabelType>
void shark::CARTClassifier< LabelType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs,
State state 
) const
inlinevirtual

Standard interface for evaluating the response of the model to a batch of patterns.

Parameters
patternsthe inputs of the model
outputsthe predictions or response of the model to every pattern
stateintermediate results stored by eval which can be reused for derivative computation.

Implements shark::AbstractModel< RealVector, LabelType >.

Definition at line 180 of file CARTClassifier.h.

◆ eval() [3/3]

template<class LabelType>
void shark::CARTClassifier< LabelType >::eval ( RealVector const &  pattern,
LabelType &  output 
)
inline

Evaluate the Tree on a single pattern.

Definition at line 184 of file CARTClassifier.h.

◆ evalPattern()

template<class LabelType>
template<class Vector >
LabelType const& shark::CARTClassifier< LabelType >::evalPattern ( Vector const &  pattern) const
inlineprotected

Evaluate the CART tree on a single sample.

Definition at line 380 of file CARTClassifier.h.

Referenced by shark::CARTClassifier< RealVector >::eval().

◆ featureImportances()

template<class LabelType>
RealVector const& shark::CARTClassifier< LabelType >::featureImportances ( ) const
inline

Return feature importances.

Definition at line 279 of file CARTClassifier.h.

◆ findNode()

template<class LabelType>
std::size_t shark::CARTClassifier< LabelType >::findNode ( std::size_t  nodeId) const
inlineprotected

Finds the index of the node with a certain nodeID in an unoptimized tree.

Definition at line 358 of file CARTClassifier.h.

Referenced by shark::CARTClassifier< RealVector >::optimizeTree().

◆ getTree()

template<class LabelType>
TreeType shark::CARTClassifier< LabelType >::getTree ( ) const
inline

Get the model tree.

Definition at line 195 of file CARTClassifier.h.

◆ inputSize()

template<class LabelType>
std::size_t shark::CARTClassifier< LabelType >::inputSize ( ) const
inline

Return input dimension.

Definition at line 240 of file CARTClassifier.h.

◆ name()

template<class LabelType>
std::string shark::CARTClassifier< LabelType >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 158 of file CARTClassifier.h.

◆ numberOfParameters()

template<class LabelType>
std::size_t shark::CARTClassifier< LabelType >::numberOfParameters ( ) const
inlinevirtual

The model does not have any parameters.

Reimplemented from shark::IParameterizable.

Definition at line 200 of file CARTClassifier.h.

◆ OOBerror()

template<class LabelType>
double shark::CARTClassifier< LabelType >::OOBerror ( ) const
inline

Return OOB error.

Definition at line 274 of file CARTClassifier.h.

◆ optimizeTree()

template<class LabelType>
void shark::CARTClassifier< LabelType >::optimizeTree ( TreeType tree) const
inlineprotected

Optimize a tree, so constant lookup can be used. The optimization is done by changing the index of the children to use indices instead of node ID. Furthermore, the node IDs are converted to index numbers.

Definition at line 368 of file CARTClassifier.h.

Referenced by shark::CARTClassifier< RealVector >::CARTClassifier(), and shark::CARTClassifier< RealVector >::setTree().

◆ parameterVector()

template<class LabelType>
RealVector shark::CARTClassifier< LabelType >::parameterVector ( ) const
inlinevirtual

The model does not have any parameters.

Reimplemented from shark::IParameterizable.

Definition at line 205 of file CARTClassifier.h.

◆ read()

template<class LabelType>
void shark::CARTClassifier< LabelType >::read ( InArchive archive)
inlinevirtual

from ISerializable, reads a model from an archive

Reimplemented from shark::AbstractModel< RealVector, LabelType >.

Definition at line 215 of file CARTClassifier.h.

◆ setInputDimension()

template<class LabelType>
void shark::CARTClassifier< LabelType >::setInputDimension ( std::size_t  d)
inline

Definition at line 245 of file CARTClassifier.h.

◆ setParameterVector()

template<class LabelType>
void shark::CARTClassifier< LabelType >::setParameterVector ( RealVector const &  param)
inlinevirtual

The model does not have any parameters.

Reimplemented from shark::IParameterizable.

Definition at line 210 of file CARTClassifier.h.

◆ setTree()

template<class LabelType>
void shark::CARTClassifier< LabelType >::setTree ( TreeType const &  tree)
inline

Set the model tree.

Definition at line 189 of file CARTClassifier.h.

Referenced by shark::CARTClassifier< RealVector >::CARTClassifier().

◆ write()

template<class LabelType>
void shark::CARTClassifier< LabelType >::write ( OutArchive archive) const
inlinevirtual

from ISerializable, writes a model to an archive

Reimplemented from shark::AbstractModel< RealVector, LabelType >.

Definition at line 220 of file CARTClassifier.h.

Member Data Documentation

◆ m_featureImportances

template<class LabelType>
RealVector shark::CARTClassifier< LabelType >::m_featureImportances
protected

◆ m_inputDimension

◆ m_OOBerror

◆ m_tree


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