class cv::ml::RTrees

Overview

The class implements the random forest predictor. More…

#include <ml.hpp>

class RTrees: public cv::ml::DTrees
{
public:
    // methods

    virtual
    int
    getActiveVarCount() const = 0;

    virtual
    bool
    getCalculateVarImportance() const = 0;

    virtual
    TermCriteria
    getTermCriteria() const = 0;

    virtual
    Mat
    getVarImportance() const = 0;

    void
    getVotes(
        InputArray samples,
        OutputArray results,
        int flags
        ) const;

    virtual
    void
    setActiveVarCount(int val) = 0;

    virtual
    void
    setCalculateVarImportance(bool val) = 0;

    virtual
    void
    setTermCriteria(const TermCriteria& val) = 0;

    static
    Ptr<RTrees>
    create();

    static
    Ptr<RTrees>
    load(
        const String& filepath,
        const String& nodeName = String()
        );
};

Inherited Members

public:
    // enums

    enum Flags;
    enum Flags;

    // classes

    class Node;
    class Split;

    // methods

    virtual
    void
    clear();

    virtual
    bool
    empty() const;

    virtual
    String
    getDefaultName() const;

    virtual
    void
    read(const FileNode& fn);

    virtual
    void
    save(const String& filename) const;

    virtual
    void
    write(FileStorage& fs) const;

    template <typename _Tp>
    static
    Ptr<_Tp>
    load(
        const String& filename,
        const String& objname = String()
        );

    template <typename _Tp>
    static
    Ptr<_Tp>
    loadFromString(
        const String& strModel,
        const String& objname = String()
        );

    template <typename _Tp>
    static
    Ptr<_Tp>
    read(const FileNode& fn);

    virtual
    float
    calcError(
        const Ptr<TrainData>& data,
        bool test,
        OutputArray resp
        ) const;

    virtual
    bool
    empty() const;

    virtual
    int
    getVarCount() const = 0;

    virtual
    bool
    isClassifier() const = 0;

    virtual
    bool
    isTrained() const = 0;

    virtual
    float
    predict(
        InputArray samples,
        OutputArray results = noArray(),
        int flags = 0
        ) const = 0;

    virtual
    bool
    train(
        const Ptr<TrainData>& trainData,
        int flags = 0
        );

    virtual
    bool
    train(
        InputArray samples,
        int layout,
        InputArray responses
        );

    template <typename _Tp>
    static
    Ptr<_Tp>
    train(
        const Ptr<TrainData>& data,
        int flags = 0
        );

    virtual
    int
    getCVFolds() const = 0;

    virtual
    int
    getMaxCategories() const = 0;

    virtual
    int
    getMaxDepth() const = 0;

    virtual
    int
    getMinSampleCount() const = 0;

    virtual
    const std::vector<Node>&
    getNodes() const = 0;

    virtual
    cv::Mat
    getPriors() const = 0;

    virtual
    float
    getRegressionAccuracy() const = 0;

    virtual
    const std::vector<int>&
    getRoots() const = 0;

    virtual
    const std::vector<Split>&
    getSplits() const = 0;

    virtual
    const std::vector<int>&
    getSubsets() const = 0;

    virtual
    bool
    getTruncatePrunedTree() const = 0;

    virtual
    bool
    getUse1SERule() const = 0;

    virtual
    bool
    getUseSurrogates() const = 0;

    virtual
    void
    setCVFolds(int val) = 0;

    virtual
    void
    setMaxCategories(int val) = 0;

    virtual
    void
    setMaxDepth(int val) = 0;

    virtual
    void
    setMinSampleCount(int val) = 0;

    virtual
    void
    setPriors(const cv::Mat& val) = 0;

    virtual
    void
    setRegressionAccuracy(float val) = 0;

    virtual
    void
    setTruncatePrunedTree(bool val) = 0;

    virtual
    void
    setUse1SERule(bool val) = 0;

    virtual
    void
    setUseSurrogates(bool val) = 0;

    static
    Ptr<DTrees>
    create();

    static
    Ptr<DTrees>
    load(
        const String& filepath,
        const String& nodeName = String()
        );

protected:
    // methods

    void
    writeFormat(FileStorage& fs) const;

Detailed Documentation

The class implements the random forest predictor.

See also:

Random Trees

Methods

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). 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
bool
getCalculateVarImportance() const = 0

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
TermCriteria
getTermCriteria() const = 0

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
Mat
getVarImportance() const = 0

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.

void
getVotes(
    InputArray samples,
    OutputArray results,
    int flags
    ) const

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:

samples Array containg the samples for which votes will be calculated.
results Array where the result of the calculation will be written.
flags Flags for defining the type of RTrees.
virtual
void
setActiveVarCount(int val) = 0

See also:

getActiveVarCount

virtual
void
setCalculateVarImportance(bool val) = 0

See also:

getCalculateVarImportance

virtual
void
setTermCriteria(const TermCriteria& val) = 0

See also:

getTermCriteria

static
Ptr<RTrees>
create()

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.

static
Ptr<RTrees>
load(
    const String& filepath,
    const String& nodeName = String()
    )

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:

filepath path to serialized RTree
nodeName name of node containing the classifier