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

📄 机器学习中文参考手册 - opencv china.htm

📁 When I use opencv, I use this very useful paper to begin the study. This is all I searched from the
💻 HTM
📖 第 1 页 / 共 5 页
字号:
algorithms can handle only ordered input variables. </P>
<P>ML中的很多模型也可以仅仅使用选择特征的子集,或者(并且)使用选择样本的子集来训练。为了让用户易于使用,train函数通常包含var_idx和sample_idx参数。var_idx指定感兴趣的特征,sample_idx指定感兴趣的样本。这两个向量可以是整数(32sC1)向量,例如以0为开始的索引,或者8位(8uC1)的使用的特征或者样本的掩码。用户也可以传入NULL指针,用来表示训练中使用所有变量/样本。 
</P>
<P>除此之外,一些算法支持数据缺失情况,也就是某个训练样本的某个特征值未知(例如,他们忘记了在周一测量病人A的温度)。参数missing_mask,一个8位的同train_data同样大小的矩阵掩码,用来指示缺失的数据(掩码中的非零值)。 
</P>
<P>通常来说,在执行训练操作前,可以用clear()函数清除掉早先训练的模型状态。然而,一些函数可以选择用新的数据更新模型,而不是将模型重置,一切从头再来。 
</P>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=12">编辑</A>]</DIV><A 
name=CvStatModel::predict></A>
<H2>CvStatModel::predict</H2>
<P>预测样本的response </P><PRE>float CvStatMode::predict( const CvMat* sample[, &lt;prediction_params&gt;] ) const;
</PRE>
<P>这个函数用来预测一个新样本的反应值(response)。在分类问题中,这个函数返回类别编号;在回归问题中,返回函数值。输入的样本必须传给train_data的训练样本同样大小。如果训练中使用了var_idx参数,一定记住在predict函数中使用跟训练特征一致的特征。 
</P>
<P>後缀const是说预测不会影响模型的内部状态,所以这个函数可以很安全地从不同的线程调用。 </P>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=13">编辑</A>]</DIV><A 
name=Normal_Bayes_.E5.88.86.E7.B1.BB.E5.99.A8></A>
<H1>Normal Bayes 分类器</H1>
<P>This is simple classification model that assumes that feature vectors from 
each class are normally distributed (though, not necessarily independently 
distributed), so the whole data distribution function is assumed to be a 
Gaussian mixture, one component per a class. Given the training data the 
algorithm estimates mean vectors and covariation matrices for every class and 
then it uses them for prediction. </P>
<P>[Fukunaga90] K. Fukunaga. Introduction to Statistical Pattern Recognition. 
second ed., New York: Academic Press, 1990. </P>
<P>注:OpenCV 1.0rc1(0.9.9)版本的贝叶斯分类器有个小bug,训练数据时候会提示错误 </P><PRE>OpenCV ERROR: Formats of input arguments do not match ()
        in function cvSVD, cxsvd.cpp(1243)
</PRE>
<P>修改方法为将文件 ml/src/mlnbayes.cpp 中的193行: </P><PRE>CV_CALL( cov = cvCreateMat( _var_count, _var_count, CV_32FC1 ));
</PRE>
<P>改为 </P><PRE>CV_CALL( cov = cvCreateMat( _var_count, _var_count, CV_64FC1 ));
</PRE>
<P>此问题在OpenCV 1.0.0中已经得到修正。 </P>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=14">编辑</A>]</DIV><A 
name=CvNormalBayesClassifier></A>
<H2>CvNormalBayesClassifier</H2>
<P>Bayes classifier for normally distributed data </P><PRE>class CvNormalBayesClassifier&nbsp;: public CvStatModel
{
public:
    CvNormalBayesClassifier();
    virtual ~CvNormalBayesClassifier();

    CvNormalBayesClassifier( const CvMat* _train_data, const CvMat* _responses,
        const CvMat* _var_idx=0, const CvMat* _sample_idx=0 );

    virtual bool train( const CvMat* _train_data, const CvMat* _responses,
        const CvMat* _var_idx = 0, const CvMat* _sample_idx=0, bool update=false );

    virtual float predict( const CvMat* _samples, CvMat* results=0 ) const;
    virtual void clear();

    virtual void save( const char* filename, const char* name=0 );
    virtual void load( const char* filename, const char* name=0 );

    virtual void write( CvFileStorage* storage, const char* name );
    virtual void read( CvFileStorage* storage, CvFileNode* node );
protected:
    ...
};
</PRE>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=15">编辑</A>]</DIV><A 
name=CvNormalBayesClassifier::train></A>
<H2>CvNormalBayesClassifier::train</H2>
<P>Trains the model </P><PRE>bool CvNormalBayesClassifier::train( const CvMat* _train_data, const CvMat* _responses,
               const CvMat* _var_idx = 0, const CvMat* _sample_idx=0, bool update=false );
</PRE>
<P>The method trains the Normal Bayes classifier. It follows the conventions of 
generic train "method" with the following limitations: only CV_ROW_SAMPLE data 
layout is supported, the input variables are all ordered, the output variable is 
categorical (i.e. elements of _responses must be integer numbers, though the 
vector may have 32fC1 type), missing measurements are not supported. </P>
<P>In addition, there is update flag that identifies, whether the model should 
be trained from scratch (update=false) or be updated using the new training data 
(update=true). </P>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=16">编辑</A>]</DIV><A 
name=CvNormalBayesClassifier::predict></A>
<H2>CvNormalBayesClassifier::predict</H2>
<P>Predicts the response for sample(s) </P><PRE>float CvNormalBayesClassifier::predict( const CvMat* samples, CvMat* results=0 ) const;
</PRE>
<P>The method predict estimates the most probable classes for the input vectors. 
The input vectors (one or more) are stored as rows of the matrix samples. In 
case of multiple input vectors there should be output vector results. The 
predicted class for a single input vector is returned by the method. </P>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=17">编辑</A>]</DIV><A 
name=K.E8.BF.91.E9.82.BB.E7.AE.97.E6.B3.95></A>
<H1>K近邻算法</H1>
<P>这个算法首先贮藏所有的训练样本,然後通过分析(包括选举,计算加权和等方式)一个新样本周围K个最近邻以给出该样本的相应值。这种方法有时候被称作“基于样本的学习”,即为了预测,我们对于给定的输入搜索最近的已知其相应的特征向量。 
</P>
<P><BR></P>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=18">编辑</A>]</DIV><A 
name=CvKNearest></A>
<H2>CvKNearest</H2>
<P>K近邻类 </P><PRE>class CvKNearest&nbsp;: public CvStatModel //继承自ML库中的统计模型基类
{
public:

    CvKNearest();
    virtual ~CvKNearest();  //虚函数定义

    CvKNearest( const CvMat* _train_data, const CvMat* _responses,
                const CvMat* _sample_idx=0, bool _is_regression=false, int max_k=32 );

    virtual bool train( const CvMat* _train_data, const CvMat* _responses,
                        const CvMat* _sample_idx=0, bool is_regression=false,
                        int _max_k=32, bool _update_base=false );

    virtual float find_nearest( const CvMat* _samples, int k, CvMat* results,
        const float** neighbors=0, CvMat* neighbor_responses=0, CvMat* dist=0 ) const;

    virtual void clear();
    int get_max_k() const;
    int get_var_count() const;
    int get_sample_count() const;
    bool is_regression() const;

protected:
    ...
};
</PRE>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=19">编辑</A>]</DIV><A 
name=CvKNearest::train></A>
<H2>CvKNearest::train</H2>
<P>训练KNN模型 </P><PRE>bool CvKNearest::train( const CvMat* _train_data, const CvMat* _responses,
                        const CvMat* _sample_idx=0, bool is_regression=false,
                        int _max_k=32, bool _update_base=false );
</PRE>
<P>这个类的方法训练K近邻模型。它遵循一个一般训练方法约定的限制:只支持CV_ROW_SAMPLE数据格式,输入向量必须都是有须的,而输出可以是 
无序的(当is_regression=false),可以是有序的(is_regression=true)。并且变量子集和省略度量是不被支持的。 </P>
<P>参数_max_k 指定了最大邻居的个数,它将被传给方法find_nearest。参数 _update_base 
指定模型是由原来的数据训练(_update_base=false),还是被新训练数据更新後再训练(_update_base=true)。在後一种情况下_max_k 
不能大于原值, 否则它会被忽略. </P>
<P><BR></P>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=20">编辑</A>]</DIV><A 
name=CvKNearest::find_nearest></A>
<H2>CvKNearest::find_nearest</H2>
<P>寻找输入向量的最近邻 </P><PRE>float CvKNearest::find_nearest( const CvMat* _samples, int k, CvMat* results=0,
        const float** neighbors=0, CvMat* neighbor_responses=0, CvMat* dist=0 ) const;
</PRE>
<P>For each input vector (which are rows of the matrix _samples) the method 
finds k≤get_max_k() nearest neighbor. In case of regression, the predicted 
result will be a mean value of the particular vector's neighbor responses. In 
case of classification the class is determined by voting. </P>
<P>For custom classification/regression prediction, the method can optionally 
return pointers to the neighbor vectors themselves (neighbors, array of 
k*_samples-&gt;rows pointers), their corresponding output values 
(neighbor_responses, a vector of k*_samples-&gt;rows elements) and the distances 
from the input vectors to the neighbors (dist, also a vector of 
k*_samples-&gt;rows elements). </P>
<P>For each input vector the neighbors are sorted by their distances to the 
vector. </P>
<P>In case of a single input vector all the output matrices are optional and the 
predicted value is returned by the method. </P>
<P><BR></P>
<DIV class=editsection style="FLOAT: right; MARGIN-LEFT: 5px">[<A 
title=机器学习中文参考手册 
href="http://www.opencv.org.cn/index.php?title=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E4%B8%AD%E6%96%87%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&amp;action=edit&amp;section=21">编辑</A>]</DIV><A 
name=.E4.BE.8B.E7.A8.8B.EF.BC.9A.E4.BD.BF.E7.94.A8kNN.E8.BF.9B.E8.A1.8C2.E7.BB.B4.E6.A0.B7.E6.9C.AC.E9.9B.86.E7.9A.84.E5.88.86.E7.B1.BB.EF.BC.8C.E6.A0.B7.E6.9C.AC.E9.9B.86.E7.9A.84.E5.88.86.E5.B8.83.E4.B8.BA.E6.B7.B7.E5.90.88.E9.AB.98.E6.96.AF.E5.88.86.E5.B8.83></A>
<H2>例程:使用kNN进行2维样本集的分类,样本集的分布为混合高斯分布</H2><PRE>#include "ml.h"
#include "highgui.h"

int main( int argc, char** argv )
{
    const int K = 10;
    int i, j, k, accuracy;
    float response;
    int train_sample_count = 100;
    CvRNG rng_state = cvRNG(-1);
    CvMat* trainData = cvCreateMat( train_sample_count, 2, CV_32FC1 );
    CvMat* trainClasses = cvCreateMat( train_sample_count, 1, CV_32FC1 );
    IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
    float _sample[2];
    CvMat sample = cvMat( 1, 2, CV_32FC1, _sample );
    cvZero( img );

    CvMat trainData1, trainData2, trainClasses1, trainClasses2;

    // form the training samples
    cvGetRows( trainData, &amp;trainData1, 0, train_sample_count/2 );
    cvRandArr( &amp;rng_state, &amp;trainData1, CV_RAND_NORMAL, cvScalar(200,200), cvScalar(50,50) );

    cvGetRows( trainData, &amp;trainData2, train_sample_count/2, train_sample_count );
    cvRandArr( &amp;rng_state, &amp;trainData2, CV_RAND_NORMAL, cvScalar(300,300), cvScalar(50,50) );

    cvGetRows( trainClasses, &amp;trainClasses1, 0, train_sample_count/2 );

⌨️ 快捷键说明

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