Opencv 300 - 随机森林预测返回错误答案

时间:2015-11-14 02:51:45

标签: c++ opencv random-forest opencv3.0 predict

你们是否知道OpenCV 300中以下简单的随机森林示例有什么问题(它总是预测" 0"这是错误的):

Mat train_data= (Mat_<int>(6,3) << 1, 1, 1, 2, 2, 2, -1, -1, -1, 0, 1, 2, 2, 3, 4, -1, -2, -3);
Mat response = (Mat_<int>(1,6) << 0,0,0,1, 1, 1);

Ptr<TrainData> tdata = TrainData::create(train_data, ROW_SAMPLE, response);

Ptr<RTrees> model;
    model = RTrees::create();
    model->setMaxDepth(4);
    model->setMinSampleCount(5);
    model->setRegressionAccuracy(0);
    model->setUseSurrogates(false);
    model->setMaxCategories(15);
    model->setPriors(Mat());
    model->setCalculateVarImportance(true);
    model->setActiveVarCount(4);
    model->setTermCriteria(TC(100,0.01f));
    model->train(tdata);

Mat sample;
sample = (Mat_<float>(1,3) << 0,0,0);  // if I use <int> I'll get error
cout << model->predict(sample) <<"\n";

sample = (Mat_<float>(1,3) << -4,-5,-6);
cout << model->predict(sample) <<"\n";

sample = (Mat_<float>(1,3) << 9,9,9);
cout << model->predict(sample) <<"\n";

sample = (Mat_<float>(1,3) << 19,20,21);
cout << model->predict(sample) <<"\n";

谢谢,

1 个答案:

答案 0 :(得分:1)

我知道我可能有点晚了,但我遇到了与OpenCV 2.4.13相同的问题,看起来OpenCV的RandomTrees算法并不喜欢值为0的类,

我的意思是如果响应 Matrice中的一个或多个元素为0,则RTree算法将始终预测为0.

我通过将响应 Matrice中的所有0替换为另一个值来解决它(例如,在你的情况下为2就可以了)。