使用OpenCV随机森林进行回归

Using OpenCV Random Forest for Regression

本文关键字:回归 森林 OpenCV 随机 使用      更新时间:2023-10-16

我以前使用Random Forest进行分类任务,使用这里的示例设置参数作为指导。效果很好。但是现在我想解决一个回归问题。

我有一些想法,这与var_type Mat有关,该Mat定义了随机森林训练方法中的数据类型,但不太确定这些标志对应的是什么。

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

// 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;

和参数设置:

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随机森林,使其作为一个回归器。我已经搜索了很多,但一直未能找到答案。我得到的最接近的解释就是这个答案。然而,这仍然没有任何意义。

我正在寻找一个简单的答案,解释var_type和参数的回归。

要使用它进行回归,您只需将var_type设置为CV_VAR_ORDERED即

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

,您可能希望将regression_accuracy设置为一个非常小的数字,例如0.0001f。