使用OpenCV随机森林进行回归

时间:2014-06-20 12:24:56

标签: c++ opencv machine-learning regression random-forest

我以前使用Random Forest for Classification任务,使用示例here作为指南设置参数。它完美无缺。但是现在我想解决一个回归问题。

我有点认为这与定义随机森林列车方法中的数据类型的var_type Mat有关,但不确定每个标志对应的内容。

对于Classifcation任务,它看起来像这样(从上面的链接复制的代码):

// define all the attributes as numerical
// alternatives are CV_VAR_CATEGORICAL or CV_VAR_ORDERED(=CV_VAR_NUMERICAL)
// that can be assigned on a per attribute basis

Mat var_type = Mat(ATTRIBUTES_PER_SAMPLE + 1, 1, CV_8U );
var_type.setTo(Scalar(CV_VAR_NUMERICAL) ); // all inputs are numerical

// this is a classification problem (i.e. predict a discrete number of class
// outputs) so reset the last (+1) output var_type element to CV_VAR_CATEGORICAL

var_type.at<uchar>(ATTRIBUTES_PER_SAMPLE, 0) = CV_VAR_CATEGORICAL;

params设置:

float priors[] = {1,1,1,1,1,1,1,1,1,1};  // weights of each classification for classes
    // (all equal as equal samples of each digit)

CvRTParams params = CvRTParams(25, // max depth
                                5, // min sample count
                                0, // regression accuracy: N/A here
                            false, // compute surrogate split, no missing data                                    
                               15, // max number of categories (use sub-optimal algorithm for larger numbers)
                            priors, // the array of priors
                            false,  // calculate variable importance
                                4,       // number of variables randomly selected at node and used to find the best split(s).
                              100,   // max number of trees in the forest
                             0.01f,             // forrest accuracy
         CV_TERMCRIT_ITER | CV_TERMCRIT_EPS // termination cirteria
                                  );

Training使用var_type和params,如下所示:

CvRTrees* rtree = new CvRTrees;

rtree->train(training_data, CV_ROW_SAMPLE, training_classifications,
                 Mat(), Mat(), var_type, Mat(), params);

我的问题是如何设置OpenCV随机森林以使其作为回归量。我搜索了很多,但未能找到答案。我得到的最接近的解释是this回答。但它仍然没有任何意义。

我正在寻找一个简单的答案来解释var_type和params for regression。

1 个答案:

答案 0 :(得分:3)

要将其用于回归,您只需将var_type设置为CV_VAR_ORDERED,即

var_type.at<uchar>(ATTRIBUTES_PER_SAMPLE, 0) = CV_VAR_ORDERED;

你可能想将regression_accuracy设置为一个非常小的数字,如0.0001f。