rambo

朴素贝叶斯实践

1、Opencv中的朴素贝叶斯

 

CvNormalBayesClassifier::train(Trains the model)

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.

In addition, there is update flag that identifies, whether the model should be trained from scratch (update=false) or should be updated using new training data (update=true).

CvNormalBayesClassifier::predict(Predicts the response for sample(s))

The method predict estimates the most probable classes for the input vectors. The input vectors (one or more) are stored as rows of the matrixsamples. 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.

先对输入的训练样本计算得到每一类的特征均值、协方差矩阵、特征值,然后分类的时候就将输入样本放入predict函数,函数内部先使用训练获得的以上参数计算样本属于每一类的后验概率,取后验概率最大的就是那一类。

讨论:predict函数中没有计算p(w)的先验概率。只有同时考虑类间距和类内距来计算相似度的时候才要用到先验概率,如果不考虑类间距,就只需要计算p(x|w)。在"Bayesian face recognition"它是用高维高斯分布计算的。用主成分分析优化计算的。

参考资料:

1)opencv2.3.1 源码

2)http://blog.csdn.net/godenlove007/article/details/8913007

附录:

训练模型形式