Ask Your Question

etecman's profile - activity

2017-03-08 05:02:32 -0500 received badge  Popular Question (source)
2014-01-21 03:08:53 -0500 received badge  Good Question (source)
2013-04-18 09:54:03 -0500 received badge  Nice Question (source)
2012-09-24 23:52:30 -0500 received badge  Student (source)
2012-09-24 14:29:25 -0500 asked a question Random forests CvRTrees

Hi all,

I am experimenting with the random forests class of openCV (CvRTrees) and I am encountering some troubles. In my example code, I simply create training data with two variables and twenty samples, 10 belonging to the first class and 10 belonging to the second class - so the classification task should be very easy.

The training works but when calling the get_train_error() function, the program crashes each time in CvDTreeTrainData::get_ord_var_data.

Did I discover a bug or did I - as a newbie - used the random forests in a wrong way? Did you encounter similar problems?

Thanks a lot in advance, Stefan

int numSamples = 10;
int numFeatures = 2;

cv::Mat trainingData(numSamples*2,numFeatures, CV_32FC1);
cv::Mat trainingClassification(numSamples*2,1, CV_32FC1);


for (int i=0; i<numSamples*2; i+=2)
{
    trainingData.at<float>(i,0) = 1.0f;
    trainingData.at<float>(i,1) = 0.0f;
    trainingClassification.at<float>(i,0) = 1.0f;

    trainingData.at<float>(i+1,0) = 0.0f;
    trainingData.at<float>(i+1,1) = 1.0f;
    trainingClassification.at<float>(i+1,0) = 0.0f;
}

cv::Mat var_type = cv::Mat(numFeatures+1 , 1, CV_8U );
var_type.setTo(cv::Scalar(CV_VAR_NUMERICAL) );
var_type.at<uchar>(numFeatures, 0) = CV_VAR_CATEGORICAL;


CvRTParams params;
CvRTrees rtree;
rtree.train(trainingData, CV_ROW_SAMPLE, trainingClassification,
            cv::Mat(), cv::Mat(), var_type, cv::Mat(), params);

std::cout << rtree.get_train_error() << std::endl;