⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opencvref_ml.htm

📁 Simple ellipse fitting example on C++Builder6 + OpenCV1.0.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html><head>
<link rel="STYLESHEET" href="opencvref.css" charset="ISO-8859-1" type="text/css">
<title>Machine Learning Reference</title>
</head><body>

<h1>Machine Learning Reference</h1>

<p font-size="120%" color="#0000ff">&nbsp;</p>

<hr><p><ul>
<!-- <li><a href=#ch_introduction>Introduction</a> -->
<li><a href=#ch_commonfunctions>Introduction. Common classes and functions</a>
<li><a href=#ch_nbayes>Normal Bayes Classifier</a>
<li><a href=#ch_knn>K Nearest Neighbors</a>
<li><a href=#ch_svm>SVM</a>
<li><a href=#ch_dtree>Decision Trees</a>
<li><a href=#ch_boosting>Boosting</a>
<li><a href=#ch_randomforest>Random Trees</a>
<li><a href=#ch_em>Expectation-Maximization</a>
<li><a href=#ch_ann>Neural Networks</a>
<!-- <li><a href=#ch_cnn>Convolutional Neural Networks</a>
<li><a href=#ch_crossvalid>Cross-validation method of accuracy estimation</a> -->
</ul><p></p>

<hr><h2><a name="ch_introduction">Introduction. Common classes and functions</a></h2>

<!-- *****************************************************************************************
     *****************************************************************************************
     ***************************************************************************************** -->

<p>The Machine Learning Library (MLL) is a set of classes and functions for statistical classification,
regression and clustering of data.</p>

<p>Most of the classification and regression algorithms are implemented as C++ classes.
As the algorithms have different set of features (like ability to handle missing measurements,
or categorical input variables etc.), there is a little common ground between the classes.
This common ground is defined by the class <code>CvStatModel</code> that all
the other ML classes are derived from.

<hr><h3><a name="decl_CvStatModel">CvStatModel</a></h3>
<p class="Blurb">Base class for statistical models in ML</p>
<pre>
class CvStatModel
{
public:
    <i>/* <a href="#decl_CvStatModel_c">CvStatModel</a>(); */</i>
    <i>/* <a href="#decl_CvStatModel_ct">CvStatModel</a>( const CvMat* train_data ... ); */</i>

    virtual <a href="#decl_CvStatModel_ct">~CvStatModel</a>();

    virtual void <a href="#decl_CvStatModel_clear">clear()</a>=0;

    <i>/* virtual bool <a href="#decl_CvStatModel_train">train</a>( const CvMat* train_data, [int tflag,] ..., const CvMat* responses, ...,
    [const CvMat* var_idx,] ..., [const CvMat* sample_idx,] ...
    [const CvMat* var_type,] ..., [const CvMat* missing_mask,] &lt;misc_training_alg_params&gt; ... )=0;
     */</i>

    <i>/* virtual float <a href="#decl_CvStatModel_predict">predict</a>( const CvMat* sample ... ) const=0; */</i>

    virtual void <a href="#decl_CvStatModel_save">save</a>( const char* filename, const char* name=0 )=0;
    virtual void <a href="#decl_CvStatModel_load">load</a>( const char* filename, const char* name=0 )=0;

    virtual void <a href="#decl_CvStatModel_write">write</a>( CvFileStorage* storage, const char* name )=0;
    virtual void <a href="#decl_CvStatModel_read">read</a>( CvFileStorage* storage, CvFileNode* node )=0;
};
</pre>

<p>
In this declaration some methods are commented off.
Actually, these are methods for which there is no unified API
(with the exception of the default constructor), however, there are many similarities
in the syntax and semantics that are briefly described below in this section,
as if they are a part of the base class.
</p>


<hr><h3><a name="decl_CvStatModel_c">CvStatModel::CvStatModel</a></h3>
<p class="Blurb">Default constructor</p>
<pre>
CvStatModel::CvStatModel();
</pre>
<p>Each statistical model class in ML has default constructor without parameters.
This constructor is useful for 2-stage model construction, when the default constructor
is followed by <a href="#decl_CvStatModel_train">train()</a> or
<a href="#decl_CvStatModel_load">load()</a>.


<hr><h3><a name="decl_CvStatModel_ct">CvStatModel::CvStatModel(...)</a></h3>
<p class="Blurb">Training constructor</p>
<pre>
CvStatModel::CvStatModel( const CvMat* train_data ... ); */
</pre>
<p>Most ML classes provide single-step construct+train constructor.
This constructor is equivalent to the default constructor, followed by the
<a href="#decl_CvStatModel_train">train()</a> method with the parameters that 
passed to the constructor.</p>


<hr><h3><a name="decl_CvStatModel_d">CvStatModel::~CvStatModel</a></h3>
<p class="Blurb">Virtual destructor</p>
<pre>
CvStatModel::~CvStatModel();
</pre>
<p>
The destructor of the base class is declared as virtual,
so it is safe to write the following code:

<pre>
CvStatModel* model;
if( use_svm )
    model = new CvSVM(... /* SVM params */);
else
    model = new CvDTree(... /* Decision tree params */);
...
delete model;
</pre>

Normally, the destructor of each derived class does nothing, but calls
the overridden method <a href="decl_CvStatModel_clear">clear()</a> that deallocates all the memory.
</p>


<hr><h3><a name="decl_CvStatModel_clear">CvStatModel::clear</a></h3>
<p class="Blurb">Deallocates memory and resets the model state</p>
<pre>
void CvStatModel::clear();
</pre>
<p>
The method <code>clear</code> does the same job as the destructor, i.e. it deallocates all the memory
occupied by the class members. But the object itself is not destructed, and it can be reused further.
This method is called from the destructor, from the <code>train</code> methods of the derived classes,
from the methods <a href="decl_CvStatModel_load">load()</a>,
<a href="decl_CvStatModel_read">read()</a> etc., or even explicitly by user.
</p>


<hr><h3><a name="decl_CvStatModel_save">CvStatModel::save</a></h3>
<p class="Blurb">Saves the model to file</p>
<pre>
void CvStatModel::save( const char* filename, const char* name=0 );
</pre>
<p>
The method <code>save</code> stores the complete model state to the specified XML or YAML file
with the specified name or default name (that depends on the particular class).
<a href="opencvref_cxcore.htm#cxcore_persistence">Data persistence</a>
functionality from cxcore is used.
</p>


<hr><h3><a name="decl_CvStatModel_load">CvStatModel::load</a></h3>
<p class="Blurb">Loads the model from file</p>
<pre>
void CvStatModel::load( const char* filename, const char* name=0 );
</pre>
<p>
The method <code>load</code> loads the complete model state with the specified 
name
(or default model-dependent name) from the specified XML or YAML file. The 
previous model state is cleared by <a href="#decl_CvStatModel_clear">clear()</a>.</p>
<p>
Note that the method is virtual, therefore any model can be loaded using this virtual method.
However, unlike the C types of OpenCV that can be loaded using generic
<a href="opencvref_cxcore.htm#decl_cvLoad">cvLoad()</a>, in this case the model type
must be known anyway, because an empty model, an instance of the appropriate class,
must be constructed beforehand.
This limitation will be removed in the later ML versions.
</p>


<hr><h3><a name="decl_CvStatModel_write">CvStatModel::write</a></h3>
<p class="Blurb">Writes the model to file storage</p>
<pre>
void CvStatModel::write( CvFileStorage* storage, const char* name );
</pre>
<p>
The method <code>write</code> stores the complete model state 
to the file storage
with the specified name or default name (that depends on the particular class).
The method is called by <a href="#decl_CvStatModel_save">save()</a>.
</p>


<hr><h3><a name="decl_CvStatModel_read">CvStatModel::read</a></h3>
<p class="Blurb">Reads the model from file storage</p>
<pre>
void CvStatMode::read( CvFileStorage* storage, CvFileNode* node );
</pre>
<p>
The method <code>read</code> restores the complete model state from the specified node
of the file storage. The node must be located by user, for example, using
the function <a href="opencvref_cxcore.htm#decl_cvGetFileNodeByName">cvGetFileNodeByName()</a>.
The method is called by <a href="#decl_CvStatModel_load">load()</a>.
</p>
<p>The previous model state is cleared by <a href="#decl_CvStatModel_clear">clear()</a>.
</p>


<hr><h3><a name="decl_CvStatModel_train">CvStatModel::train</a></h3>
<p class="Blurb">Trains the model</p>
<pre>
bool CvStatMode::train( const CvMat* train_data, [int tflag,] ..., const CvMat* responses, ...,
    [const CvMat* var_idx,] ..., [const CvMat* sample_idx,] ...
    [const CvMat* var_type,] ..., [const CvMat* missing_mask,] &lt;misc_training_alg_params&gt; ... );
</pre>
<p>
The method trains the statistical model using a set of input feature vectors and the corresponding
output values (responses). Both input and output vectors/values are passed as matrices.
By default the input feature vectors are stored as <code>train_data</code> rows,
i.e. all the components (features) of a training vector are stored
continuously. However, some algorithms can handle the transposed representation,
when all values of each particular feature (component/input variable)
over the whole input set are stored continuously. If both layouts are supported, the method
includes <code>tflag</code> parameter that specifies the orientation:<br>
<code>tflag=CV_ROW_SAMPLE</code> means that the feature vectors are stored as rows,<br>
<code>tflag=CV_COL_SAMPLE</code> means that the feature vectors are stored as columns.<br>
The <code>train_data</code> must have
<code>32fC1</code> (32-bit floating-point, single-channel) format.
Responses are usually stored in the 1d
vector (a row or a column) of <code>32sC1</code> (only in the classification problem) or
<code>32fC1</code> format, one value per an input vector (although some algorithms, like various flavors of neural nets,
take vector responses).<p>For classification problems the responses are discrete class labels,
for regression problems - the responses are values of the function to be approximated.
Some algorithms can deal only with classification problems, some - only with regression problems,
and some can deal with both problems. In the latter case the type of output variable is either passed as separate parameter,
or as a last element of <code>var_type</code> vector:<br>
<code>CV_VAR_CATEGORICAL</code> means that the output values are discrete class labels,<br>
<code>CV_VAR_ORDERED(=CV_VAR_NUMERICAL)</code> means that the output values are ordered, i.e.
2 different values can be compared as numbers, and this is a regression problem<br>
The types of input variables can be also specified using <code>var_type</code>.
Most algorithms can handle only ordered input variables.
</p>
<p>
Many models in the ML may be trained on a selected feature subset,
and/or on a selected sample subset of the training set. To make it easier for user,
the method <code>train</code> usually includes <code>var_idx</code>
and <code>sample_idx</code> parameters. The former identifies variables (features) of interest,
and the latter identifies samples of interest. Both vectors are either integer (<code>32sC1</code>)
vectors, i.e. lists of 0-based indices, or 8-bit (<code>8uC1</code>) masks of active variables/samples.
User may pass <code>NULL</code> pointers instead of either of the argument, meaning that
all the variables/samples are used for training.
</p>
<p>Additionally some algorithms can handle missing measurements,
that is when certain features of
certain training samples have unknown values (for example, they forgot to
measure a temperature
of patient A on Monday). The parameter <code>missing_mask</code>, 8-bit matrix of the same size as
<code>train_data</code>, is used to mark the missed values (non-zero elements of the mask).</p>
<p>Usually, the previous model state is cleared by
<a href="#decl_CvStatModel_clear">clear()</a> before running the training procedure.
However, some algorithms may optionally update the model state with the new
training data, instead of resetting it.</a>


<hr><h3><a name="decl_CvStatModel_predict">CvStatModel::predict</a></h3>
<p class="Blurb">Predicts the response for sample</p>
<pre>
float CvStatMode::predict( const CvMat* sample[, &lt;prediction_params&gt;] ) const;
</pre>
<p>
The method is used to predict the response for a new sample. In case of 
classification the method returns the class label, in case of regression - the 
output function value. The input sample must have as many components as the
<code>train_data</code> passed to <a href="#decl_CvStatModel_train">train</a>
contains. If the <code>var_idx</code> parameter is passed to <a href="#decl_CvStatModel_train">train</a>,
it is remembered and then is used to extract only the necessary components from the input sample

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -