opencv/modules/ml/doc/statistical_models.rst

113 lines
4.2 KiB
ReStructuredText
Raw Normal View History

Statistical Models
==================
.. highlight:: cpp
2014-08-03 06:34:47 +08:00
.. index:: StatModel
2014-08-03 06:34:47 +08:00
StatModel
-----------
2014-08-03 06:34:47 +08:00
.. ocv:class:: StatModel
2014-08-03 06:34:47 +08:00
Base class for statistical models in OpenCV ML.
2011-02-26 19:05:10 +08:00
2014-08-03 06:34:47 +08:00
StatModel::train
------------------------
Trains the statistical model
2011-02-26 19:05:10 +08:00
2014-08-03 06:34:47 +08:00
.. ocv:function:: bool StatModel::train( const Ptr<TrainData>& trainData, int flags=0 )
2011-02-26 19:05:10 +08:00
2014-08-03 06:34:47 +08:00
.. ocv:function:: bool StatModel::train( InputArray samples, int layout, InputArray responses )
2011-03-03 15:29:55 +08:00
2014-08-03 06:34:47 +08:00
.. ocv:function:: Ptr<_Tp> StatModel::train(const Ptr<TrainData>& data, const _Tp::Params& p, int flags=0 )
2014-08-03 06:34:47 +08:00
.. ocv:function:: Ptr<_Tp> StatModel::train(InputArray samples, int layout, InputArray responses, const _Tp::Params& p, int flags=0 )
2014-08-03 06:34:47 +08:00
:param trainData: training data that can be loaded from file using ``TrainData::loadFromCSV`` or created with ``TrainData::create``.
2014-08-03 06:34:47 +08:00
:param samples: training samples
2014-08-03 06:34:47 +08:00
:param layout: ``ROW_SAMPLE`` (training samples are the matrix rows) or ``COL_SAMPLE`` (training samples are the matrix columns)
2014-08-03 06:34:47 +08:00
:param responses: vector of responses associated with the training samples.
2014-08-03 06:34:47 +08:00
:param p: the stat model parameters.
2014-08-03 06:34:47 +08:00
:param 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``).
2014-08-03 06:34:47 +08:00
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.
2014-08-03 06:34:47 +08:00
StatModel::isTrained
-----------------------------
2014-08-03 06:34:47 +08:00
Returns true if the model is trained
2014-08-03 06:34:47 +08:00
.. ocv:function:: bool StatModel::isTrained()
2014-08-03 06:34:47 +08:00
The method must be overwritten in the derived classes.
2014-08-03 06:34:47 +08:00
StatModel::isClassifier
-----------------------------
Returns true if the model is classifier
2014-08-03 06:34:47 +08:00
.. ocv:function:: bool StatModel::isClassifier()
2014-08-03 06:34:47 +08:00
The method must be overwritten in the derived classes.
2014-08-03 06:34:47 +08:00
StatModel::getVarCount
-----------------------------
Returns the number of variables in training samples
2011-03-03 15:29:55 +08:00
2014-08-03 06:34:47 +08:00
.. ocv:function:: int StatModel::getVarCount()
2014-08-03 06:34:47 +08:00
The method must be overwritten in the derived classes.
2014-08-03 06:34:47 +08:00
StatModel::predict
------------------
2014-08-03 06:34:47 +08:00
Predicts response(s) for the provided sample(s)
2014-08-03 06:34:47 +08:00
.. ocv:function:: float StatModel::predict( InputArray samples, OutputArray results=noArray(), int flags=0 ) const
2014-08-03 06:34:47 +08:00
:param samples: The input samples, floating-point matrix
2014-08-03 06:34:47 +08:00
:param results: The optional output matrix of results.
2014-08-03 06:34:47 +08:00
:param flags: The optional flags, model-dependent. 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.
2014-08-03 06:34:47 +08:00
StatModel::calcError
-------------------------
Computes error on the training or test dataset
2014-08-03 06:34:47 +08:00
.. ocv:function:: float StatModel::calcError( const Ptr<TrainData>& data, bool test, OutputArray resp ) const
2014-08-03 06:34:47 +08:00
:param data: the training data
2014-08-03 06:34:47 +08:00
:param 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.
2014-08-03 06:34:47 +08:00
:param resp: the optional output responses.
2014-08-03 06:34:47 +08:00
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%).
2014-08-03 06:34:47 +08:00
StatModel::save
-----------------
2014-08-03 06:34:47 +08:00
Saves the model to a file.
2014-08-03 06:34:47 +08:00
.. ocv:function:: void StatModel::save( const String& filename )
2014-08-03 06:34:47 +08:00
In order to make this method work, the derived class must overwrite ``Algorithm::write(FileStorage& fs)``.
2014-08-03 06:34:47 +08:00
StatModel::load
-----------------
Loads model from the file
2014-08-03 06:34:47 +08:00
.. ocv:function:: Ptr<_Tp> StatModel::load( const String& filename )
2014-08-03 06:34:47 +08:00
This is static template method of StatModel. It's usage is following (in the case of SVM): ::
2014-08-03 06:34:47 +08:00
Ptr<SVM> svm = StatModel::load<SVM>("my_svm_model.xml");
2014-08-03 06:34:47 +08:00
In order to make this method work, the derived class must overwrite ``Algorithm::read(const FileNode& fn)``.