贝叶斯分类器(Normal Bayes 分类器)
Normal Bayes 分類(lèi)器
這個(gè)簡(jiǎn)單的分類(lèi)器模型是建立在每一個(gè)類(lèi)別的特征向量服從正態(tài)分布的基礎(chǔ)上的(盡管,不必是獨(dú)立的),因此,整個(gè)分布函數(shù)被假設(shè)為一個(gè)高斯分布,每一類(lèi)別一組系數(shù)。當(dāng)給定了訓(xùn)練數(shù)據(jù),算法將會(huì)估計(jì)每一個(gè)類(lèi)別的向量均值和方差矩陣,然后根據(jù)這些進(jìn)行預(yù)測(cè)。
CvNormalBayesClassifier
對(duì)正態(tài)分布的數(shù)據(jù)的貝葉斯分類(lèi)器
class CvNormalBayesClassifier?: 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:... }; [編輯]CvNormalBayesClassifier::train
訓(xùn)練這個(gè)模型
bool CvNormalBayesClassifier::train( const CvMat* _train_data, const CvMat* _responses,const CvMat* _var_idx = 0, const CvMat* _sample_idx=0, bool update=false );這個(gè)函數(shù)訓(xùn)練正態(tài)貝葉斯分類(lèi)器。并且遵循通常訓(xùn)練“函數(shù)”的以下一些限制:只支持CV_ROW_SAMPLE類(lèi)型的數(shù)據(jù),輸入的變量全部應(yīng)該是有序的,輸出的變量是一個(gè)分類(lèi)結(jié)果。(例如,_responses中的元素必須是整數(shù),因此向量的類(lèi)型有可能是32fC1類(lèi)型的),不支持missing, measurements。
另外,有一個(gè)update標(biāo)志,標(biāo)志著模型是否使用新數(shù)據(jù)升級(jí)。 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).
[編輯]CvNormalBayesClassifier::predict
對(duì)未知的樣本或或本集進(jìn)行預(yù)測(cè)
float CvNormalBayesClassifier::predict( const CvMat* samples, CvMat* results=0 ) const;這個(gè)函數(shù)估計(jì)輸入向量的最有可能的類(lèi)別。輸入向量(一個(gè)或多個(gè))被儲(chǔ)存在矩陣的每一行中。對(duì)于多個(gè)輸入向量,則輸出會(huì)是一個(gè)向量結(jié)果。對(duì)于單一的輸入,函數(shù)本身的返回值就是預(yù)測(cè)結(jié)果。 長(zhǎng)段文字
//openCV中貝葉斯分類(lèi)器的API函數(shù)用法舉例 //運(yùn)行環(huán)境:winXP + VS2008 + openCV2.3.0 #include "stdafx.h" #include "opencv.hpp" #include "iostream" using namespace cv; using namespace std;//10個(gè)樣本特征向量維數(shù)為12的訓(xùn)練樣本集,第一列為該樣本的類(lèi)別標(biāo)簽 double inputArr[10][13] = {1,0.708333,1,1,-0.320755,-0.105023,-1,1,-0.419847,-1,-0.225806,0,1, -1,0.583333,-1,0.333333,-0.603774,1,-1,1,0.358779,-1,-0.483871,0,-1,1,0.166667,1,-0.333333,-0.433962,-0.383562,-1,-1,0.0687023,-1,-0.903226,-1,-1,-1,0.458333,1,1,-0.358491,-0.374429,-1,-1,-0.480916,1,-0.935484,0,-0.333333,-1,0.875,-1,-0.333333,-0.509434,-0.347032,-1,1,-0.236641,1,-0.935484,-1,-0.333333,-1,0.5,1,1,-0.509434,-0.767123,-1,-1,0.0534351,-1,-0.870968,-1,-1,1,0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,1,0.25,1,1,-0.698113,-0.484018,-1,1,0.0839695,1,-0.612903,0,-0.333333,1,0.291667,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333,1,0.416667,-1,1,0.0566038,0.283105,-1,1,0.267176,-1,0.290323,0,1 };//一個(gè)測(cè)試樣本的特征向量 double testArr[]= {0.25,1,1,-0.226415,-0.506849,-1,-1,0.374046,-1,-0.83871,0,-1 };int _tmain(int argc, _TCHAR* argv[]) {Mat trainData(10, 12, CV_32FC1);//構(gòu)建訓(xùn)練樣本的特征向量for (int i=0; i<10; i++){for (int j=0; j<12; j++){trainData.at<float>(i, j) = inputArr[i][j+1];}}Mat trainResponse(10, 1, CV_32FC1);//構(gòu)建訓(xùn)練樣本的類(lèi)別標(biāo)簽for (int i=0; i<10; i++){trainResponse.at<float>(i, 0) = inputArr[i][0];}CvNormalBayesClassifier nbc;bool trainFlag = nbc.train(trainData, trainResponse);//進(jìn)行貝葉斯分類(lèi)器訓(xùn)練if (trainFlag){cout<<"train over..."<<endl;nbc.save("c:/normalBayes.txt");}else{cout<<"train error..."<<endl;system("pause");exit(-1);}CvNormalBayesClassifier testNbc;testNbc.load("c:/normalBayes.txt");Mat testSample(1, 12, CV_32FC1);//構(gòu)建測(cè)試樣本for (int i=0; i<12; i++){testSample.at<float>(0, i) = testArr[i];}float flag = testNbc.predict(testSample);//進(jìn)行測(cè)試cout<<"flag = "<<flag<<endl;system("pause");return 0; }
總結(jié)
以上是生活随笔為你收集整理的贝叶斯分类器(Normal Bayes 分类器)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 两个栈来实现一个队列的C++代码
- 下一篇: Normal Bayes 分类器过程详解