我以前使用随机森林进行分类任务,使用示例设置参数 here作为指南。它工作完美。但是现在我想解决回归问题。
我有一些想法,这与在随机森林训练方法中定义数据类型的 var_type 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
);
训练使用 var_type 和参数如下:
CvRTrees* rtree = new CvRTrees;
rtree->train(training_data, CV_ROW_SAMPLE, training_classifications,
Mat(), Mat(), var_type, Mat(), params);
我的问题是如何设置 OpenCV 随机森林,使其作为回归器工作。我已经搜索了很多,但一直无法找到答案。我得到的最接近的解释是 this回答。然而,它仍然没有任何意义。
我正在寻找一个简单的答案来解释回归的 var_type 和参数。
最佳答案
要将其用于回归,您只需将 var_type 设置为 CV_VAR_ORDERED 即
var_type.at<uchar>(ATTRIBUTES_PER_SAMPLE, 0) = CV_VAR_ORDERED;
并且您可能希望将 regression_accuracy 设置为非常小的数字,例如 0.0001f。
关于c++ - 使用 OpenCV 随机森林进行回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24327230/