OpenCV  4.1.1-pre
Open Source Computer Vision
Looking for a C++ dev who knows OpenCV?
I'm looking for work. Hire me!
cv::ml::RTrees Class Referenceabstract

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

#include <opencv2/ml.hpp>

Inheritance diagram for cv::ml::RTrees:
Collaboration diagram for cv::ml::RTrees:

Public Types

enum  Flags {
  PREDICT_AUTO =0,
  PREDICT_SUM =(1<<8),
  PREDICT_MAX_VOTE =(2<<8),
  PREDICT_MASK =(3<<8)
}
 Predict options. More...
 

Public Member Functions

virtual float calcError (const Ptr< TrainData > &data, bool test, OutputArray resp) const
 Computes error on the training or test dataset. More...
 
virtual void clear ()
 Clears the algorithm state. More...
 
virtual bool empty () const CV_OVERRIDE
 Returns true if the Algorithm is empty (e.g. More...
 
virtual 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). More...
 
virtual bool getCalculateVarImportance () const =0
 If true then variable importance will be calculated and then it can be retrieved by RTrees::getVarImportance. More...
 
virtual 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. More...
 
virtual String getDefaultName () const
 Returns the algorithm string identifier. More...
 
virtual int getMaxCategories () const =0
 Cluster possible values of a categorical variable into K<=maxCategories clusters to find a suboptimal split. More...
 
virtual int getMaxDepth () const =0
 The maximum possible depth of the tree. More...
 
virtual int getMinSampleCount () const =0
 If the number of samples in a node is less than this parameter then the node will not be split. More...
 
virtual const std::vector< Node > & getNodes () const =0
 Returns all the nodes. More...
 
virtual cv::Mat getPriors () const =0
 The array of a priori class probabilities, sorted by the class label value. More...
 
virtual float getRegressionAccuracy () const =0
 Termination criteria for regression trees. More...
 
virtual const std::vector< int > & getRoots () const =0
 Returns indices of root nodes. More...
 
virtual const std::vector< Split > & getSplits () const =0
 Returns all the splits. More...
 
virtual const std::vector< int > & getSubsets () const =0
 Returns all the bitsets for categorical splits. More...
 
virtual TermCriteria getTermCriteria () const =0
 The termination criteria that specifies when the training algorithm stops. More...
 
virtual bool getTruncatePrunedTree () const =0
 If true then pruned branches are physically removed from the tree. More...
 
virtual bool getUse1SERule () const =0
 If true then a pruning will be harsher. More...
 
virtual bool getUseSurrogates () const =0
 If true then surrogate splits will be built. More...
 
virtual int getVarCount () const =0
 Returns the number of variables in training samples. More...
 
virtual Mat getVarImportance () const =0
 Returns the variable importance array. More...
 
virtual void getVotes (InputArray samples, OutputArray results, int flags) const =0
 Returns the result of each individual tree in the forest. More...
 
virtual bool isClassifier () const =0
 Returns true if the model is classifier. More...
 
virtual bool isTrained () const =0
 Returns true if the model is trained. More...
 
virtual float predict (InputArray samples, OutputArray results=noArray(), int flags=0) const =0
 Predicts response(s) for the provided sample(s) More...
 
virtual void read (const FileNode &fn)
 Reads algorithm parameters from a file storage. More...
 
virtual void save (const String &filename) const
 Saves the algorithm to a file. More...
 
virtual 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). More...
 
virtual void setCalculateVarImportance (bool val)=0
 If true then variable importance will be calculated and then it can be retrieved by RTrees::getVarImportance. More...
 
virtual 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. More...
 
virtual void setMaxCategories (int val)=0
 Cluster possible values of a categorical variable into K<=maxCategories clusters to find a suboptimal split. More...
 
virtual void setMaxDepth (int val)=0
 The maximum possible depth of the tree. More...
 
virtual 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. More...
 
virtual void setPriors (const cv::Mat &val)=0
 The array of a priori class probabilities, sorted by the class label value. More...
 
virtual void setRegressionAccuracy (float val)=0
 Termination criteria for regression trees. More...
 
virtual void setTermCriteria (const TermCriteria &val)=0
 The termination criteria that specifies when the training algorithm stops. More...
 
virtual void setTruncatePrunedTree (bool val)=0
 If true then pruned branches are physically removed from the tree. More...
 
virtual void setUse1SERule (bool val)=0
 If true then a pruning will be harsher. More...
 
virtual void setUseSurrogates (bool val)=0
 If true then surrogate splits will be built. More...
 
virtual bool train (const Ptr< TrainData > &trainData, int flags=0)
 Trains the statistical model. More...
 
virtual bool train (InputArray samples, int layout, InputArray responses)
 Trains the statistical model. More...
 
virtual void write (FileStorage &fs) const
 Stores algorithm parameters in a file storage. More...
 
void write (const Ptr< FileStorage > &fs, const String &name=String()) const
 simplified API for language bindings This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 

Static Public Member Functions

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

Protected Member Functions

void writeFormat (FileStorage &fs) const
 

Detailed Description

The class implements the random forest predictor.

See also
Random Trees

Member Enumeration Documentation

◆ Flags

enum cv::ml::DTrees::Flags
inherited

Predict options.

Enumerator
PREDICT_AUTO 
PREDICT_SUM 
PREDICT_MAX_VOTE 
PREDICT_MASK 

Member Function Documentation

◆ calcError()

virtual float cv::ml::StatModel::calcError ( const Ptr< TrainData > &  data,
bool  test,
OutputArray  resp 
) const
virtualinherited

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%).

◆ clear()

virtual void cv::Algorithm::clear ( )
inlinevirtualinherited

Clears the algorithm state.

Reimplemented in cv::FlannBasedMatcher, and cv::DescriptorMatcher.

◆ create()

static Ptr<RTrees> cv::ml::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.

◆ empty()

virtual bool cv::ml::StatModel::empty ( ) const
virtualinherited

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

in the very beginning or after unsuccessful read

Reimplemented from cv::Algorithm.

◆ getActiveVarCount()

virtual int cv::ml::RTrees::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

◆ getCalculateVarImportance()

virtual bool cv::ml::RTrees::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

◆ getCVFolds()

virtual int cv::ml::DTrees::getCVFolds ( ) const
pure virtualinherited

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

◆ getDefaultName()

virtual String cv::Algorithm::getDefaultName ( ) const
virtualinherited

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.

Reimplemented in cv::AKAZE, cv::KAZE, cv::SimpleBlobDetector, cv::GFTTDetector, cv::AgastFeatureDetector, cv::FastFeatureDetector, cv::MSER, cv::ORB, cv::BRISK, and cv::Feature2D.

◆ getMaxCategories()

virtual int cv::ml::DTrees::getMaxCategories ( ) const
pure virtualinherited

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

◆ getMaxDepth()

virtual int cv::ml::DTrees::getMaxDepth ( ) const
pure virtualinherited

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

◆ getMinSampleCount()

virtual int cv::ml::DTrees::getMinSampleCount ( ) const
pure virtualinherited

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

◆ getNodes()

virtual const std::vector<Node>& cv::ml::DTrees::getNodes ( ) const
pure virtualinherited

Returns all the nodes.

all the node indices are indices in the returned vector

◆ getPriors()

virtual cv::Mat cv::ml::DTrees::getPriors ( ) const
pure virtualinherited

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

◆ getRegressionAccuracy()

virtual float cv::ml::DTrees::getRegressionAccuracy ( ) const
pure virtualinherited

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

◆ getRoots()

virtual const std::vector<int>& cv::ml::DTrees::getRoots ( ) const
pure virtualinherited

Returns indices of root nodes.

◆ getSplits()

virtual const std::vector<Split>& cv::ml::DTrees::getSplits ( ) const
pure virtualinherited

Returns all the splits.

all the split indices are indices in the returned vector

◆ getSubsets()

virtual const std::vector<int>& cv::ml::DTrees::getSubsets ( ) const
pure virtualinherited

Returns all the bitsets for categorical splits.

Split::subsetOfs is an offset in the returned vector

◆ getTermCriteria()

virtual TermCriteria cv::ml::RTrees::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

◆ getTruncatePrunedTree()

virtual bool cv::ml::DTrees::getTruncatePrunedTree ( ) const
pure virtualinherited

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

◆ getUse1SERule()

virtual bool cv::ml::DTrees::getUse1SERule ( ) const
pure virtualinherited

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

◆ getUseSurrogates()

virtual bool cv::ml::DTrees::getUseSurrogates ( ) const
pure virtualinherited

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

◆ getVarCount()

virtual int cv::ml::StatModel::getVarCount ( ) const
pure virtualinherited

Returns the number of variables in training samples.

◆ getVarImportance()

virtual Mat cv::ml::RTrees::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.

◆ getVotes()

virtual void cv::ml::RTrees::getVotes ( InputArray  samples,
OutputArray  results,
int  flags 
) const
pure virtual

Returns the result of each individual tree in the forest.

In case the model is a regression problem, the method will return each of the trees' results for each of the sample cases. If the model is a classifier, it will return a Mat with samples + 1 rows, where the first row gives the class number and the following rows return the votes each class had for each sample.

Parameters
samplesArray containing the samples for which votes will be calculated.
resultsArray where the result of the calculation will be written.
flagsFlags for defining the type of RTrees.

◆ isClassifier()

virtual bool cv::ml::StatModel::isClassifier ( ) const
pure virtualinherited

Returns true if the model is classifier.

◆ isTrained()

virtual bool cv::ml::StatModel::isTrained ( ) const
pure virtualinherited

Returns true if the model is trained.

◆ load()

static Ptr<RTrees> cv::ml::RTrees::load ( const String filepath,
const String nodeName = String() 
)
static

Loads and creates a serialized RTree from a file.

Use RTree::save to serialize and store an RTree to disk. Load the RTree from this file again, by calling this function with the path to the file. Optionally specify the node for the file containing the classifier

Parameters
filepathpath to serialized RTree
nodeNamename of node containing the classifier

◆ loadFromString()

template<typename _Tp >
static Ptr<_Tp> cv::Algorithm::loadFromString ( const String strModel,
const String objname = String() 
)
inlinestaticinherited

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);

References CV_WRAP, cv::FileNode::empty(), cv::FileStorage::getFirstTopLevelNode(), cv::FileStorage::MEMORY, and cv::FileStorage::READ.

Here is the call graph for this function:

◆ predict()

virtual float cv::ml::StatModel::predict ( InputArray  samples,
OutputArray  results = noArray(),
int  flags = 0 
) const
pure virtualinherited

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 cv::ml::LogisticRegression, and cv::ml::EM.

◆ read() [1/2]

virtual void cv::Algorithm::read ( const FileNode fn)
inlinevirtualinherited

Reads algorithm parameters from a file storage.

Reimplemented in cv::FlannBasedMatcher, cv::DescriptorMatcher, and cv::Feature2D.

◆ read() [2/2]

template<typename _Tp >
static Ptr<_Tp> cv::Algorithm::read ( const FileNode fn)
inlinestaticinherited

Reads algorithm from the file node.

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

cv::FileStorage fsRead("example.xml", FileStorage::READ);
Ptr<SVM> svm = Algorithm::read<SVM>(fsRead.root());

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)

◆ save()

virtual void cv::Algorithm::save ( const String filename) const
virtualinherited

Saves the algorithm to a file.

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

◆ setActiveVarCount()

virtual void cv::ml::RTrees::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

◆ setCalculateVarImportance()

virtual void cv::ml::RTrees::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

◆ setCVFolds()

virtual void cv::ml::DTrees::setCVFolds ( int  val)
pure virtualinherited

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

◆ setMaxCategories()

virtual void cv::ml::DTrees::setMaxCategories ( int  val)
pure virtualinherited

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

See also
getMaxCategories

◆ setMaxDepth()

virtual void cv::ml::DTrees::setMaxDepth ( int  val)
pure virtualinherited

The maximum possible depth of the tree.

See also
getMaxDepth

◆ setMinSampleCount()

virtual void cv::ml::DTrees::setMinSampleCount ( int  val)
pure virtualinherited

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

See also
getMinSampleCount

◆ setPriors()

virtual void cv::ml::DTrees::setPriors ( const cv::Mat val)
pure virtualinherited

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

See also
getPriors

◆ setRegressionAccuracy()

virtual void cv::ml::DTrees::setRegressionAccuracy ( float  val)
pure virtualinherited

Termination criteria for regression trees.

See also
getRegressionAccuracy

◆ setTermCriteria()

virtual void cv::ml::RTrees::setTermCriteria ( const TermCriteria val)
pure virtual

The termination criteria that specifies when the training algorithm stops.

See also
getTermCriteria

◆ setTruncatePrunedTree()

virtual void cv::ml::DTrees::setTruncatePrunedTree ( bool  val)
pure virtualinherited

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

See also
getTruncatePrunedTree

◆ setUse1SERule()

virtual void cv::ml::DTrees::setUse1SERule ( bool  val)
pure virtualinherited

If true then a pruning will be harsher.

See also
getUse1SERule

◆ setUseSurrogates()

virtual void cv::ml::DTrees::setUseSurrogates ( bool  val)
pure virtualinherited

If true then surrogate splits will be built.

See also
getUseSurrogates

◆ train() [1/3]

virtual bool cv::ml::StatModel::train ( const Ptr< TrainData > &  trainData,
int  flags = 0 
)
virtualinherited

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).

◆ train() [2/3]

virtual bool cv::ml::StatModel::train ( InputArray  samples,
int  layout,
InputArray  responses 
)
virtualinherited

Trains the statistical model.

Parameters
samplestraining samples
layoutSee ml::SampleTypes.
responsesvector of responses associated with the training samples.

◆ train() [3/3]

template<typename _Tp >
static Ptr<_Tp> cv::ml::StatModel::train ( const Ptr< TrainData > &  data,
int  flags = 0 
)
inlinestaticinherited

Create and train model with default parameters.

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

◆ write() [1/2]

virtual void cv::Algorithm::write ( FileStorage fs) const
inlinevirtualinherited

Stores algorithm parameters in a file storage.

Reimplemented in cv::FlannBasedMatcher, cv::DescriptorMatcher, and cv::Feature2D.

References CV_WRAP.

Referenced by cv::Feature2D::write(), and cv::DescriptorMatcher::write().

Here is the caller graph for this function:

◆ write() [2/2]

void cv::Algorithm::write ( const Ptr< FileStorage > &  fs,
const String name = String() 
) const
inherited

simplified API for language bindings This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ writeFormat()

void cv::Algorithm::writeFormat ( FileStorage fs) const
protectedinherited

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