opencv on mbed

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

RTrees Class Reference

The class implements the random forest predictor. More...

#include <ml.hpp>

Inherits cv::ml::DTrees.

Public Types

enum  Flags
 

Predict options.

More...

Public Member Functions

virtual CV_WRAP bool getCalculateVarImportance () const =0
 If true then variable importance will be calculated and then it can be retrieved by RTrees::getVarImportance.
virtual CV_WRAP void setCalculateVarImportance (bool val)=0
 

If true then variable importance will be calculated and then it can be retrieved by RTrees::getVarImportance.


virtual CV_WRAP int getActiveVarCount () const =0
 The size of the randomly selected subset of features at each tree node and that are used to find the best split(s).
virtual CV_WRAP void setActiveVarCount (int val)=0
 

The size of the randomly selected subset of features at each tree node and that are used to find the best split(s).


virtual CV_WRAP TermCriteria getTermCriteria () const =0
 The termination criteria that specifies when the training algorithm stops.
virtual CV_WRAP void setTermCriteria (const TermCriteria &val)=0
 

The termination criteria that specifies when the training algorithm stops.


virtual CV_WRAP Mat getVarImportance () const =0
 Returns the variable importance array.
virtual CV_WRAP int getMaxCategories () const =0
 Cluster possible values of a categorical variable into K<=maxCategories clusters to find a suboptimal split.
virtual CV_WRAP void setMaxCategories (int val)=0
 

Cluster possible values of a categorical variable into K<=maxCategories clusters to find a suboptimal split.


virtual CV_WRAP int getMaxDepth () const =0
 The maximum possible depth of the tree.
virtual CV_WRAP void setMaxDepth (int val)=0
 

The maximum possible depth of the tree.


virtual CV_WRAP int getMinSampleCount () const =0
 If the number of samples in a node is less than this parameter then the node will not be split.
virtual CV_WRAP void setMinSampleCount (int val)=0
 

If the number of samples in a node is less than this parameter then the node will not be split.


virtual CV_WRAP int getCVFolds () const =0
 If CVFolds > 1 then algorithms prunes the built decision tree using K-fold cross-validation procedure where K is equal to CVFolds.
virtual CV_WRAP void setCVFolds (int val)=0
 

If CVFolds > 1 then algorithms prunes the built decision tree using K-fold cross-validation procedure where K is equal to CVFolds.


virtual CV_WRAP bool getUseSurrogates () const =0
 If true then surrogate splits will be built.
virtual CV_WRAP void setUseSurrogates (bool val)=0
 

If true then surrogate splits will be built.


virtual CV_WRAP bool getUse1SERule () const =0
 If true then a pruning will be harsher.
virtual CV_WRAP void setUse1SERule (bool val)=0
 

If true then a pruning will be harsher.


virtual CV_WRAP bool getTruncatePrunedTree () const =0
 If true then pruned branches are physically removed from the tree.
virtual CV_WRAP void setTruncatePrunedTree (bool val)=0
 

If true then pruned branches are physically removed from the tree.


virtual CV_WRAP float getRegressionAccuracy () const =0
 Termination criteria for regression trees.
virtual CV_WRAP void setRegressionAccuracy (float val)=0
 

Termination criteria for regression trees.


virtual CV_WRAP cv::Mat getPriors () const =0
 The array of a priori class probabilities, sorted by the class label value.
virtual CV_WRAP void setPriors (const cv::Mat &val)=0
 

The array of a priori class probabilities, sorted by the class label value.


virtual const std::vector< int > & getRoots () const =0
 Returns indices of root nodes.
virtual const std::vector< Node > & getNodes () const =0
 Returns all the nodes.
virtual const std::vector
< Split > & 
getSplits () const =0
 Returns all the splits.
virtual const std::vector< int > & getSubsets () const =0
 Returns all the bitsets for categorical splits.
virtual CV_WRAP int getVarCount () const =0
 Returns the number of variables in training samples.
virtual CV_WRAP bool empty () const
 Returns true if the Algorithm is empty (e.g.
virtual CV_WRAP bool isTrained () const =0
 Returns true if the model is trained.
virtual CV_WRAP bool isClassifier () const =0
 Returns true if the model is classifier.
virtual CV_WRAP bool train (const Ptr< TrainData > &trainData, int flags=0)
 Trains the statistical model.
virtual CV_WRAP bool train (InputArray samples, int layout, InputArray responses)
 Trains the statistical model.
virtual CV_WRAP float calcError (const Ptr< TrainData > &data, bool test, OutputArray resp) const
 Computes error on the training or test dataset.
virtual CV_WRAP float predict (InputArray samples, OutputArray results=noArray(), int flags=0) const =0
 Predicts response(s) for the provided sample(s)
virtual CV_WRAP void clear ()
 Clears the algorithm state.
virtual void write (FileStorage &fs) const
 Stores algorithm parameters in a file storage.
virtual void read (const FileNode &fn)
 Reads algorithm parameters from a file storage.
virtual CV_WRAP void save (const String &filename) const
 Saves the algorithm to a file.
virtual CV_WRAP String getDefaultName () const
 Returns the algorithm string identifier.

Static Public Member Functions

static CV_WRAP Ptr< RTreescreate ()
 Creates the empty model.
template<typename _Tp >
static Ptr< _Tp > train (const Ptr< TrainData > &data, int flags=0)
 Create and train model with default parameters.
template<typename _Tp >
static Ptr< _Tp > read (const FileNode &fn)
 Reads algorithm from the file node.
template<typename _Tp >
static Ptr< _Tp > load (const String &filename, const String &objname=String())
 Loads algorithm from the file.
template<typename _Tp >
static Ptr< _Tp > loadFromString (const String &strModel, const String &objname=String())
 Loads algorithm from a String.

Detailed Description

The class implements the random forest predictor.

See also:
ml_intro_rtrees

Definition at line 1112 of file ml.hpp.


Member Enumeration Documentation

enum Flags [inherited]

Predict options.

Reimplemented from StatModel.

Definition at line 933 of file ml.hpp.


Member Function Documentation

virtual CV_WRAP float calcError ( const Ptr< TrainData > &  data,
bool  test,
OutputArray  resp 
) const [virtual, inherited]

Computes error on the training or test dataset.

Parameters:
datathe training data
testif true, the error is computed over the test subset of the data, otherwise it's computed over the training subset of the data. Please note that if you loaded a completely different dataset to evaluate already trained classifier, you will probably want not to set the test subset at all with TrainData::setTrainTestSplitRatio and specify test=false, so that the error is computed for the whole new set. Yes, this sounds a bit confusing.
respthe optional output responses.

The method uses StatModel::predict to compute the error. For regression models the error is computed as RMS, for classifiers - as a percent of missclassified samples (0-100%).

virtual CV_WRAP void clear (  ) [virtual, inherited]

Clears the algorithm state.

Reimplemented in DescriptorMatcher, and FlannBasedMatcher.

Definition at line 2984 of file core.hpp.

static CV_WRAP Ptr<RTrees> create (  ) [static]

Creates the empty model.

Use StatModel::train to train the model, StatModel::train to create and train the model, Algorithm::load to load the pre-trained model.

Reimplemented from DTrees.

virtual CV_WRAP bool empty (  ) const [virtual, inherited]

Returns true if the Algorithm is empty (e.g.

in the very beginning or after unsuccessful read

Reimplemented from Algorithm.

virtual CV_WRAP int getActiveVarCount (  ) const [pure virtual]

The size of the randomly selected subset of features at each tree node and that are used to find the best split(s).

If you set it to 0 then the size will be set to the square root of the total number of features. Default value is 0.

See also:
setActiveVarCount
virtual CV_WRAP bool getCalculateVarImportance (  ) const [pure virtual]

If true then variable importance will be calculated and then it can be retrieved by RTrees::getVarImportance.

Default value is false.

See also:
setCalculateVarImportance
virtual CV_WRAP int getCVFolds (  ) const [pure virtual, inherited]

If CVFolds > 1 then algorithms prunes the built decision tree using K-fold cross-validation procedure where K is equal to CVFolds.

Default value is 10.

See also:
setCVFolds
virtual CV_WRAP String getDefaultName (  ) const [virtual, inherited]

Returns the algorithm string identifier.

This string is used as top level xml/yml node tag when the object is saved to a file or string.

virtual CV_WRAP int getMaxCategories (  ) const [pure virtual, inherited]

Cluster possible values of a categorical variable into K<=maxCategories clusters to find a suboptimal split.

If a discrete variable, on which the training procedure tries to make a split, takes more than maxCategories values, the precise best subset estimation may take a very long time because the algorithm is exponential. Instead, many decision trees engines (including our implementation) try to find sub-optimal split in this case by clustering all the samples into maxCategories clusters that is some categories are merged together. The clustering is applied only in n > 2-class classification problems for categorical variables with N > max_categories possible values. In case of regression and 2-class classification the optimal split can be found efficiently without employing clustering, thus the parameter is not used in these cases. Default value is 10.

See also:
setMaxCategories
virtual CV_WRAP int getMaxDepth (  ) const [pure virtual, inherited]

The maximum possible depth of the tree.

That is the training algorithms attempts to split a node while its depth is less than maxDepth. The root node has zero depth. The actual depth may be smaller if the other termination criteria are met (see the outline of the training procedure here), and/or if the tree is pruned. Default value is INT_MAX.

See also:
setMaxDepth
virtual CV_WRAP int getMinSampleCount (  ) const [pure virtual, inherited]

If the number of samples in a node is less than this parameter then the node will not be split.

Default value is 10.

See also:
setMinSampleCount
virtual const std::vector<Node>& getNodes (  ) const [pure virtual, inherited]

Returns all the nodes.

all the node indices are indices in the returned vector

virtual CV_WRAP cv::Mat getPriors (  ) const [pure virtual, inherited]

The array of a priori class probabilities, sorted by the class label value.

The parameter can be used to tune the decision tree preferences toward a certain class. For example, if you want to detect some rare anomaly occurrence, the training base will likely contain much more normal cases than anomalies, so a very good classification performance will be achieved just by considering every case as normal. To avoid this, the priors can be specified, where the anomaly probability is artificially increased (up to 0.5 or even greater), so the weight of the misclassified anomalies becomes much bigger, and the tree is adjusted properly.

You can also think about this parameter as weights of prediction categories which determine relative weights that you give to misclassification. That is, if the weight of the first category is 1 and the weight of the second category is 10, then each mistake in predicting the second category is equivalent to making 10 mistakes in predicting the first category. Default value is empty Mat.

See also:
setPriors
virtual CV_WRAP float getRegressionAccuracy (  ) const [pure virtual, inherited]

Termination criteria for regression trees.

If all absolute differences between an estimated value in a node and values of train samples in this node are less than this parameter then the node will not be split further. Default value is 0.01f

See also:
setRegressionAccuracy
virtual const std::vector<int>& getRoots (  ) const [pure virtual, inherited]

Returns indices of root nodes.

virtual const std::vector<Split>& getSplits (  ) const [pure virtual, inherited]

Returns all the splits.

all the split indices are indices in the returned vector

virtual const std::vector<int>& getSubsets (  ) const [pure virtual, inherited]

Returns all the bitsets for categorical splits.

Split::subsetOfs is an offset in the returned vector

virtual CV_WRAP TermCriteria getTermCriteria (  ) const [pure virtual]

The termination criteria that specifies when the training algorithm stops.

Either when the specified number of trees is trained and added to the ensemble or when sufficient accuracy (measured as OOB error) is achieved. Typically the more trees you have the better the accuracy. However, the improvement in accuracy generally diminishes and asymptotes pass a certain number of trees. Also to keep in mind, the number of tree increases the prediction time linearly. Default value is TermCriteria(TermCriteria::MAX_ITERS + TermCriteria::EPS, 50, 0.1)

See also:
setTermCriteria
virtual CV_WRAP bool getTruncatePrunedTree (  ) const [pure virtual, inherited]

If true then pruned branches are physically removed from the tree.

Otherwise they are retained and it is possible to get results from the original unpruned (or pruned less aggressively) tree. Default value is true.

See also:
setTruncatePrunedTree
virtual CV_WRAP bool getUse1SERule (  ) const [pure virtual, inherited]

If true then a pruning will be harsher.

This will make a tree more compact and more resistant to the training data noise but a bit less accurate. Default value is true.

See also:
setUse1SERule
virtual CV_WRAP bool getUseSurrogates (  ) const [pure virtual, inherited]

If true then surrogate splits will be built.

These splits allow to work with missing data and compute variable importance correctly. Default value is false.

Note:
currently it's not implemented.
See also:
setUseSurrogates
virtual CV_WRAP int getVarCount (  ) const [pure virtual, inherited]

Returns the number of variables in training samples.

virtual CV_WRAP Mat getVarImportance (  ) const [pure virtual]

Returns the variable importance array.

The method returns the variable importance vector, computed at the training stage when CalculateVarImportance is set to true. If this flag was set to false, the empty matrix is returned.

virtual CV_WRAP bool isClassifier (  ) const [pure virtual, inherited]

Returns true if the model is classifier.

virtual CV_WRAP bool isTrained (  ) const [pure virtual, inherited]

Returns true if the model is trained.

static Ptr<_Tp> load ( const String &  filename,
const String &  objname = String() 
) [static, inherited]

Loads algorithm from the file.

Parameters:
filenameName of the file to read.
objnameThe optional name of the node to read (if empty, the first top-level node will be used)

This is static template method of Algorithm. It's usage is following (in the case of SVM):

     Ptr<SVM> svm = Algorithm::load<SVM>("my_svm_model.xml");

In order to make this method work, the derived class must overwrite Algorithm::read(const FileNode& fn).

Definition at line 3027 of file core.hpp.

static Ptr<_Tp> loadFromString ( const String &  strModel,
const String &  objname = String() 
) [static, inherited]

Loads algorithm from a String.

Parameters:
strModelThe string variable containing the model you want to load.
objnameThe optional name of the node to read (if empty, the first top-level node will be used)

This is static template method of Algorithm. It's usage is following (in the case of SVM):

     Ptr<SVM> svm = Algorithm::loadFromString<SVM>(myStringModel);

Definition at line 3046 of file core.hpp.

virtual CV_WRAP float predict ( InputArray  samples,
OutputArray  results = noArray(),
int  flags = 0 
) const [pure virtual, inherited]

Predicts response(s) for the provided sample(s)

Parameters:
samplesThe input samples, floating-point matrix
resultsThe optional output matrix of results.
flagsThe optional flags, model-dependent. See cv::ml::StatModel::Flags.

Implemented in LogisticRegression.

virtual void read ( const FileNode fn ) [virtual, inherited]

Reads algorithm parameters from a file storage.

Reimplemented in DescriptorMatcher, and FlannBasedMatcher.

Definition at line 2992 of file core.hpp.

static Ptr<_Tp> read ( const FileNode fn ) [static, inherited]

Reads algorithm from the file node.

This is static template method of Algorithm. It's usage is following (in the case of SVM):

     Ptr<SVM> svm = Algorithm::read<SVM>(fn);

In order to make this method work, the derived class must overwrite Algorithm::read(const FileNode& fn) and also have static create() method without parameters (or with all the optional parameters)

Reimplemented in DescriptorMatcher, and FlannBasedMatcher.

Definition at line 3008 of file core.hpp.

virtual CV_WRAP void save ( const String &  filename ) const [virtual, inherited]

Saves the algorithm to a file.

In order to make this method work, the derived class must implement Algorithm::write(FileStorage& fs).

virtual CV_WRAP void setActiveVarCount ( int  val ) [pure virtual]

The size of the randomly selected subset of features at each tree node and that are used to find the best split(s).

See also:
getActiveVarCount
virtual CV_WRAP void setCalculateVarImportance ( bool  val ) [pure virtual]

If true then variable importance will be calculated and then it can be retrieved by RTrees::getVarImportance.

See also:
getCalculateVarImportance
virtual CV_WRAP void setCVFolds ( int  val ) [pure virtual, inherited]

If CVFolds > 1 then algorithms prunes the built decision tree using K-fold cross-validation procedure where K is equal to CVFolds.

See also:
getCVFolds
virtual CV_WRAP void setMaxCategories ( int  val ) [pure virtual, inherited]

Cluster possible values of a categorical variable into K<=maxCategories clusters to find a suboptimal split.

See also:
getMaxCategories
virtual CV_WRAP void setMaxDepth ( int  val ) [pure virtual, inherited]

The maximum possible depth of the tree.

See also:
getMaxDepth
virtual CV_WRAP void setMinSampleCount ( int  val ) [pure virtual, inherited]

If the number of samples in a node is less than this parameter then the node will not be split.

See also:
getMinSampleCount
virtual CV_WRAP void setPriors ( const cv::Mat val ) [pure virtual, inherited]

The array of a priori class probabilities, sorted by the class label value.

See also:
getPriors
virtual CV_WRAP void setRegressionAccuracy ( float  val ) [pure virtual, inherited]

Termination criteria for regression trees.

See also:
getRegressionAccuracy
virtual CV_WRAP void setTermCriteria ( const TermCriteria val ) [pure virtual]

The termination criteria that specifies when the training algorithm stops.

See also:
getTermCriteria
virtual CV_WRAP void setTruncatePrunedTree ( bool  val ) [pure virtual, inherited]

If true then pruned branches are physically removed from the tree.

See also:
getTruncatePrunedTree
virtual CV_WRAP void setUse1SERule ( bool  val ) [pure virtual, inherited]

If true then a pruning will be harsher.

See also:
getUse1SERule
virtual CV_WRAP void setUseSurrogates ( bool  val ) [pure virtual, inherited]

If true then surrogate splits will be built.

See also:
getUseSurrogates
static Ptr<_Tp> train ( const Ptr< TrainData > &  data,
int  flags = 0 
) [static, inherited]

Create and train model with default parameters.

The class must implement static `create()` method with no parameters or with all default parameter values

Definition at line 357 of file ml.hpp.

virtual CV_WRAP bool train ( InputArray  samples,
int  layout,
InputArray  responses 
) [virtual, inherited]

Trains the statistical model.

Parameters:
samplestraining samples
layoutSee ml::SampleTypes.
responsesvector of responses associated with the training samples.
virtual CV_WRAP bool train ( const Ptr< TrainData > &  trainData,
int  flags = 0 
) [virtual, inherited]

Trains the statistical model.

Parameters:
trainDatatraining data that can be loaded from file using TrainData::loadFromCSV or created with TrainData::create.
flagsoptional flags, depending on the model. Some of the models can be updated with the new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).
virtual void write ( FileStorage fs ) const [virtual, inherited]

Stores algorithm parameters in a file storage.

Reimplemented in DescriptorMatcher, and FlannBasedMatcher.

Definition at line 2988 of file core.hpp.