andrea
andrea

Reputation: 1358

CvNormalBayesClassifier

I'm using a CvNormalBayesClassifier. I'm training it with some random data, like in the code I'm posting

CvMat* train = cvCreateMat( 100, 32, CV_32FC1 );
cvZero( train );
cvmSet( train, 0, 0, (double) 2 ); 
cvmSet( train, 0, 1, (double) 5 ); 
cvmSet( train, 1, 17, (double) 12 ); 
cvmSet( train, 1, 9, (double) 235 ); 
cvmSet( train, 29, 1, (double) 645 );
cvmSet( train, 34, 12, (double) 65 );  
cvmSet( train, 23, 3, (double) 2.64 ); 
cvmSet( train, 27, 8, (double) 5443 ); 
cvmSet( train, 3, 7, (double) 125432 ); 
cvmSet( train, 67, 14, (double) 6533 ); 
cvmSet( train, 78, 18, (double) 43265 );
cvmSet( train, 92, 12, (double) 65.543 ); 
CvMat* res=cvCreateMat( 1, 100, CV_32FC1 );
cvZero( res );
cvSet( train, cvScalarAll(CV_VAR_ORDERED));
cvSet( res, cvScalarAll(CV_VAR_CATEGORICAL));
M1.train(train, res);
CvMat* prova = cvCreateMat( 1, 32, CV_32FC1 );
cvZero( prova );
cvmSet( prova, 0, 7, (double) 10 ); 
float result=M1.predict(prova);

the problem is that, even if every element in the training is in the "0" category, I can get "1" as result of predict(). another problem is that I tried to change some values in the training set and then save the classifier. as result I get that the classifier are always the same if I don't change the size of the training set (even with totally different values)

how is that possible? does anyone can tell me how the train, save and predict function works? thank you

Upvotes: 0

Views: 901

Answers (1)

Kevin
Kevin

Reputation: 771

I found that to get proper results from the ml library you need to create and access the classifier like below:

bayes = new CvNormalBayesClassifier();
bayes->train( samplesMat, responses );

The above code snip worked for me with some training data.

Upvotes: 1

Related Questions