Ask Your Question

# Revision history [back]

### Exception on training RTrees with categorical data and categorical responses

Hi all, I get the following exception when trying to train an RTree with categorical data and more than two different categorical responses:

OpenCV Error: Assertion failed (sum > 0) in findSplitCatClass, file /home/.../dev/opencv-3.1.0/modules/ml/src/tree.cpp, line 880 terminate called after throwing an instance of 'cv::Exception' what(): /home/.../dev/opencv-3.1.0/modules/ml/src/tree.cpp:880: error: (-215) sum > 0 in function findSplitCatClass

My sample code is as follows:

vector<int> responses;
responses.push_back(2);
responses.push_back(0);
responses.push_back(0);
responses.push_back(0);
responses.push_back(0);
responses.push_back(1);
Mat resp;
Mat(responses).copyTo(resp);

Mat dat;
vector<int> data(6, 0);
Mat(data).copyTo(dat);

Mat types(2, 1, CV_8U);
types.at<uchar>(0) = VAR_CATEGORICAL;
types.at<uchar>(1) = VAR_CATEGORICAL;

Ptr<TrainData> tdata = TrainData::create(dat, ROW_SAMPLE, resp, noArray(), noArray(), noArray(), types);

Ptr<RTrees> model;
model = RTrees::create();
model->setMinSampleCount(4);
int success = model->train(tdata);


The exception does not occur if only two different response values are present (e.g. change the 1 to 2 or the 2 to 1) or if the data or the responses are chosen as type VAR_ORDERED.

What is the reason for the exception and how can I avoid it?