class cv::ml::StatModel

Overview

Base class for statistical models in OpenCV ML. More…

#include <ml.hpp>

class StatModel: public cv::Algorithm
{
public:
    // enums

    enum Flags;

    // methods

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

// direct descendants

class ANN_MLP;
class DTrees;
class EM;
class KNearest;
class LogisticRegression;
class NormalBayesClassifier;
class SVM;
class SVMSGD;

Inherited Members

public:
    // 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);

protected:
    // methods

    void
    writeFormat(FileStorage& fs) const;

Detailed Documentation

Base class for statistical models in OpenCV ML.

Methods

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

Computes error on the training or test dataset.

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

Parameters:

data the training data
test if 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.
resp the optional output responses.
virtual
bool
empty() const

Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read.

virtual
int
getVarCount() const = 0

Returns the number of variables in training samples.

virtual
bool
isClassifier() const = 0

Returns true if the model is classifier.

virtual
bool
isTrained() const = 0

Returns true if the model is trained.

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

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

Parameters:

samples The input samples, floating-point matrix
results The optional output matrix of results.
flags The optional flags, model-dependent. See cv::ml::StatModel::Flags.
virtual
bool
train(
    const Ptr<TrainData>& trainData,
    int flags = 0
    )

Trains the statistical model.

Parameters:

trainData training data that can be loaded from file using TrainData::loadFromCSV or created with TrainData::create.
flags optional 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
bool
train(
    InputArray samples,
    int layout,
    InputArray responses
    )

Trains the statistical model.

Parameters:

samples training samples
layout See ml::SampleTypes.
responses vector of responses associated with the training samples.
template <typename _Tp>
static
Ptr<_Tp>
train(
    const Ptr<TrainData>& data,
    int flags = 0
    )

Create and train model with default parameters.

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