贝叶斯分类器(Normal Bayes 分类器)
Normal Bayes 分類器
這個簡單的分類器模型是建立在每一個類別的特征向量服從正態分布的基礎上的(盡管,不必是獨立的),因此,整個分布函數被假設為一個高斯分布,每一類別一組系數。當給定了訓練數據,算法將會估計每一個類別的向量均值和方差矩陣,然后根據這些進行預測。
CvNormalBayesClassifier
對正態分布的數據的貝葉斯分類器
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
訓練這個模型
bool CvNormalBayesClassifier::train( const CvMat* _train_data, const CvMat* _responses,const CvMat* _var_idx = 0, const CvMat* _sample_idx=0, bool update=false );這個函數訓練正態貝葉斯分類器。并且遵循通常訓練“函數”的以下一些限制:只支持CV_ROW_SAMPLE類型的數據,輸入的變量全部應該是有序的,輸出的變量是一個分類結果。(例如,_responses中的元素必須是整數,因此向量的類型有可能是32fC1類型的),不支持missing, measurements。
另外,有一個update標志,標志著模型是否使用新數據升級。 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
對未知的樣本或或本集進行預測
float CvNormalBayesClassifier::predict( const CvMat* samples, CvMat* results=0 ) const;這個函數估計輸入向量的最有可能的類別。輸入向量(一個或多個)被儲存在矩陣的每一行中。對于多個輸入向量,則輸出會是一個向量結果。對于單一的輸入,函數本身的返回值就是預測結果。 長段文字
//openCV中貝葉斯分類器的API函數用法舉例 //運行環境:winXP + VS2008 + openCV2.3.0 #include "stdafx.h" #include "opencv.hpp" #include "iostream" using namespace cv; using namespace std;//10個樣本特征向量維數為12的訓練樣本集,第一列為該樣本的類別標簽 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 };//一個測試樣本的特征向量 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);//構建訓練樣本的特征向量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);//構建訓練樣本的類別標簽for (int i=0; i<10; i++){trainResponse.at<float>(i, 0) = inputArr[i][0];}CvNormalBayesClassifier nbc;bool trainFlag = nbc.train(trainData, trainResponse);//進行貝葉斯分類器訓練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);//構建測試樣本for (int i=0; i<12; i++){testSample.at<float>(0, i) = testArr[i];}float flag = testNbc.predict(testSample);//進行測試cout<<"flag = "<<flag<<endl;system("pause");return 0; }
總結
以上是生活随笔為你收集整理的贝叶斯分类器(Normal Bayes 分类器)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两个栈来实现一个队列的C++代码
- 下一篇: Normal Bayes 分类器过程详解