Statistical Models
StatModel

class StatModel
Base class for statistical models in OpenCV ML.
StatModel::train
Trains the statistical model

C++: bool StatModel::train(const Ptr<TrainData>& trainData, int flags=0 )

C++: bool StatModel::train(InputArray samples, int layout, InputArray responses)

C++: Ptr<_Tp> StatModel::train(const Ptr<TrainData>& data, const _Tp::Params& p, int flags=0 )

C++: Ptr<_Tp> StatModel::train(InputArray samples, int layout, InputArray responses, const _Tp::Params& p, int flags=0 )
Parameters: 
 trainData – training data that can be loaded from file using TrainData::loadFromCSV or created with TrainData::create.
 samples – training samples
 layout – ROW_SAMPLE (training samples are the matrix rows) or COL_SAMPLE (training samples are the matrix columns)
 responses – vector of responses associated with the training samples.
 p – the stat model parameters.
 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).

There are 2 instance methods and 2 static (class) template methods. The first two train the already created model (the very first method must be overwritten in the derived classes). And the latter two variants are convenience methods that construct empty model and then call its train method.
StatModel::isTrained
Returns true if the model is trained

C++: bool StatModel::isTrained()
The method must be overwritten in the derived classes.
StatModel::isClassifier
Returns true if the model is classifier

C++: bool StatModel::isClassifier()
The method must be overwritten in the derived classes.
StatModel::getVarCount
Returns the number of variables in training samples

C++: int StatModel::getVarCount()
The method must be overwritten in the derived classes.
StatModel::predict
Predicts response(s) for the provided sample(s)

C++: float StatModel::predict(InputArray samples, OutputArray results=noArray(), int flags=0 ) const
Parameters: 
 samples – The input samples, floatingpoint matrix
 results – The optional output matrix of results.
 flags – The optional flags, modeldependent. Some models, such as Boost, SVM recognize StatModel::RAW_OUTPUT flag, which makes the method return the raw results (the sum), not the class label.

StatModel::calcError
Computes error on the training or test dataset

C++: float StatModel::calcError(const Ptr<TrainData>& data, bool test, OutputArray resp) const
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.

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%).
StatModel::save
Saves the model to a file.

C++: void StatModel::save(const String& filename)
In order to make this method work, the derived class must overwrite Algorithm::write(FileStorage& fs).
StatModel::load
Loads model from the file

C++: Ptr<_Tp> StatModel::load(const String& filename)
This is static template method of StatModel. It’s usage is following (in the case of SVM):
Ptr<SVM> svm = StatModel::load<SVM>("my_svm_model.xml");
In order to make this method work, the derived class must overwrite Algorithm::read(const FileNode& fn).
Help and Feedback
You did not find what you were looking for?
 Ask a question on the Q&A forum.
 If you think something is missing or wrong in the documentation,
please file a bug report.