Create train data for RTree with selected features not working

asked 2017-09-11 09:00:53 -0500

VxW gravatar image

Hi@all

I would like to train a RTree (using Opencv 3.3, Windows 7) with a selected number of features but it seems to be that the feature selection is not working.

I have created with the following lines the training data

int var_count = 14

cv::Mat data = cv::Mat(nsamples_all, var_count, CV_32F);
//... and fill each row with var_count features

cv::Mat responses = cv::Mat(nsamples_all, 1, CV_32F);
//... and fill responses with labels

cv::Mat var_idx = Mat::zeros(1, var_count, CV_8U);
for (int i = 0; i<numFeatures; i++)
{
    if(i is feature of your choice)
          var_idx.at<uchar>(i) = 1; 
}

int nvars = data.cols;
Mat var_type(1, nvars+1, CV_8U);
var_type.setTo(Scalar::all(VAR_ORDERED));
var_type.at<uchar>(nvars) = VAR_CATEGORICAL;

TrainData::create(data, ROW_SAMPLE, responses, varIdx, noArray(), noArray(), var_type);

After training I'm getting unreliable output and var_importance (error=10.23):

var#    importance (in %):
0   0.0 
1   9.3 
2   9.0 
3   9.1 
4   9.1 
5   0.0 
6   0.0 
7   0.0 
8   9.0 
9   9.7 
10  8.6 
11  9.1 
12  9.1 
13  9.1 
14  9.1

If I fill the data only with the features I want do use and when I call create without varIdx

TrainData::create(data, ROW_SAMPLE, responses, noArray(), noArray(), noArray(), var_type);

I'm getting reliable and working results (error=4.31)

var#    importance (in %):
0   12.0
1   8.2 
2   4.8 
3   3.7 
4   3.7 
5   6.0 
6   33.8
7   15.3
8   2.7 
9   4.4 
10  5.4

Any idea what happend or what I'm doing wrong?

Thanks, Volkmar

edit retag flag offensive close merge delete